From disk to memory
A program is a file kept on the disk, here an SSD (solid-state drive). The disk keeps files even when the power is off, but it is slow, so the CPU does not run programs straight from it.
Inside the file, the instructions are just numbers, called machine code. A CPU only understands its own instruction set: a short list of numbered commands. This made-up CPU knows three: 1 stores a number, 3 adds, 0 stops.
To run it, the operating system copies the program from the disk into memory (RAM). Memory is a row of numbered boxes; a box’s number is its address. The four instructions land at addresses 0, 1, 2 and 3.
One cycle
The CPU has registers: a few tiny, very fast boxes inside the chip. One, the program counter (PC), holds the address of the next instruction to run. The program starts at the top, so the PC holds 0.
Every cycle starts with fetch. The CPU reads the box whose address is in the PC, box 0, and copies its number, 112, into the instruction register (IR), the register that holds the instruction being worked on.
Next, decode. The CPU splits 112 into its digits and looks them up in its instruction set: 1 means store a number, the next 1 means register r1, and 2 is the number to store.
Then execute: the CPU does it, and 2 goes into register r1. The program counter moves on by one, to 1, so the next cycle fetches the next instruction. Fetch, decode, execute, repeat.
Running the rest
The next cycle works the same way. Fetch 123 from address 1. Decode: 1 means store, into register r2, the number 3. Execute: r2 now holds 3, and the PC moves on to 2.
Fetch 312 from address 2. Decode: 3 means add, r1 plus r2, answer into r1. Execute: the CPU works out 2 + 3 = 5, so r1 now holds 5. The PC moves on to 3.
Fetch 000 from address 3. Decode: 0 means stop, so the CPU halts with the answer, 5, in register r1. Every program, however big, is run like this: one small instruction at a time.
A clock ticks to keep the CPU in step. The Raspberry Pi 5’s CPU ticks 2.4 billion times a second (2.4 GHz, gigahertz), so a CPU like it works through billions of instructions every second.
What happens
;; step 1/11 · on-disk
disk stores files, even with the power off
program file: 4 instructions
memory (empty: the program is not loaded yet)
CPU waiting
The instruction set here is made up for teaching, with instructions written as decimal digits. Real CPUs use binary codes. On an x86-64 PC, for example, the bytes B8 02 00 00 00 mean "put 2 in register eax" and 01 D8 means "add ebx to eax" (Intel Software Developer’s Manual, Vol. 2). One instruction does not always take exactly one tick. Modern CPUs overlap several instructions at once (pipelining) and have several cores, so they can finish more than one instruction per tick, or need several ticks for a slow one.