Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 40 ยท Data, Numbers and Registers

Registers: The CPU's Working Memory

Learn what CPU registers are, the difference between general-purpose and special-purpose registers, the program counter, stack pointer and flags, with a runnable toy register file.

Registers are the CPU's own tiny, extremely fast storage cells. Almost every calculation a processor performs happens on values that are sitting in registers. When you add two numbers, you do not add two memory locations; you load them into registers (or, on x86, let the instruction do that for you), add, and put the result somewhere. If assembly language has a central character, it is the register.

In this lesson you will learn what registers are, why there are so few of them, the difference between general-purpose and special-purpose registers, and what the program counter, stack pointer and flags register do. You will then explore a register file by running toy programs and comparing a loop that keeps its working value in a register with one that keeps it in memory.

What a register is

A register is a fixed-size group of bits inside the CPU, with a name. On a 64-bit processor most registers are 64 bits wide. The CPU wires them directly to its arithmetic unit, so reading or writing one takes a fraction of a clock cycle. Main memory sits far away on the other side of the chip, and it takes many cycles to reach unless the data is in a cache.

flowchart TD
    R["Registers<br/>under 1 cycle, bytes"] --> L1["L1 cache<br/>about 4 cycles, tens of KB"]
    L1 --> L2["L2 cache<br/>roughly 10 to 15 cycles, hundreds of KB to MB"]
    L2 --> L3["L3 cache<br/>roughly 40 cycles, many MB"]
    L3 --> RAM["Main memory<br/>around 200 cycles or more, GB"]

Storage gets bigger and slower as you move away from the CPU core. The numbers are rough and differ between chips.

The price of speed is size. A CPU has only a few dozen registers that your program can name, compared with billions of bytes of memory. Programmers and compilers therefore juggle: keep the values being used right now in registers, and move others out to memory (the stack) when registers run out. Moving a value to memory to free a register is called spilling.

The two families of registers

Registers fall into two groups:

General-purpose registers hold whatever the program wants: numbers, addresses, counters, characters. Almost any arithmetic or logic instruction can use them. How many are there?

Architecture General-purpose registers Names
x86-64 16 (64-bit) rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp, r8 to r15
ARM64 31 (64-bit) x0 to x30, with 32-bit views w0 to w30
RISC-V (RV64I) 31 plus a constant zero x1 to x31, plus x0 that always reads as 0
Toy CPU 8 (32-bit) r0 to r7

Although they are called "general", operating systems and compilers give many of them agreed roles through a calling convention: which registers carry function arguments, which carry results, and which must be preserved across calls. The hardware does not enforce those roles, except in a few cases where an instruction uses a specific register on its own. You will find the details in Calling Conventions.

Special-purpose registers have a dedicated job that the CPU itself relies on:

Role x86-64 ARM64 Toy CPU
Program counter, address of the next instruction rip pc (internal)
Stack pointer, top of the call stack rsp sp sp
Flags, status bits from the last operation rflags NZCV Z S C O
Floating-point and vector registers xmm0 to xmm15 and wider v0 to v31 none

Some of these registers cannot be named like the others. On x86-64 you cannot write mov rax, rip directly (you read the program counter through lea rax, [rip+0] or by calling a function), and on ARM64 you cannot use mov x0, pc. The program counter changes through jumps, calls and returns. The flags are updated by arithmetic and comparison instructions and read by conditional jumps.

The program counter

You met the program counter in How a CPU Runs Instructions. It always points at the next instruction to fetch. Because jumps, calls and returns work by changing it, whoever controls its value controls the program. That is why security problems such as overwriting a return address are so dangerous: they let an attacker pick the next instruction.

The stack pointer

The stack pointer holds the address of the top of the stack, an area of memory used for function calls and temporary values. On x86-64 and ARM64 the stack grows towards lower addresses, so push subtracts from the stack pointer and pop adds to it. Programs also use rsp or sp as the base for local variables. The next chapters spend a lot of time on it, starting with The Stack.

The flags register

Arithmetic and comparison instructions leave small status bits behind. A conditional jump looks at those bits to decide whether to jump. The most important ones are:

