All episodes · From One Switch to a CPU · Episode 10

How a CPU Runs a Program: Fetch, Decode, Execute

Pressing play loads the video from YouTube.

The instructions are in memory. Now we watch a processor run them, one step at a time.
We lay out the datapath (program counter, instruction register, decoder, registers, ALU), step through fetch, decode and execute for a small program, then add jumps, so a countdown loop runs until it hits zero.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

Last time, we wrote a program: four numbers sitting in memory. But numbers don't do anything by themselves. Something has to read them, one by one, and carry them out. That something is the CPU. Today, we watch it run a program, step by step.

Here are the parts. Memory holds the program. The program counter, the counter from episode seven, holds the address of the next instruction. The instruction register holds the instruction being worked on. A decoder reads its fields. Then there are the four registers, and the ALU from episode three, which does the actual math. All of it is joined by wires called buses. Together, this is the datapath.

Every instruction goes through the same three steps. Step one: fetch. The program counter sends its address, zero, to memory. Memory sends back the instruction stored there, into the instruction register. And the program counter adds one, so it already points at the next instruction.

Step two: decode. The decoder splits the instruction into its fields. Opcode zero zero zero one: load a number. Register field: R1. Number field: five.

Step three: execute. The number five goes straight into register one.

Then it starts over: fetch, decode, execute. Load three into R2. Add R1 and R2 in the ALU: eight. Store R1 into memory cell fifteen. The answer is in memory. Fetch, decode, execute, over and over. A modern processor does this billions of times every second.

So far, the program counter only counts up. But a program that only runs straight down can't repeat anything. That needs a jump. A jump instruction puts a new address into the program counter. Instead of the next line, the CPU continues from there. A conditional jump only jumps if something is true, like the zero flag from episode three. That's how a program makes a decision.

Here's a countdown. Put three in R1, and one in R2. Subtract: R1 is two. Not zero, so don't jump to the end. Jump back to the subtraction. R1 is one. Again. R1 is zero, the zero flag lights up, and this time the conditional jump goes to the end. Every loop and every if, in every program you've ever used, comes down to jumps like these.

So: fetch, decode, execute, repeat. And jumps let the program counter move around. But who flips all these switches at the right moment, telling each register when to load and the ALU what to do? Next time: the control unit. See you there.

Next episode: the control unit, the part that flips all the switches.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal