Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 40 ยท Data, Numbers and Registers

The x86-64 Register Set

Learn the sixteen x86-64 general-purpose registers, their 64, 32, 16 and 8-bit names, the zero-extension rule for 32-bit writes, rip, rflags and xmm.

The previous lesson explained what registers are in general. This lesson is about one specific family: the registers of an x86-64 processor, the kind inside most laptops, desktops and cloud servers. You will meet every name you will see in compiler output (rax, edi, r9d, sil, xmm0) and learn the single most surprising rule of the architecture: writing to a 32-bit register wipes the top half of the 64-bit one.

Reading real assembly gets much easier once these names stop looking like noise. A handful of them have jobs that are fixed by the hardware or by convention, and the rest are interchangeable workers. After this lesson you can look at mov eax, edi and say exactly which bits move and which bits are cleared.

The sixteen general-purpose registers

x86-64 has sixteen 64-bit general-purpose registers. Eight of them carry names inherited from the 16-bit 8086 of 1978, and eight were added with the 64-bit extension and are simply numbered.

64-bit 32-bit 16-bit 8-bit low Traditional role
rax eax ax al accumulator, function return value
rbx ebx bx bl general purpose (callee-saved)
rcx ecx cx cl counter, shift counts, 4th argument
rdx edx dx dl data, high half of multiply/divide, 3rd argument
rsi esi si sil source index, 2nd argument
rdi edi di dil destination index, 1st argument
rbp ebp bp bpl base pointer (frame pointer)
rsp esp sp spl stack pointer
r8 to r15 r8d to r15d r8w to r15w r8b to r15b general purpose

The "traditional role" column is mostly history plus convention. Apart from rsp (the stack pointer, which push, pop, call and ret use implicitly), only a few instructions truly demand a specific register: shifts by a variable amount read the count from cl, mul and div use rax and rdx, and string instructions use rsi, rdi and rcx. Which register holds a function's first argument is a convention of the operating system's ABI, covered in Calling Conventions. Do not read more into the names than that.

The names rax, rbx and so on come from the 8086's 16-bit ax, bx, ... The 32-bit 80386 prefixed an e ("extended"), and x86-64 replaced it with an r. That is the whole naming story.

One register, several names

A register such as rax is not four separate registers. It is one 64-bit storage cell that you can address through narrower windows:

flowchart LR
  A["rax (bits 63..0)"] --> B["eax (bits 31..0)"]
  B --> C["ax (bits 15..0)"]
  C --> D["ah (bits 15..8)"]
  C --> E["al (bits 7..0)"]

The names ax, eax and rax are views of the same storage; ah only exists for the four legacy registers a, b, c and d.

Writing through a narrow name changes only those bits in most cases, so mov al, 0xFF sets the lowest byte and leaves the other 56 bits alone. There is one famous exception, which is the next section. The high-byte names ah, bh, ch and dh exist for compatibility; you cannot use them in an instruction that also needs a REX prefix (for example anything touching sil, dil or r8 to r15), which is why compilers rarely emit them.

The zero-extension rule

When an instruction writes a 32-bit register such as eax, the processor also clears bits 63 to 32. When it writes a 16-bit or 8-bit register, the other bits are kept.

NASM
mov rax, 0x1122334455667788   ; rax = 1122334455667788
mov eax, 0xAABBCCDD           ; rax = 00000000AABBCCDD  (top half cleared!)
mov ax,  0x1234               ; rax = 00000000AABB1234  (only low 16 bits changed)
mov al,  0xFF                 ; rax = 00000000AABB12FF  (only low 8 bits changed)

This asymmetry was a deliberate design choice. Partial writes to 8- and 16-bit registers forced the hardware to merge the new bits with the old register contents, which creates a dependency on the register's previous value and slows things down. For 32-bit writes AMD decided to drop the old upper bits entirely, so the instruction does not depend on them. Most 32-bit values in real programs are ints, so this makes the common case cheap.

It also gives compilers a trick. To set rax to zero, xor eax, eax (2 bytes) clears the whole 64-bit register, and is shorter than xor rax, rax (3 bytes, because of the extra REX prefix). Likewise, mov eax, 5 sets all 64 bits of rax to 5 and takes 5 bytes, where mov rax, 5 would take 7.

Let us model this. The demo below keeps a register as a 64-bit BigInt and implements the four write widths exactly as described.

JS Partial writes to rax: which bits survive?

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

No output.

