Figure: Chip, a cartoon CPU, adds two numbers. Its recipe card has five lines: LOAD A, [0]; LOAD B, [1]; ADD A, B; STORE A, [2]; HALT. For each line Chip fetches the instruction (a card flies from the recipe to its face), decodes it (says what it means in plain words) and executes it: number tokens fly from box 0 and box 1 into hands A and B, into the belly calculator, back to hand A, and into box 2. With 2 and 3 in the boxes, box 2 ends with 5. The animation repeats with new numbers.
Figure: a scroll-driven tour of Chip. 1, Chip is the CPU. 2, its three hands are registers. 3, the shelf of numbered boxes is memory. 4, the recipe of tiny steps is the program, written in assembly language. 5, the finger is the program counter. 6, the belly calculator is the ALU and the antenna is the zero flag. 7, Chip runs the add program from start to finish.
Every phone, laptop and game console has a chip like this inside: the CPU. Chip is incredibly fast, but it can only follow tiny, simple steps.
Scroll down to take the tour.Anything Chip wants to work on has to be held in a hand first. It can't add numbers that are sitting somewhere else.
Real name: registers. An x86-64 CPU has 16; ARM64 has 31.Next to Chip is a shelf of numbered boxes. There is lots of room, but walking to the shelf is slow compared with using a hand.
Real name: memory (RAM). Billions of boxes, each with a number called its address.Chip follows a recipe, one short line at a time: "copy box 0 into hand A", "add hand B to hand A"…
Real name: the program, written in assembly language.After each step the finger moves down one line, unless a line says "jump", which sends it somewhere else.
Real name: the program counter (rip on x86-64).Chip's belly adds and subtracts. Whenever an answer comes out as zero, its antenna lights up. Jumps can check that light, and that's how loops work.
Real names: the ALU and the zero flag.Chip runs the whole recipe: two loads, an add and a store. Then it's your turn to be the CPU.
Every instruction is fetch → decode → execute.Figure: a game. The reader plays the CPU and carries out the add program by tapping: box 0 then hand A (LOAD A, [0]), box 1 then hand B (LOAD B, [1]), the belly calculator (ADD A, B), hand A then box 2 (STORE A, [2]), and Chip’s face (HALT). At the end the game compares the reader’s time with a 3 GHz chip, which needs about 1.7 nanoseconds for 5 instructions at one instruction per clock tick.
Figure: Chip multiplies 3 × 4 with no multiply instruction. Program: MOV A, 0; MOV B, 3; MOV C, 4; then the loop ADD A, B; DEC C; JNZ 3 repeats 4 times. Each DEC C counts down; when C reaches 0 the antenna (zero flag) lights, JNZ stops jumping back, STORE A, [0] writes 12, and HALT. 17 instructions in total, with 3 jumps back. Speeds: snail, rabbit and lightning.
Figure: 16 switches, two bytes. Starting state 00000001 11011000, which is hex 01 D8 and decodes to the real x86-64 instruction add eax, ebx (eax = eax + ebx). Presets: 29 D8 is sub eax, ebx; 89 D8 is mov eax, ebx; 31 C0 is xor eax, eax; 01 C3 is add ebx, eax. Flipping any switch decodes the new bytes live.
Figure: the C function int add(int a, int b) { return a + b; } compiled for two CPU families. x86-64 with GCC 13.3 at -O2: endbr64; lea eax, [rdi+rsi]; ret. It has 16 general-purpose registers and instructions 1 to 15 bytes long. ARM64 with Clang 18 at -O2: add w0, w1, w0; ret. It has 31 general-purpose registers and every instruction is 4 bytes.
int add(int a, int b) { return a + b; }.Figure: four places assembly still shows up. Reading what the compiler made, to check why a hot loop is slow. Debugging crashes without source code. The first code a computer runs: bootloaders, kernels and tiny microcontrollers. The fastest inner loops: video codecs and crypto libraries.