Skip to main content
Be Bitwise

The Instruction Cycle

CPU Basics · Foundations track

5 min read

Part ofSystems Programming Companion, week 6

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

Key Concept

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:

AddressBytesInstructionrip after fetch
0x401000b8 05 00 00 00mov eax, 50x401005
0x40100501 d8add eax, ebx0x401007
0x401007c3ret(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 = 0x401000rip = 0x401000

rip points at the first byte. The CPU doesn't yet know how long this instruction is.

Step 1 of 4

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

200ms

Registers

R00
0x0000
R10
0x0000
R20
0x0000
R30
0x0000
R40
0x0000
R50
0x0000
R60
0x0000
R70
0x0000
PC0
0x0000
SP255
0x00FF
FLAGS0x00
ZCVN

ALU

Idle

Memory

PCSPWatched
Memory contents — rows indexed by base address, columns by hex offset
Address0123456789ABCDEF
0000000000000000000000000000000000
1000000000000000000000000000000000
2000000000000000000000000000000000
3000000000000000000000000000000000
4000000000000000000000000000000000
5000000000000000000000000000000000
6000000000000000000000000000000000
7000000000000000000000000000000000
8000000000000000000000000000000000
9000000000000000000000000000000000
A000000000000000000000000000000000
B000000000000000000000000000000000
C000000000000000000000000000000000
D000000000000000000000000000000000
E000000000000000000000000000000000
F000000000000000000000000000000000

Pipeline

Cycle 0
Fetch
Decode
Execute
Writeback
→
→
→
No instruction loaded

Guided 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

200ms

Registers

R00
0x0000
R10
0x0000
R20
0x0000
R30
0x0000
R40
0x0000
R50
0x0000
R60
0x0000
R70
0x0000
PC0
0x0000
SP255
0x00FF
FLAGS0x00
ZCVN

ALU

Idle

Memory

PCSPWatched
Memory contents — rows indexed by base address, columns by hex offset
Address0123456789ABCDEF
0000000000000000000000000000000000
1000000000000000000000000000000000
2000000000000000000000000000000000
3000000000000000000000000000000000
4000000000000000000000000000000000
5000000000000000000000000000000000
6000000000000000000000000000000000
7000000000000000000000000000000000
8000000000000000000000000000000000
9000000000000000000000000000000000
A000000000000000000000000000000000
B000000000000000000000000000000000
C000000000000000000000000000000000
D000000000000000000000000000000000
E000000000000000000000000000000000
F000000000000000000000000000000000

Pipeline

Cycle 0
Fetch
Decode
Execute
Writeback
→
→
→
No instruction loaded