Notice the last line: mov eax, -1 produces 0x00000000ffffffff, not all ones. If you want a 64-bit -1 you must write mov rax, -1 (or sign-extend with movsxd, see MOV, LEA and Moving Data). This is a classic source of bugs when 32-bit arithmetic results are later used as 64-bit indexes: the value is zero-extended, so a negative int becomes a large positive number unless the compiler inserts a sign-extension.

Reading register names in the wild

Once you know the pattern you can decode any register name. The r8 to r15 family uses suffixes: d for double-word (32 bits), w for word (16 bits) and b for byte (8 bits). The classic four use the prefix and suffix letters from the table above. The next demo prints the whole table by code and decodes names.

JS Decode any x86-64 register name

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

No output.

You will rarely see the 16-bit names in modern code, except for ax appearing in a handful of instructions, because 16-bit operations need an extra operand-size prefix byte. The common widths are 64 (pointers, long, size_t) and 32 (int).

rip: the instruction pointer

rip holds the address of the next instruction. You cannot mov into it directly; it changes by normal execution, jmp, call, ret and conditional jumps. What x86-64 added is rip-relative addressing: an operand like [rip + 0x2ee4] means "the byte at the address of the following instruction plus 0x2ee4". This lets code reference its own constants and global variables without knowing where the program was loaded, which is the foundation of position-independent code. You will see it in nearly every real listing, for example lea rdi, [rip + .LC0] to get the address of a string literal.

rflags: the status register

rflags collects single-bit status flags updated by arithmetic and logic. The ones you will use constantly are the carry flag CF (bit 0), parity PF (bit 2), zero ZF (bit 6), sign SF (bit 7), and overflow OF (bit 11). The full register also holds control flags such as the interrupt flag IF (bit 9) and the direction flag DF (bit 10). Programs do not read rflags as a whole in everyday code; conditional jumps test individual bits. The lesson on flags and conditions goes through how each flag is set. The snippet below decodes a raw value, the way a debugger prints it.

JS Decode an rflags value

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

No output.

The value 0x246 is what a debugger often shows right after a comparison found two equal numbers: ZF, PF and the always-on interrupt flag. Bit 1 of rflags is always 1 and is not named here.

xmm registers

Floating-point and vector work uses a separate bank: xmm0 to xmm15, each 128 bits wide. On processors with AVX they extend to 256-bit ymm0 to ymm15 (the xmm registers are their lower halves), and AVX-512 adds 512-bit zmm registers. A scalar double lives in the low 64 bits of an xmm register; instructions such as addsd xmm0, xmm1 add the low doubles. Function returns of float and double come back in xmm0. We come back to these in Floating Point and SIMD.

Caller-saved and callee-saved: a preview

When a function calls another function, which registers can it count on being unchanged afterwards? Under the System V ABI used on Linux and macOS:

  • Caller-saved (the callee may destroy them): rax, rcx, rdx, rsi, rdi, r8 to r11, and all xmm registers.
  • Callee-saved (the callee must restore them before returning): rbx, rbp, r12 to r15, and of course rsp.

So if you keep a loop counter in rcx across a call, it may be gone, but one in rbx or r12 will still be there. Windows x64 uses a slightly different split, which the calling-conventions lesson compares side by side.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does rax contain after mov rax, -1 followed by mov eax, 5?

    A write to a 32-bit register clears bits 63..32, so the all-ones upper half is wiped and only the value 5 remains.
  2. 2. Which instruction leaves the upper 56 bits of rax unchanged?

    8-bit and 16-bit writes only replace their own bits. Any 32-bit write (including xor eax, eax) zeroes the upper half.
  3. 3. Which name refers to the lowest 8 bits of the register rsi?

    si is the 16-bit name, esi the 32-bit name, and sil the 8-bit name (it needs a REX prefix, which is why it only exists in 64-bit mode).
  4. 4. Which of these registers is callee-saved in the System V ABI?

    rbx, rbp and r12 to r15 must be preserved by the callee. rdi, rcx and r10 are caller-saved and may be overwritten by any function you call.

Summary

  • x86-64 has sixteen 64-bit general-purpose registers: rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp and r8 to r15.
  • Each register has narrower names: for example rax, eax, ax, al (and ah); the numbered ones use d, w and b suffixes.
  • Writing a 32-bit register zeroes bits 63..32; writing 16-bit or 8-bit names preserves the rest.
  • rip is the instruction pointer and supports rip-relative addressing; rflags holds status flags such as CF, ZF, SF and OF.
  • xmm0 to xmm15 are 128-bit registers for floating-point and SIMD work.
  • Under System V, rbx, rbp, r12 to r15 are callee-saved; the rest are caller-saved.

Next: Memory, Addresses and Endianness, where we look at the other half of the machine: the bytes outside the CPU.