CPU registers
A look inside the processor at the few slots it computes with: the general-purpose registers, the program counter, the flags and the stack pointer, traced through a five-line program.
In 30 seconds
Registers are a handful of storage slots built into the processor itself. Each holds one number, and they are the fastest storage a computer has, far quicker to reach than main memory (RAM).
General-purpose registers hold whatever the program is working on. Special ones have fixed jobs: the program counter holds the address of the next instruction, the stack pointer marks the top of the stack, and the flags record facts about the last result.
There are only a few dozen, because every extra register costs instruction bits and speed.
CPU registers, step by step
The instruction set here is invented for clarity. Real chips also keep cache memory between the registers and RAM, and have further registers for decimals, vectors and the operating system.
- Step 1: Storage inside the CPU. A processor cannot calculate on numbers that sit far away. It keeps a handful of slots, called registers, built into its own circuitry. Each holds one number, 64 bits wide on a modern chip. Main memory (RAM) is a separate, much larger store outside the processor.
- Step 2: Why they are the fastest. A register sits right beside the circuits that do the arithmetic, so its value is ready within one tick of the processor’s clock, well under a nanosecond. Fetching a value from RAM can take around 100 nanoseconds, hundreds of ticks. So the processor works on registers and visits memory only when it must.
- Step 3: General-purpose registers. General-purpose registers are the processor’s scratch space: a program may put any value in them. Here two instructions place 5 and 7 in R1 and R2, and a third adds them into R3. The whole sum happens inside the processor, with no trip to memory.
- Step 4: The program counter. The program counter (PC) is a special register with one job: it holds the memory address of the next instruction. Each time an instruction is fetched the PC moves on, here by 4 because each instruction is 4 bytes long. That is how the processor keeps its place in a program.
- Step 5: The flags register. CMP compares by subtracting and throws the answer away, keeping only facts about it in the flags register, one bit each. 12 − 12 is zero, so the zero flag Z becomes 1. The next instruction, JEQ (jump if equal), reads Z and sets the PC to 132. Every if and every loop is built from this pair.
- Step 6: The stack pointer. The stack is a region of RAM for temporary values such as saved registers and return addresses. The stack pointer (SP) holds the address of its top. A push moves SP down by 8 bytes, the size of one register, and stores the value there. A pop does the reverse.
- Step 7: Why there are so few. Each instruction has to name its registers in a few bits: 5 bits can pick one of only 32. More registers would also mean longer wires and slower selection, and more to save whenever the processor switches programs. So real chips settle on 16 general-purpose registers (x86-64) or 31 (ARM64).
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?