Skip to content

Computer Architecture

The fetch-decode-execute cycle explained

A processor runs a program by repeating three steps for every instruction: fetch the instruction from memory, decode it to work out what it means, and execute it. Then it fetches the next one. This fetch-decode-execute cycle is how every CPU, from an Arduino's chip to a laptop's, turns a list of numbers in memory into actions.

Fetch

The program counter holds the address of the next instruction. The processor sends that address to memory, copies the instruction that comes back into the instruction register, and moves the program counter on to the following address. The instruction register holds the instruction steady while the rest of the cycle works on it.

Decode

The instruction is just a binary number. Part of it is the opcode, which says what to do; the rest says what to do it to, a register, a number or an address. A decoder turns the opcode into control lines, exactly one lit for each kind of instruction: this one means add, that one means load, another means jump.

Execute

The lit control line wakes up its part of the processor. For an ADD, the ALU adds and the result goes back into a register. For a LOAD, a value comes from memory. For a JUMP, a new address is loaded into the program counter, so the next fetch comes from somewhere else.

Live experiment

Three cards, three steps each

A little processor runs three cards: ADD 1, ADD 2, ADD 3. Two flip-flops step through FETCH, DECODE and EXECUTE, one per tick, and each lights its part: fetch copies the card and moves the counter on, decode reads it, execute adds it to the total.

Try it yourself: free lesson

“Fetch, decode, execute” is lesson 6 of 6 in Fetch Fjord, Computer Architecture World 2. Free with an account. Lessons unlock in order, so you’ll do the 12 before it first.

Who keeps the steps in order?

A small state machine does: a couple of flip-flops that step through FETCH, DECODE and EXECUTE, one per clock tick, then back to FETCH. A decoder on those flip-flops lights one step's line at a time, and each line enables its step's parts. Three instructions at three steps each take nine ticks.

Pipelining

Three ticks per instruction leaves most of the processor idle most of the time. A pipelined processor overlaps the steps like an assembly line: while one instruction executes, the next is decoded and the one after that is fetched. Each instruction still passes through all three stages, but once the pipe is full, one finishes every tick.

Modern processors split the work into 10 to 20 stages and run several pipelines side by side, finishing several instructions per tick. A jump spoils the flow, since the instructions fetched behind it were the wrong ones, so processors guess which way each branch will go. Underneath all of it is the same loop: fetch, decode, execute.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Fetch, decode, execute” is lesson 6 of 6 in Fetch Fjord, Computer Architecture World 2. Free with an account. Lessons unlock in order, so you’ll do the 12 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The step is 10, EXECUTE. After the next tick it's…
    Show the answer

    Answer: 00, FETCH: the cycle goes round. D0 = NOR(Q0, Q1) = NOR(0, 1) = 0 and D1 = Q0 = 0: back to 00, and the next instruction is fetched.

    • Why not “11”? 11 isn't one of the step machine's states. After EXECUTE, it goes back to FETCH.
    • Why not “10 again”? Staying in EXECUTE would run the same instruction again. The cycle goes round to fetch the next one.
    • Why not “01”? 01 is DECODE, which comes after FETCH, not straight after EXECUTE.
  2. Question 2 of 3"One hot" means…
    Show the answer

    Answer: Exactly one output line is 1 at a time. A decoder's outputs are never two at once: each opcode wakes exactly one part of the processor.

    • Why not “The chip is warm”? It's nothing to do with temperature: hot here means active, the one line that's 1.
    • Why not “All lines are 1”? All lines at 1 would mean every part of the processor acting at once, the opposite of one-hot.
    • Why not “One line is always 0”? One-hot is about the single line that is on, not a line that's always off.
  3. Question 3 of 3An accumulator…
    Show the answer

    Answer: Adds its input to its own value on command. An adder in front of a register, its output fed back.

    • Why not “Resets every tick”? It keeps its total between instructions; resetting every tick would throw the total away.
    • Why not “Only counts by one”? Counting by one is a counter's job. An accumulator adds whatever its input is.
    • Why not “Holds a constant”? It holds whatever its last addition gave, which changes with each ADD.
Keep practising: free lesson

“Fetch, decode, execute” is lesson 6 of 6 in Fetch Fjord, Computer Architecture World 2. Free with an account. Lessons unlock in order, so you’ll do the 12 before it first.

Questions people ask

What are the three stages of the fetch-decode-execute cycle?

Fetch reads the instruction at the program counter's address into the instruction register and moves the counter on. Decode works out what the instruction means and which parts it needs. Execute carries it out, such as an addition in the ALU, and stores the result.

What registers are used in the fetch-decode-execute cycle?

The program counter holds the next instruction's address, the memory address register and memory data register carry it to and from memory, the instruction register holds the instruction being decoded, and an accumulator or general registers hold the data.

How does pipelining speed up the cycle?

It overlaps the stages: while one instruction executes, the next is being decoded and the one after that fetched. Each instruction still takes three steps, but once the pipeline is full, one finishes every clock tick.

Every topic