How a CPU Executes a Program
Transistors to Processors · Hardware Foundations track
5 min read
In this module
From Instructions to Execution
The fetch-decode-execute cycle is clear as an abstraction. Now let's run it on a real program and watch data actually move through the CPU's internal components.
The CPU's Internal Components
A processor has four key subsystems:
- Register File - a small, fast bank of storage locations (R0, R1, R2, etc.) holding the data the CPU is actively working with.
- ALU (Arithmetic Logic Unit) - performs arithmetic (add, subtract) and logical (AND, OR, NOT, XOR) operations on two inputs, producing a result and status flags.
- Control Unit - decodes instructions and generates control signals that orchestrate all other components.
- Memory Bus - the pathway between the CPU and main memory. Load instructions read data in; store instructions write data out.
The register file provides fast storage, the ALU performs computation, the control unit directs operations via control signals, and the memory bus connects the CPU to external memory. Every instruction involves some combination of these four components.
Walking Through a Program
Consider this short program that computes (5 + 3) * 2:
MOV R0, #5 ; load the value 5 into R0MOV R1, #3 ; load the value 3 into R1ADD R0, R1 ; R0 = R0 + R1 = 8MOV R2, #2 ; load the value 2 into R2MUL R0, R2 ; R0 = R0 * R2 = 16STR R0, [0x200] ; store the result to memory address 0x200HALT ; stop executionThis is generic teaching assembly rather than any one real instruction set, and it won't paste straight into the course's CPU simulator, which has no MUL instruction, spells the store STORE [Rn], Rm and only has 256 bytes of memory.
Instruction 1: MOV R0, #5
Fetch - The PC holds the address of this instruction. The CPU reads it from memory into the IR. The PC advances.
Decode - The control unit identifies this as a MOV with an immediate value. The destination is R0, and the constant is 5.
Execute - The value 5 is written directly into register R0. No ALU operation is needed.
Instruction 2: MOV R1, #3
The same process repeats. The value 3 is loaded into R1.
Instruction 3: ADD R0, R1
Fetch/Decode - The control unit recognises an ADD instruction. It configures the register file to output R0 and R1, and sets the ALU to addition mode.
Execute - The ALU receives 5 (from R0) and 3 (from R1), computes 8, and the result is written back to R0. The ALU also updates its flags: the zero flag is cleared (the result is not zero) and the carry flag is cleared (no overflow occurred).
Instruction 4-5: Load 2, Multiply
R2 receives the value 2. The MUL instruction sends R0 (now 8) and R2 (2) to the ALU, which produces 16 and writes it back to R0.
Instruction 6: STR R0, [0x200]
Execute - The CPU places the address 0x200 on the memory bus, sends the value 16 from R0, and signals a write. Main memory stores the value at that address.
Control Signals
At each stage, the control unit asserts specific control signals - binary flags that configure the processor's datapath. For example:
- RegWrite - enables writing to the register file.
- ALUSrc - selects whether the ALU's second input comes from a register or an immediate value.
- MemRead / MemWrite - enables reading from or writing to memory.
- ALUOp - tells the ALU which operation to perform (add, subtract, multiply, etc.).
These signals are the invisible choreography that makes everything happen at the right moment.
Pipelining: Doing More at Once
In the simple model above, the CPU completes one entire instruction before starting the next. But modern processors use pipelining to overlap the stages of different instructions, like an assembly line in a factory. The classic teaching pipeline splits the work into five stages rather than three: Fetch (F), Decode (D), Execute (E), Memory access (M) for loads and stores, and Writeback (W) into the register file.
The visualiser below runs five independent instructions through those stages. Step it cycle by cycle: by cycle 5 every stage is busy with a different instruction, and from then on one instruction finishes per cycle.
Instructions
| Cycle | F | D | E | M | W |
|---|---|---|---|---|---|
| 1 | ADD R1, R2, R3 | - | - | - | - |
| 2 | SUB R4, R5, R6 | ADD R1, R2, R3 | - | - | - |
| 3 | AND R7, R8, R9 | SUB R4, R5, R6 | ADD R1, R2, R3 | - | - |
| 4 | OR R10, R11, R12 | AND R7, R8, R9 | SUB R4, R5, R6 | ADD R1, R2, R3 | - |
| 5 | ADD R13, R14, .. | OR R10, R11, R12 | AND R7, R8, R9 | SUB R4, R5, R6 | ADD R1, R2, R3 |
| 6 | - | ADD R13, R14, .. | OR R10, R11, R12 | AND R7, R8, R9 | SUB R4, R5, R6 |
| 7 | - | - | ADD R13, R14, .. | OR R10, R11, R12 | AND R7, R8, R9 |
| 8 | - | - | - | ADD R13, R14, .. | OR R10, R11, R12 |
| 9 | - | - | - | - | ADD R13, R14, .. |
Pipelining doesn't make any individual instruction faster — each still takes five cycles. But throughput climbs sharply. Under ideal conditions a five-stage pipeline completes one instruction per clock cycle, because each stage is processing a different instruction simultaneously.
Pipelining overlaps the stages (fetch, decode, execute, memory access and writeback) of successive instructions. While one instruction executes, the next is decoded and another is fetched. This increases throughput without speeding up individual instructions.
See It in Action
The assembly line has a catch. In the program below, SUB needs the R1 that ADD has only just computed, and without help that value isn't in the register file until ADD reaches Writeback. Step through and count the bubbles the pipeline inserts before SUB can go. Then tick Forwarding, which routes the ALU result straight back to the next instruction, and watch the stall disappear.
Instructions
| Cycle | F | D | E | M | W |
|---|---|---|---|---|---|
| 1 | ADD R1, R2, R3 | - | - | - | - |
| 2 | stall | ADD R1, R2, R3 | stall | stall | stall |
| 3 | stall | stall | ADD R1, R2, R3 | stall | stall |
| 4 | SUB R4, R1, R5 | - | - | ADD R1, R2, R3 | - |
| 5 | AND R6, R7, R8 | SUB R4, R1, R5 | - | - | ADD R1, R2, R3 |
| 6 | - | AND R6, R7, R8 | SUB R4, R1, R5 | - | - |
| 7 | - | - | AND R6, R7, R8 | SUB R4, R1, R5 | - |
| 8 | - | - | - | AND R6, R7, R8 | SUB R4, R1, R5 |
| 9 | - | - | - | - | AND R6, R7, R8 |