Flag x86 name Set when
Zero ZF the result was zero
Sign (negative) SF the top bit of the result was 1
Carry CF an unsigned addition carried out, or a subtraction borrowed
Overflow OF a signed result did not fit and has the wrong sign

ARM64 calls its four flags N, Z, C and V and only sets them when you ask for it, with instructions like cmp or adds (the "s" means "set flags"). On x86 most arithmetic instructions set flags every time. Flags: ZF, SF, CF, OF and How They Are Set is a whole lesson on them.

Exploring the toy register file

The toy CPU has eight general-purpose registers r0 to r7, the special register sp, and four flags. The interpreter below returns its whole register file after a run, and the helper dump prints it in one line. The first program swaps two values the obvious way, using a spare register as a temporary. The second swaps them using three xor instructions, with no temporary at all, a well-known register trick: a ^= b; b ^= a; a ^= b.

JS Swap values between registers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In both runs r0 and r1 end up exchanged (9 and 7). The first leaves a copy in r2; the second does not touch it. Note that every register not mentioned stays 0, and sp stays at 1024, the top of memory, because nothing used the stack. Real programs use the xor swap rarely, since a compiler will pick a good instruction sequence for you, but it shows that registers are plain places that instructions read and write.

Watching sp move

Now an experiment with the special register. push stores a value and subtracts 4 from sp; pop reads a value and adds 4. The program records sp in different registers at different moments.

JS The stack pointer moves with push and pop

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The stack pointer moved 8 bytes down for two 4-byte pushes, the values came back in reverse order, and sp ended where it began. Real 64-bit code moves rsp by 8 bytes per push.

Why compilers fight for registers

Registers are fast, but is it really that important to use them? Compare two toy programs that both add the numbers 1 to 100. The first keeps its running total in register r0. The second keeps the total in memory at address 200, loading and storing it on every pass of the loop, the way unoptimized compiler output stores every variable on the stack. The helper counts how many executed instructions touched memory and applies a made-up cost model in which every memory access costs 4 extra cycles. The numbers are illustrative only: real CPUs overlap work and cache cleverly, and the gap depends on the chip. But the direction of the difference is real.

JS Register loop versus memory loop

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Both programs print 5050, but the memory version executes 50% more instructions, touches memory 202 times and has more than three times the modeled cost. That is the reason optimizing compilers keep hot values (loop counters, totals, pointers) in registers whenever they can, and why code compiled with -O0 is so much slower than -O2.

Note: A real CPU has tricks that soften this gap, such as store-to-load forwarding and caches, so a memory-based loop is not always three times slower. Registers are still always the cheapest storage, and instructions that avoid memory are easier for the CPU to run in parallel.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does a CPU have only a few dozen registers instead of millions?

    Registers are wired directly into the CPU core. Making many of them would cost space and slow the chip down, so memory and caches provide the bulk storage.
  2. 2. Which register holds the address of the next instruction?

    The program counter (rip on x86-64, pc on ARM64) holds the address of the next instruction to fetch. Jumps and calls work by changing it.
  3. 3. After push 11 and push 22 on the toy CPU, which value does the first pop return?

    The stack is last-in, first-out, so pop returns the most recently pushed value, 22.
  4. 4. What does it mean to spill a register?

    When the compiler runs out of registers it stores a value on the stack and reloads it later. That store-and-reload is called spilling.
  5. 5. Which instruction style updates the ARM64 flags?

    ARM64 updates N, Z, C and V only for flag-setting forms such as cmp, cmn, tst and the S-suffixed arithmetic instructions like adds and subs.

Summary

  • Registers are tiny, very fast storage cells inside the CPU; all computation is done on them.
  • x86-64 has 16 general-purpose 64-bit registers, ARM64 has 31, the toy CPU has 8.
  • Special-purpose registers include the program counter (rip/pc), the stack pointer (rsp/sp) and the flags (rflags/NZCV).
  • Flags (zero, sign, carry, overflow) record facts about the last operation for conditional jumps to read.
  • Calling conventions give general-purpose registers agreed roles, but the hardware mostly does not.
  • When registers run out the compiler spills values to the stack; keeping hot values in registers is the most important optimization.

Next: The x86-64 Register Set, where each register gets its real name.