Concepts / Program Execution: From Code to CPU Instructions

Program Execution: From Code to CPU Instructions

The CPU’s primary role is not simply to perform calculations. Its fundamental job is to determine the next operation, interpret that operation, and carry it out.

  • Programming

The CPU’s Central Question

A program may look like a sequence of instructions written by a person, but execution depends on a component that repeatedly determines what should happen next. The Central Processing Unit, or CPU, is the part of the computer built to ask “What is next?” and determine the next operation to execute. It performs this work at a scale measured in billions of cycles per second.

Key point

The CPU’s primary role is not simply to perform calculations. Its fundamental job is to determine the next operation, interpret that operation, and carry it out.

The Repeating Execution Cycle

FetchRetrieve the nextinstructionDecodeDetermine what it meansExecutePerform the operationNext instructionReturn to Fetchinstruction retrievedmeaning identifiedoperation completedrepeat
How does the CPU repeatedly determine what operation comes next and then carry it out?

The CPU follows a cycle called fetch-decode-execute. First, it fetches the next instruction from memory. Next, it decodes the instruction by determining what it means. Then it executes the operation. After execution, the CPU immediately returns to the fetch phase and retrieves the next instruction. This cycle continues throughout program execution.

  • Fetch: retrieve the next instruction from memory
  • Decode: determine what the instruction means
  • Execute: perform the operation
  • Repeat: fetch the next instruction and continue the cycle

From Program to Instruction Sequence

Written programA programmer’s orderedinstructionsInstruction listOperations for the CPU tofollowFetchRetrieve the nextinstructionDecodeInterpret the instructionExecutePerform the operationexpressesprovides next operationretrievesidentifies meaning
How does a human-readable program become the individual instructions the CPU executes?

When you write a program, you are essentially giving the CPU a list of instructions to execute in order. The CPU works through those instructions using fetch, decode, and execute. The important idea is that execution is not one large, mysterious event: the CPU repeatedly handles the next instruction, understands it, performs it, and then moves on.

Gigahertz and Repeated Timing

0 secondsBeginning of the intervalOne cycleOne repeated timing cycleOne billion cycles1 GHz in one second1 secondEnd of the intervalrepeatedone billion timeswithin
What does one gigahertz represent, and how does it relate to the repeated timing of CPU operations?
Definition

Gigahertz, abbreviated GHz, is a measure of CPU speed. One gigahertz means one billion cycles per second.

A 3.0 GHz CPU is described in the source as asking “What is next?” three billion times per second. In that same perspective, three billion decisions, three billion instruction fetches, and three billion execution cycles happen in one second. Modern CPUs often run between 2.0 and 5.0 GHz, although some specialized processors can go faster.

RatingCycles per secondInterpretation
1.0 GHzOne billionOne billion cycles per second
3.0 GHzThree billionThree billion cycles per second
5.0 GHzFive billionFive billion cycles per second

Gigahertz expresses the number of cycles occurring in one second.

A Single Instruction Trace

Worked example

Tracing Three Abstract Operations

Suppose a program provides the CPU with three ordered operations: instruction A, instruction B, and instruction C. How does the CPU handle them?

  1. 1
    First fetch

    The CPU retrieves instruction A from memory because it is the next instruction in the sequence.

  2. 2
    First decode

    The CPU determines what instruction A means.

  3. 3
    First execute

    The CPU performs the operation represented by instruction A.

  4. 4
    Second instruction

    The CPU returns to fetch, retrieves instruction B, decodes it, and executes it.

  5. 5
    Third instruction

    The CPU repeats the same cycle for instruction C.

Result

The CPU processes the ordered instruction sequence by repeatedly fetching, decoding, and executing the next operation.

This example uses abstract instruction names rather than a particular programming language. Its purpose is to show the repeated state change: the CPU moves from one next instruction to the following instruction, and each one passes through fetch, decode, and execute.

What do you think happens?

After the CPU executes instruction B, what does it do next?

  • It stops immediately

  • It fetches instruction C

  • It decodes instruction A again before doing anything else

