Mansori

Inside a CPU: Fetch, Decode, Execute

About this lesson, for parents and teachers

A CPU is not smart. It does one dumb thing, very fast: grab an instruction, figure out what it means, do it, repeat. Step through that cycle yourself and see real programs come alive.

Ages 12–14 · about 9 minutes · builds on Build an Adder From Logic Gates

Try this

  1. Press Step again and again. Each press runs one phase: FETCH, DECODE or EXECUTE. Watch PC, IR and ACC change.
  2. In program 2, the answer comes out wrong. Tap the buggy instruction to change it, then press Run.
  3. In program 3 there is no multiply instruction. Tap a data cell so the loop computes 15.
  4. Drag the speed slider up to watch the loop race around.

How it works

This CPU has 16 memory cells, numbered 0 to 15. Some hold instructions like LOAD 13, others hold plain numbers. The CPU itself has a few tiny storage slots called registers: the PC (program counter) holds the address of the next instruction, the IR (instruction register) holds the instruction being worked on, and ACC (the accumulator) holds the number being calculated.

Every instruction goes through the same three steps. Fetch: copy the instruction at address PC into IR, then add 1 to PC. Decode: work out what the instruction means, for example ADD 14 means add the number in memory 14 to ACC. Execute: do it. Then the cycle starts again. A real CPU repeats this billions of times per second.

Our instruction set has seven instructions. LOAD a copies memory a into ACC. ADD a and SUB a add or subtract memory a. STORE a copies ACC into memory a. JMP a sets PC to a, so the program jumps. JZ a jumps only if ACC is zero. HALT stops. With just these, you can build loops, decisions and even multiplication.

Notice that the program and its data live in the same memory. That idea, called the stored-program or von Neumann design, is why changing one cell from ADD to SUB changes what the program does. It is also why a CPU that accidentally fetches a plain number gets confused. Programming languages like Python are translated down into instructions like these before your CPU can run them.

Where engineers use this

Phones use ARM processors and most laptops use x86 or ARM chips. Their instruction sets have hundreds of instructions, but each one still goes through fetch, decode and execute, overlapped in a pipeline so several are in flight at once.

Words to know

Program counter (PC)
A register that holds the memory address of the next instruction to fetch.
Register
A tiny, very fast storage slot inside the CPU, like ACC or IR.
Instruction set
The list of commands a CPU understands, such as LOAD, ADD and JMP.
Fetch-decode-execute cycle
The loop every CPU repeats: get an instruction, work out what it means, then do it.

Talk about it

Goal: Step a tiny CPU through a program and watch memory, registers and the program counter change.

  • Why does the PC go up by 1 after every fetch? What does JMP do to that pattern?
  • If there is no multiply instruction, how did program 3 still multiply?
  • What would happen if program 3's counter started at 0? Try to predict before testing.

Without a screen: Play 'human CPU': write a short program on 16 numbered sticky notes (LOAD, ADD, STORE, HALT plus some numbers). One person is the PC and points at notes, one fetches and reads the instruction aloud, one holds a whiteboard as ACC and does the maths. Run it, then edit a note and run again.

Curriculum links: CSTA K-12 2-CS-02 (hardware and software components), 3A-CS-02 (levels of abstraction), 2-AP-12 (loops and conditionals); AP Computer Science Principles Big Idea 4 (computer systems)

Questions

What is the fetch decode execute cycle?
It is the loop a CPU runs for every instruction. It fetches the next instruction from memory, decodes what it means, and executes it, then moves on to the next instruction.

What does the program counter do?
The program counter holds the address of the next instruction. It normally goes up by one after each fetch, and jump instructions change it so the program can loop or skip ahead.

Is this how a real CPU works?
Yes, in a simplified form. Real CPUs have more registers, many more instructions and tricks like pipelining and caches, but the same fetch, decode and execute cycle is at the heart of every one.