Skip to content
systemcoding.dev
Assembly

x86-64 General-Purpose Registers Explained: RAX to R15 and Sub-Registers

The sixteen x86-64 general-purpose registers, their 32, 16 and 8-bit names, what each is conventionally for, and the write rules that trip up beginners.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Intel, AMD and Arm logos.

x86-64 has sixteen general-purpose registers, and every one of them can be addressed by up to four different names depending on how many bits you want. The names look like alphabet soup until you see the pattern, and then one rule about which writes clear the rest of the register explains most of the surprises a beginner meets. This post lays out all sixteen, the sub-register names, what each register is conventionally used for, and the write rules, using Intel syntax and the AMD64 and Intel 64 manuals as the references.

Sixteen registers, 64 bits each

A register is a storage slot inside the processor that instructions can read and write far faster than memory. The general-purpose ones hold integers and addresses. On x86-64 there are sixteen, each 64 bits wide:

64-bit32-bit16-bit8-bit lowConventional use
raxeaxaxalreturn value, mul/div operand
rbxebxbxblgeneral, preserved across calls
rcxecxcxclshift count, rep counter, 4th argument
rdxedxdxdlhigh half of mul/div, 3rd argument
rsiesisisilsource pointer, 2nd argument
rdiedididildestination pointer, 1st argument
rbpebpbpbplframe pointer, preserved across calls
rspespspsplstack pointer
r8 to r15r8d to r15dr8w to r15wr8b to r15bgeneral (r8 and r9 carry arguments 5 and 6)

The first eight are the registers inherited from the 32-bit 8086 lineage. The prefix tells you the width: r for 64 bits, e (for "extended") for the 32-bit view, nothing for 16 bits. AMD added r8 through r15 when it extended the architecture to 64 bits, and gave them a regular naming scheme: add d for 32 bits (doubleword), w for 16 (word), b for 8 (byte).

There is also rip, the instruction pointer, and rflags, the flags register. Neither is general purpose: rip is read by lea rax, [rel label] and changed by jumps, and flags are the subject of a later post.

The sub-register names are not separate registers. eax is the low 32 bits of rax, ax is the low 16 bits of that, and al is the low 8. Writing one changes the matching bits of the bigger one, with one exception covered below.

What the "conventional use" column really means

Only a few registers are wired into instructions. The rest are conventions that compilers and the calling convention agree on.

The hardwired ones are few. rsp is the stack pointer, and push, pop, call and ret use it implicitly. rcx is the counter for rep string instructions and the shift count for shl rax, cl. rax and rdx are the implicit operands of the one-operand mul, imul and div. rsi and rdi are the source and destination of movsb and its relatives.

Everything else is convention. The System V ABI used on Linux and macOS passes the first six integer arguments in rdi, rsi, rdx, rcx, r8 and r9, in that order, and returns in rax. The Windows x64 convention uses rcx, rdx, r8, r9 instead. A later post in this series covers both. rbp is the frame pointer by tradition, but a compiler running with optimization is free to treat it as a general register, so do not rely on it unless the code was built with -fno-omit-frame-pointer.

How the sub-registers behave when you write them

This is the part that matters, and it has three rules.

  1. A 32-bit write zeroes the upper 32 bits. mov eax, 1 sets rax to 1 entirely.

  2. A 16-bit or 8-bit write leaves the rest alone. mov ax, 1 changes only the low 16 bits, and mov al, 1 only the low 8.

  3. A 64-bit write replaces everything.

Watch the effect on one register. Start with a known 64-bit value and apply each write in turn:

regs.asmasm
        mov     rax, 0x1122334455667788   ; 64-bit write
        mov     ax,  0xAABB               ; 16-bit write
        mov     al,  0xCC                 ; 8-bit write
        mov     ah,  0xDD                 ; second byte of ax
        mov     eax, 0x01020304           ; 32-bit write
Text
after line 1:  rax = 0x1122334455667788
after line 2:  rax = 0x112233445566AABB
after line 3:  rax = 0x112233445566AACC
after line 4:  rax = 0x112233445566DDCC
after line 5:  rax = 0x0000000001020304

Lines 2, 3 and 4 touch only the bytes they name. Line 5 is the odd one: writing eax wiped out the upper half, which is why mov eax, 0 is the standard way to zero all of rax and costs fewer bytes than mov rax, 0. A later post in this series explains the reason for the 32-bit rule.

The practical consequence is a classic bug. Code that reads a 32-bit int into eax and then uses rax as an array index gets a clean zero-extended value, which is right for an unsigned index and wrong for a negative signed one. The compiler inserts movsxd rax, eax (sign-extend) when the C type is signed, and you must do the same by hand.

The byte registers and the REX restriction

The 8-bit names come in two groups, and they do not mix freely. The old 8086 names ah, bh, ch and dh address bits 8 to 15 of rax, rbx, rcx and rdx. The newer names sil, dil, bpl, spl and r8b to r15b address the low byte of the others. Because the new names need a REX prefix on the instruction, and the instruction encoding reuses the same bits for ah to dh without one, an instruction cannot use a high-byte register and a REX-only register together.

asm
        mov     ah, sil        ; invalid: ah cannot be encoded with sil
Text
error: cannot use high register in rex instruction

That is NASM 2.16's wording for the failure, and the fix is to avoid the high-byte registers in new code. Use movzx eax, sil to widen a byte, or shift and mask a wider register when you need bits 8 to 15.

Choosing registers in your own code

When writing a function, a few habits keep it correct and readable:

  • Prefer the 32-bit name for values that fit in 32 bits. xor eax, eax is shorter than xor rax, rax and does the same job, and it breaks the dependency on the old value.

  • Use rax, rcx, rdx, rsi, rdi and r8 to r11 freely inside a leaf function. The System V ABI lets a callee overwrite them. These are the caller-saved registers, discussed in a later post.

  • Save and restore rbx, rbp and r12 to r15 if you use them, because the caller expects them unchanged.

  • Never store anything you want to keep in rsp. It must always point at the top of the stack, since signal handlers and interrupts push onto it at any moment.

Here is a function that follows those habits. It computes a * 3 + b for two 32-bit integers passed in edi and esi:

mul3add.asmasm
; int mul3add(int a, int b)   a in edi, b in esi, result in eax
global mul3add
mul3add:
        lea     eax, [rdi+rdi*2]   ; eax = a * 3
        add     eax, esi           ; eax += b
        ret

With a equal to 5 and b equal to 7, eax holds 5*3 + 7:

Text
22

It uses only caller-saved registers, so it saves nothing, and the lea with a scaled index does the multiply by three in one instruction without touching the flags.

Where this model ends

The sixteen-register picture is the integer view only. Floating-point and vector work uses a separate file of registers (xmm0 to xmm15 at minimum, ymm and zmm wider), covered under SIMD. The segment registers fs and gs survive in 64-bit mode for thread-local storage. And a few instructions, such as cpuid and syscall, clobber registers that are not in their operand list, so always read the instruction's reference page rather than trusting the visible operands.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.