register (processor)

term

programming

Kept in this browser · My desk

A small, fast storage location inside the processor, named directly by instructions; the number and width of registers, and which ones are special, are part of an instruction set architecture.

Registers are where assembly does its work, and where a calling convention puts arguments and results. Which register carries what is not decided by the processor alone: on the same x86-64 chip, Linux's System V convention passes the first integer argument in RDI and Microsoft's in RCX. Some architectures also give a register a fixed value: RISC-V's x0 always reads as zero.

What it is. A register is a storage location inside the processor that instructions name directly, by a number encoded in the instruction itself. Registers are few because each register number costs bits in every instruction that uses it: x86's ModR/M byte has 3-bit register fields, extended by one bit from the REX prefix to reach 16 registers, while RISC-V's 32-bit instructions carry 5-bit register fields for 32 registers. Everything a program computes passes through them; memory holds what does not fit.

Three modern register files. In 64-bit mode an x86-64 processor has 16 general-purpose registers; the ModR/M byte has 3-bit fields (RAX 000 to RDI 111) and the REX prefix supplies the extra bit for R8 to R15. A 32-bit operation zero-extends its result into the full 64-bit register, which is why xor eax, eax clears RAX. AArch64 has 31 general-purpose registers X0 to X30, X30 being the procedure link register, and register number 31 means either the stack pointer or a zero register depending on the instruction. RISC-V has 32 registers x0 to x31, and x0 "is hardwired with all bits equal to 0".

Eight-bit ancestors. The Z80 has two sets of six general-purpose registers usable as 8-bit registers or 16-bit pairs, plus accumulators and flag registers; the 6502 has an accumulator and two index registers, X and Y, and can add only memory to the accumulator, so a loop counter must pass through memory to be summed.

Beyond the general-purpose set. A processor has more registers than the ones arithmetic names. The program counter (RIP on x86-64, the PC on Arm and RISC-V) holds the address of the next instruction, and the stack pointer the top of the stack, which is why both are the first targets of an exploit. Vector registers hold several values at once: Microsoft's x64 documentation notes that all floating-point operations use the 16 XMM registers and that the old x87 register stack is unused. Control and model-specific registers configure the processor itself; Intel's IA32_EFER, for example, holds the NXE bit (bit 11) that turns on execute-disable page protection, and only the operating system may write it.

Who decides what lives in a register. In compiled code the programmer rarely chooses: the compiler's register allocator does, keeping the busiest values in registers and spilling the rest to the stack. OpenJDK's HotSpot glossary, for example, describes its fast C1 compiler as using "a linear scan register allocation". Hand-written assembly makes the choice explicit, which is where the calling conventions below become rules rather than habits.

Flags and conventions. Status flags such as x86's ZF (bit 6 of EFLAGS, set when a result is zero) live in a flags register; RISC-V has none for integer comparisons. Calling conventions assign roles: System V uses RDI, RSI, RDX, RCX, r8 and r9 for integer arguments and RAX for results; the Arm 64-bit procedure call standard (AAPCS64) uses r0-r7 for both; the RISC-V convention uses a0-a7 (x10-x17), with ra (x1) holding the return address.

Who saves what. A convention also decides which registers a called function may destroy. Microsoft's x64 application binary interface (ABI) is "a four-register, fast-call calling convention": integer arguments go in RCX, RDX, R8 and R9, floating-point arguments in XMM0 to XMM3, and the caller reserves a shadow store on the stack where the callee may save those four; they, together with RAX, R10, R11, XMM4 and XMM5, are volatile, while the others must be preserved. The RISC-V psABI (processor-specific ABI) tabulates the same decision for every register: x2 (sp) is preserved across calls, s0 to s11 (x8, x9 and x18 to x27) are callee-saved, a0 to a7 and the temporaries t0 to t6 are not, and gp (x3) and tp (x4) are "Unallocatable". Assembly that breaks these rules works until a caller relies on a register it thought was safe.

A register is state to protect. On every context switch the operating system saves one program's registers and loads another's, so register contents are as private as memory. Two flaws showed what happens when that boundary leaks. Tavis Ormandy's Zenbleed (Common Vulnerabilities and Exposures identifier CVE-2023-20593) began with the vector registers: x86-64 processors have 128-bit XMM registers extended to 256-bit YMM; on every Zen 2 class processor a mispredicted vzeroupper could be recovered incorrectly, and his variant leaked "about 30 kb per core, per second", fast enough to watch keys and passwords; he reported it to AMD on 15 May 2023, and the National Vulnerability Database (NVD) published it on 24 July 2023. Earlier, CVE-2018-3665 (published 21 June 2018) described how software using the lazy floating-point state restore technique on Intel Core processors "may potentially allow a local process to infer data from another process" through a speculative execution side channel.

Neighbours. Registers are part of the instruction set architecture, the operands of opcodes, the working space of assembly language and the subject of calling conventions in the application binary interface. Brazilian practice says registrador.

Also known as: register, CPU register, general-purpose register, registrador, registrador de uso geral, accumulator, zero register

Sources

← Back to R Back to the glossary