The Instruction Cycle
CPU Basics · Foundations track
5 min read
In this module
The Four Stages
We've discussed fetch, decode, and execute as separate ideas. Time to put them together into the complete instruction cycle, the loop the CPU repeats for every single instruction it runs. Most descriptions break it into four stages:
-
Fetch. The CPU reads the next instruction from memory. The program counter (PC) holds the address of that instruction. After fetching, the PC is incremented to point to the next one.
-
Decode. The control unit examines the fetched instruction and works out what operation to perform, which registers or memory addresses are involved, and what control signals to send.
-
Execute. The ALU or another functional unit carries out the operation. Could be an addition, a comparison, a memory read, or a jump to a different address.
-
Store / Writeback. The result is written back to a register or to memory. Not every instruction has a writeback stage (a jump instruction just updates the program counter, for example), but most arithmetic and data-movement instructions do.
The instruction cycle has four stages: fetch, decode, execute, and store/writeback. The CPU repeats this cycle for every instruction, billions of times per second. Understanding this cycle is the foundation for understanding how any processor works.
Walking Through an Example
Imagine a simple instruction: ADD R1, R2. Add the contents of register R2 to register R1, and store the result in R1.
Fetch. The CPU reads the instruction from the memory address held in the program counter. The PC then advances to point at the next instruction.
Decode. The control unit recognises this as an ADD operation. It identifies R1 as both a source and the destination, and R2 as the other source.
Execute. The ALU receives the values from R1 and R2 and performs addition. Flags get updated along the way: if the result is zero, the zero flag is set; if there's a carry, the carry flag is set.
Writeback. The sum produced by the ALU is written back into R1.
The whole sequence might run in one or two clock cycles on a modern pipelined processor. Multiply by billions of cycles per second and you start to see how computers get so much done in so little time.
The Same Loop on a Real CPU
Our teaching CPU gives every instruction the same size, two bytes, so the PC always moves forward by exactly 2. Real x86-64 processors are messier. Instructions run from 1 to 15 bytes long, and the program counter, called rip there, has to advance by however long the instruction it just fetched turned out to be. Working out that length is part of the decode stage's job.
Here are three instructions sitting in memory from address 0x401000:
| Address | Bytes | Instruction | rip after fetch |
|---|---|---|---|
0x401000 | b8 05 00 00 00 | mov eax, 5 | 0x401005 |
0x401005 | 01 d8 | add eax, ebx | 0x401007 |
0x401007 | c3 | ret | (replaced by the return address) |
The mov is five bytes: one for the opcode and four for the number 5, stored little-endian. The add needs only two. And ret is a single byte whose whole job is to overwrite rip with an address popped off the stack, so its "writeback" lands in the program counter itself.
Step through the same eight bytes. Each step is one fetch: decode works out how long the instruction is, and rip jumps by exactly that much.
rip stepping through x86-64 code
Starts at 0x401000; small numbers under each byte are the last two hex digits of its address.
rip points at the first byte. The CPU doesn't yet know how long this instruction is.
Jumps work the same way. Most x86 jumps store a distance rather than a destination, measured from the end of the jump instruction. The CPU has already advanced rip past the jump during fetch, so execute just adds the distance to it.
Predict: the two bytes eb fe are a short jump with a distance of −2. What does the CPU do when it reaches them?
It loops forever on the spot. After fetching the two-byte jump, rip points just past it. Adding −2 takes rip straight back to the start of the same jump, which gets fetched again, and again. eb fe is a well-known "hang here" instruction, used in early boot code and as a quick way to freeze a program at a known point so a debugger can attach.
The Bigger Picture
Every program, from a web browser to a game engine, is ultimately a long list of these simple instructions processed one cycle at a time. The CPU doesn't understand high-level code. It understands this loop: fetch the next instruction, work out what it means, do it, save the result, repeat.
Watch the program counter in the simulator below as you step. It's the one register that changes on every single instruction, even ones that touch nothing else.
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 0Guided lab: predict, then step
This one stops to quiz you. Press Step, and when a question pops up, work the answer out from the instruction you just ran before you glance at the registers. Type numbers in decimal or 0x hex, and flags as 1 for set or 0 for clear. Stuck? Skip shows you the answer.
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 0Try the Simulations
Add Two Numbers
Load two values into registers and add them together. The simplest possible program.
Understanding CPU Flags
Run several comparisons and watch how the CPU flags change. Equal values set the zero flag, smaller-than sets the negative flag - flags are how the CPU remembers comparison results.