Play free

The machine

How a CPU works, from logic gates up

A CPU is not magic, and it is not even complicated in principle. It is a counter that points at a list of numbers, a decoder that turns each number into switch settings, and an adder with some friends. This page puts the pieces together, and then you can step through a tiny computer one beat at a time.

4 min read · Updated

The parts list

Nearly every processor, from the 1971 Intel 4004 to the chip in your phone, is built from the same handful of blocks:

addresscontrolPCnext addressMemoryprogram + dataInstruction reg.current instructionDecoderbits → controlRegistersALU+ − & | <<Flags
A simple CPU at block level. The program counter addresses memory, the fetched instruction drives the decoder, and the decoder steers registers, ALU and memory.

The cycle: fetch, decode, execute

A CPU repeats three steps for as long as it is powered on:

  1. Fetch. Read the word at the address in the PC into the instruction register, and advance the PC to the next instruction.
  2. Decode. Split the instruction into fields. Typically an opcode says what to do, and operand fields say what to do it to. The decoder turns those bits into control signals.
  3. Execute. The control signals set up the data path. Values flow from registers or memory through the ALU, and the result is written back. A jump instruction does its work by loading a new value into the PC, so the next fetch comes from somewhere else.

That is all there is to it. Loops, function calls, operating systems and games are all built from this cycle running billions of times a second. Try it on the toy computer below. It has one register (the accumulator), 16 bytes of memory, and eight instructions with a 4-bit opcode and a 4-bit address. The program adds 5 + 4 + 3 + 2 + 1 with a loop.

    ready
    PC0
    IR—
    ACC0
    OUT—

    Press Step to fetch the first instruction.

    Step through fetch, decode and execute. The highlighted line is where the program counter points. Memory lines 13–15 hold data: the constant 1, the loop counter n, and the running total.

    Programs are just numbers

    The toy machine’s ADD 15 is stored as the byte 0010 1111: opcode 2, address 15. Memory doesn’t know whether a word is an instruction or a number. It only matters how the CPU uses it. That is the stored-program idea, first run on the Manchester Baby in 1948. It is what makes computers general-purpose: to change what the machine does, you change numbers in memory, not wires.

    Writing those numbers by hand gets old quickly, so programmers write assembly language (ADD 15), and a program called an assembler translates it into machine code. Which instructions exist and how they are encoded is the machine’s instruction set architecture, the contract between hardware and software. See designing your own instruction set.

    Single-cycle, multi-cycle, pipelined

    The toy machine spends one step on each phase, which is called a multi-cycle design. A single-cycle CPU does all three phases in one long clock tick, which is simple but slow, because the clock period must cover the slowest instruction. Real processors pipeline: while one instruction executes, the next is being decoded and the one after that fetched, like an assembly line. Pipelining brings its own puzzles, called hazards. What if an instruction needs a result that hasn’t been written yet? What should be fetched after a branch whose outcome isn’t known yet? Forwarding, stalls and branch prediction are the answers, and modern CPUs go further still with out-of-order execution and several pipelines side by side.

    Von Neumann and Harvard

    A von Neumann machine keeps instructions and data in one memory and reaches it over one path. It is flexible, since a program can even load another program, but fetching and data access compete for that one path. A Harvard machine has separate instruction and data memories, so it can do both at once. Many microcontrollers are Harvard machines. Most big CPUs are von Neumann at the level of main memory but split instruction and data caches close to the core, which gives them some of both.

    How many gates is a CPU?

    Fewer than you might think, for a simple one. A small 8-bit CPU can be built from a few thousand NAND-equivalents, and the 6502 that powered the Apple II had about 3,500 transistors. A modern desktop processor has tens of billions of transistors. The ideas are the same, repeated, widened and sped up: bigger registers, more of them, caches, many pipelines and many cores.