Skip to main content
Be Bitwise

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.
Key Concept

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:

c
MOV R0, #5 ; load the value 5 into R0
MOV R1, #3 ; load the value 3 into R1
ADD R0, R1 ; R0 = R0 + R1 = 8
MOV R2, #2 ; load the value 2 into R2
MUL R0, R2 ; R0 = R0 * R2 = 16
STR R0, [0x200] ; store the result to memory address 0x200
HALT ; stop execution

This 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: 1/9500ms
F = FetchD = DecodeE = ExecuteM = MemoryW = WriteBubble/Stall
CycleFDEMW
1ADD R1, R2, R3----
2SUB R4, R5, R6ADD R1, R2, R3---
3AND R7, R8, R9SUB R4, R5, R6ADD R1, R2, R3--
4OR R10, R11, R12AND R7, R8, R9SUB R4, R5, R6ADD R1, R2, R3-
5ADD R13, R14, ..OR R10, R11, R12AND R7, R8, R9SUB R4, R5, R6ADD R1, R2, R3
6-ADD R13, R14, ..OR R10, R11, R12AND R7, R8, R9SUB R4, R5, R6
7--ADD R13, R14, ..OR R10, R11, R12AND R7, R8, R9
8---ADD R13, R14, ..OR R10, R11, R12
9----ADD R13, R14, ..
Instructions: 5Total cycles: 9Stall cycles: 0CPI: 1.80

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.

Key Concept

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: 1/9500ms
F = FetchD = DecodeE = ExecuteM = MemoryW = WriteBubble/StallHazard
CycleFDEMW
1ADD R1, R2, R3----
2stallADD R1, R2, R3stallstallstall
3stallstallADD R1, R2, R3stallstall
4SUB R4, R1, R5--ADD R1, R2, R3-
5AND R6, R7, R8SUB R4, R1, R5--ADD R1, R2, R3
6-AND R6, R7, R8SUB R4, R1, R5--
7--AND R6, R7, R8SUB R4, R1, R5-
8---AND R6, R7, R8SUB R4, R1, R5
9----AND R6, R7, R8

Detected RAW Hazards

#2 SUB R4, R1, R5 reads R1 written by #1 ADD R1, R2, R3 -> 2 stalls
Instructions: 3Total cycles: 9Stall cycles: 2CPI: 3.00