The Fetch-Decode-Execute Cycle
Transistors to Processors · Hardware Foundations track
5 min read
In this module
The Heartbeat of Every Processor
A CPU doesn't understand Python, JavaScript, or any high-level language. It only recognises machine instructions — tightly packed binary commands that say: add these numbers, move this data, jump to this address. The mechanism that drives execution, one instruction at a time, is the fetch-decode-execute cycle. Every processor ever built runs on this same loop.
The Key Registers
Before walking through the cycle, you need to know about two critical registers:
- Program Counter (PC) - also called the Instruction Pointer (IP) on x86 architectures. This register holds the memory address of the next instruction to execute. After each fetch, the PC advances to point at the subsequent instruction.
- Instruction Register (IR) - holds the instruction that was most recently fetched from memory. The control unit reads the IR to determine what operation to perform.
Think of the PC as a bookmark — it tracks where the CPU is in the program. The IR is the page the CPU is reading right now.
The Program Counter (PC) points to the next instruction in memory. The Instruction Register (IR) holds the instruction currently being processed. Together, they allow the CPU to step through a program one instruction at a time.
The Three Stages
1. Fetch
The CPU sends the address stored in the PC out over the memory bus and reads back the instruction at that address. The fetched instruction is placed into the IR. The PC is then incremented so it points to the next instruction in sequence.
For a processor with 32-bit instructions, the PC typically advances by 4 bytes (since each instruction is 4 bytes long). On x86, where instructions vary in length, the PC advances by the size of the fetched instruction.
2. Decode
The control unit examines the binary pattern in the IR and breaks it into fields:
- Opcode - identifies the operation (add, subtract, load, store, branch, etc.).
- Operands - specify which registers or memory addresses are involved.
- Immediate values - constant numbers embedded directly in the instruction.
Based on the opcode, the control unit generates control signals that configure the rest of the CPU for the upcoming operation. It might tell the ALU to perform addition, instruct the register file to provide two specific values, or signal the memory unit to prepare for a read.
3. Execute
The operation specified by the decoded instruction is carried out. This may involve the ALU performing arithmetic, data being read from or written to memory, or a result being stored into a register. In many descriptions, this stage also encompasses writeback - writing the result to its destination register.
How Branches Modify the PC
Most instructions let the PC advance sequentially. But branch (or jump) instructions break this pattern. A branch instruction replaces the PC's value with a different address, causing the CPU to continue execution from a new location in the program.
Conditional branches check a condition - such as whether the result of a previous operation was zero - and only modify the PC if the condition is true. This is how programs implement if statements and loops: by conditionally jumping forward (to skip code) or backward (to repeat code).
Consider this sequence:
Address 0x100: CMP R0, #0 ; compare R0 with zeroAddress 0x104: BEQ 0x110 ; branch to 0x110 if equalAddress 0x108: ADD R1, R1, #1 ; increment R1Address 0x10C: ...Address 0x110: SUB R2, R2, #1 ; decrement R2If R0 equals zero, the BEQ instruction sets the PC to 0x110, skipping the ADD instruction entirely. Otherwise, the PC advances normally to 0x108.
Before reading on, write down the sequence of PC values the CPU fetches from in that listing, once with R0 = 0 and once with R0 = 5. (Answer: 0x100, 0x104, 0x110 when R0 is zero; 0x100, 0x104, 0x108, 0x10C, 0x110 otherwise.)
Then check it in the simulator. It has its own small instruction set, so JZ (jump if the zero flag is set) stands in for BEQ, and its instructions are 2 bytes long, so the addresses run 0, 2, 4 instead of 0x100, 0x104. Step through with R0 = 0 and watch the PC jump from 4 to 8, then change the first line to MOV R0, #5 and step again. The final JMP done jumps to itself to park the CPU once the interesting part is over.
For the best simulation experience, use a desktop browser.
Assembly
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
Pipeline
Cycle 0The fetch-decode-execute cycle repeats endlessly while the CPU is running. Branch instructions are what allow programs to make decisions and repeat operations - they override the normal sequential advance of the Program Counter with a new address.
See It in Action
Use the simulator below to step through a short program one instruction at a time. Each Step runs one whole fetch-decode-execute cycle. Watch the Program Counter advance by 2, see the decoder panel show the instruction just fetched (its assembly, encoding and opcode), and check which registers changed. R0 should end on 7, the CMP leaves the zero flag clear because 7 isn't 0, and HALT stops the CPU.
For the best simulation experience, use a desktop browser.
Assembly
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |