Written by: Damjan Lulakov. Originally published on the CodeChem blog.
I write code every day, and I still couldn't have explained what actually runs it. Then I read But How Do It Know? by J. Clark Scott, a short book that builds a whole computer on paper from one tiny part. Here's the path it takes, with C you can run along the way.
The only part you need: NAND
Picture a room with a ceiling light and two switches. Flip one: the light stays on. Flip the other: still on. The light only goes out when both switches are on.
That's a NAND gate ("Not AND"): two bits in, one bit out.
| A | B | Out |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
The book's big claim is that every other part of the computer is just NANDs wired together. You can check the first step in C:
#include <stdio.h>
#include <stdbool.h>
bool NAND(bool a, bool b) { return !(a && b); }
bool NOT(bool a) { return NAND(a, a); }
bool AND(bool a, bool b) { return NOT(NAND(a, b)); }
bool OR(bool a, bool b) { return NAND(NOT(a), NOT(b)); }
bool XOR(bool a, bool b) {
bool n = NAND(a, b);
return NAND(NAND(a, n), NAND(b, n));
}
int main(void) {
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
printf("%d %d | AND %d OR %d XOR %d\n",
a, b, AND(a, b), OR(a, b), XOR(a, b));
}
One function, and every other gate falls out of it.
From gates to memory
Wire four NANDs so the output loops back into the input, and you get something that remembers one bit. While a "set" wire is on, the output follows the input. When it turns off, the output keeps its last value.
Stack 8 of those and you have a byte. Add a way to switch its output on and off and you have a register.
The bus and RAM
Every register connects to the same 8 wires, the bus. Copying R1 into R4 is two moves: R1 puts its byte on the bus (enable), R4 grabs it (set). That's what mov r4, r1 is doing underneath.
RAM is 256 of those registers in a grid, picked by an address. An address is a byte and so is the content, which means one byte can hold the location of another. That's all a pointer is.
The ALU and the flags
The ALU takes two bytes and does one of eight things: add, shift left or right, NOT, AND, OR, XOR, or compare. There's no subtract or multiply in hardware. Programs build those from adds and shifts.
It also sets four flags: Carry, A larger, Equal and Zero. Keep those in mind, because they're about to become every if you've ever written.
Fetch, execute, jump
A clock ticks on and off, and a stepper turns those ticks into steps. Each step does one bus transfer.
The trick that makes it a computer: store instructions in RAM next to the data. Every cycle, the CPU fetches the byte at the address in its program counter, then does what that byte says. Then it repeats.
Two more instructions turn that into real programs:
- JUMP writes a new address into the program counter. Now you can loop.
- JUMP IF jumps only when a flag is on. Now you can decide.
Here's a C loop compiled with gcc -S -O0 -masm=intel:
.L2:
add DWORD PTR -4[rbp], 1 ; r0 += 1
cmp DWORD PTR -4[rbp], 5 ; compare r0 with 5
jne .L2 ; jump back if not equal
A compare that sets the flags, and a conditional jump. Every if, for and while ends up like this.
So how does it know?
The title comes from an old joke about a Thermos: "It keeps hot food hot and cold food cold." "But how do it know?"
The book's answer for computers is the same as for the Thermos: it doesn't know anything. A NAND gate reacts to electricity on its inputs. A billion of them wired together still just react, very fast.
Try it
- Compile the gates above, then build XOR a different way using only
NAND. - Paste a small loop into Compiler Explorer and find the compare and the jump.
- If you write code for a living, read the book. It's about 170 pages and you won't look at a CPU the same way again.
The full version on our blog goes step by step through the bus, RAM decoders, the clock, the complete instruction set and I/O, with all the diagrams.
What part of the stack below your code would you want explained next?

Top comments (0)