Inside the CPU: Fetch, Decode, Execute
Meet the ALU, registers, control unit and clock, then trace a tiny program through the fetch-decode-execute cycle one instruction at a time.
Welcome back!
Step 1 of 15Learn
The world's fastest clerk
Imagine a clerk at a desk with a short list of numbered instructions and a wall of numbered mailboxes. Each instruction is simple: copy the number from mailbox 20 onto your notepad, add the number in mailbox 21, write the result into mailbox 22, if the result is zero, skip to instruction 9. The clerk reads one instruction, does it, moves to the next, and never stops.
That clerk is a CPU. The mailboxes are memory, each with a numbered address. The notepad is a set of registers. The instruction list is a program stored in memory. The real CPU differs mostly in speed: it can perform several billion of these steps per second.
The surprising thing is how little each instruction does. There is no instruction for show a video or check spelling. Every app you use is built from millions of tiny steps like add, compare, load, store and jump. Complexity comes from the program, not the processor.
Step 2 of 15Learn
The main parts
A processor core has a few key parts:
- Arithmetic logic unit (ALU): does the actual math and logic: addition, subtraction, comparison, AND, OR, shifting bits. It's built from the adders and gates you met earlier.
- Registers: a small set of very fast storage slots inside the CPU, typically holding 64 bits each on modern chips. Data must usually be in a register before the ALU can work on it.
- Program counter (PC): a special register holding the memory address of the next instruction.
- Instruction register: holds the instruction currently being carried out.
- Control unit: reads each instruction and sends signals that route data between registers, the ALU and memory, like a conductor directing an orchestra.
- Clock: a crystal-controlled signal that ticks billions of times per second, keeping every part in step.
A modern chip also contains cache memory, several cores (complete copies of all of the above) and often a graphics processor, but the core idea hasn't changed since the 1940s.
Step 3 of 15Practice · Match the pairs
Match each CPU part to its job.
Tap an answer, then tap the place where it belongs (or drag it there).
- ALU
- Program counter
- Control unit
- Registers
- Clock
Step 4 of 15Diagram
Inside one processor core
Tap a shape with to see more.
- CPU core
- ALU
Does the math and logic.
- Registers
A few very fast slots holding the values being worked on.
- Control unit
Decodes instructions and directs the other parts.
- Program counter
Holds the address of the next instruction.
- Cache
Fast memory keeping copies of recently used data.
Step 5 of 15Note
Step 6 of 15Learn
The fetch-decode-execute cycle
Every CPU repeats one loop for as long as it is powered on:
- Fetch: read the instruction at the address in the program counter from memory into the instruction register, then increase the program counter so it points to the next instruction.
- Decode: the control unit works out what the instruction means: which operation, which registers, which memory address.
- Execute: carry it out. The ALU might add two registers, or data might be loaded from memory or stored to it.
- Write back: save the result, usually into a register.
Then back to fetch. A jump (or branch) instruction is how programs make decisions and loops: instead of letting the program counter move to the next instruction, it overwrites the program counter with a different address. If the counter is not zero, jump back to the top is how a loop repeats.
This loop is the heartbeat of the whole machine. When people say a program is running, they mean the CPU is fetching, decoding and executing its instructions, one after another, billions of times per second.
Step 7 of 15Practice · Put in order
Put the steps of one instruction cycle in order.
Drag the items, or use the arrow buttons, to put them in order.
- Execute the operation in the ALU or memory
- Write the result back to a register
- Decode the instruction in the control unit
- Read the instruction at the address in the program counter
- Increase the program counter
Step 8 of 15Diagram
The instruction cycle
Tap a shape with to see more.
- Fetch
Read the instruction at the program counter; move the counter on.
- Decode
Work out the operation and its operands.
- Execute
The ALU calculates, or data moves, or a jump changes the program counter.
- Write back
Store the result in a register or memory.
Step 9 of 15Note
Step 10 of 15Practice · Diagram
The tiny program, one cycle at a time
Which instruction is the only one that uses the ALU?
Tap the shape that answers the question, then press Check.
- LOAD R1, [20]
R1 = 5
- LOAD R2, [21]
R2 = 7
- ADD R3, R1, R2
R3 = 12
- STORE R3, [22]
Address 22 now holds 12.
Step 11 of 15Note
Step 12 of 15Learn
Clock speed and what it means
The clock speed is measured in hertz (ticks per second). A 3 GHz processor ticks 3 billion times per second, so one cycle lasts 1 ÷ 3,000,000,000 seconds, about 0.33 nanoseconds. In that time, light travels only about 10 centimeters.
It's tempting to think a 4 GHz chip is always faster than a 3 GHz chip. Often it isn't. What matters is how much work gets done per second:
Instructions per second = clock speed × instructions per cycle (IPC) × number of cores in use
A newer design might complete 4 to 6 instructions per cycle on average using tricks you'll meet in the next lesson, while an older design manages 1 to 2. So a 3 GHz modern core can easily beat a 4 GHz older one.
Clock speeds stopped climbing quickly in the mid-2000s at around 3 to 5 GHz. Higher speeds made chips run too hot, because power use rises steeply with frequency and voltage. Since then, performance gains have come mainly from doing more per cycle and from adding cores.
Step 13 of 15Try the calculator
Clock speed calculator
- Length of one cycle
- 0.333 ns
- Instructions per second (billions)
- 12
Step 14 of 15Practice · Work it out
Processor X runs at 4 GHz and averages 1.5 instructions per cycle. Processor Y runs at 3 GHz and averages 3 instructions per cycle. Using one core each, how many times faster is Y than X?
Step 15 of 15Practice · Choose one
A program has a loop that runs 1,000 times. Which instruction makes the CPU go back to the start of the loop?
Lesson complete!