Reveal answer
Answer:

It fetches instruction C.

The fetch-decode-execute cycle returns to fetching the next instruction after an operation is executed.

The CPU’s Internal Team

The CPU does not carry out this work through one undifferentiated part. The Control Unit directs the flow of instructions and coordinates the fetch-decode-execute cycle. The Arithmetic Logic Unit, or ALU, performs calculations and logical operations. Registers are tiny, ultra-fast storage locations holding data the CPU is currently working with. Cache is a small, very fast memory that stores frequently used data so the CPU does not have to wait for slower main memory.

CPU componentRole described in the source
Control UnitDirects instruction flow and coordinates fetch-decode-execute
Arithmetic Logic UnitPerforms calculations and logical operations
RegistersHold data the CPU is currently working with
CacheStores frequently used data in very fast memory

Several CPU components work together during instruction execution.

Why Repetition Matters

The CPU’s speed makes repeated work important. If a loop runs a million times and each iteration performs unnecessary work, the CPU repeats that unnecessary work a million times. Efficient code helps the CPU complete the intended work so quickly that a program can feel instant to the user.

Common Misunderstandings

  • Common mistake

    Thinking CPU speed is measured only by the number of calculations a CPU performs.

    What it looks like

    Describing 3.0 GHz as simply “three billion calculations per second.”

    Why it fails

    The source defines GHz as one billion cycles per second and connects the CPU’s speed to repeated decisions, instruction fetches, and execution cycles.

    Do this instead

    Describe 3.0 GHz as three billion cycles per second, while remembering that the CPU’s broader role is to fetch, decode, and execute instructions.

  • Common mistake

    Stopping the execution model after the execute phase.

    What it looks like

    Assuming the CPU fetches one instruction, executes it, and then finishes.

    Why it fails

    The fetch-decode-execute cycle immediately returns to fetching the next instruction.

    Do this instead

    Treat execution as a repeating cycle: fetch, decode, execute, and fetch again.

  • Common mistake

    Treating the CPU as if it works alone.

    What it looks like

    Ignoring the Control Unit, ALU, registers, and cache when explaining execution.

    Why it fails

    These components work together to execute instructions at high speed.

    Do this instead

    Explain the CPU as a coordinated collection of components with different roles.

  • Common mistake

    Assuming every inefficiency matters equally.

    What it looks like

    Focusing on one unnecessary operation that happens once while ignoring unnecessary work repeated a million times.

    Why it fails

    The source emphasizes that repeated work, especially inside a loop, is multiplied across many executions.

    Do this instead

    Pay particular attention to unnecessary operations that occur repeatedly.

Check Your Understanding

EASY

In your own words, describe what happens after the CPU fetches an instruction. Then explain what 2.0 GHz means in terms of cycles per second.

Hints
  • Use the words decode and execute.
  • One gigahertz means one billion cycles per second.
  • Multiply one billion by the numerical GHz rating.
MEDIUM

A loop performs unnecessary work on every one of its million iterations. Explain why this may matter even though the unnecessary operation in one iteration seems small.

Hints
  • Focus on repetition.
  • The CPU executes the repeated operation each time the loop iterates.

Execution in One View

  1. 1
    The CPU is the computer component whose central role is determining what operation comes next.
  2. 2
    It executes instructions through the repeating fetch-decode-execute cycle.
  3. 3
    Gigahertz measures cycles per second; one GHz means one billion cycles per second.
  4. 4
    The Control Unit, ALU, registers, and cache work together inside the CPU.
  5. 5
    Repeated unnecessary work can become significant because the CPU performs it again and again.

Key Takeaways

  • The CPU repeatedly asks “What is next?” by determining the next operation to execute.
  • Program execution follows fetch, decode, execute, and then returns to fetch for the next instruction.
  • One GHz equals one billion cycles per second, so a 3.0 GHz CPU is described as operating across three billion cycles per second.
  • The Control Unit, ALU, registers, and cache contribute different roles to instruction execution.
  • Efficient code matters especially when unnecessary work is repeated many times.

More concepts starting with P