Skip to main content
Be Bitwise
x86 Assemblyx86 Basics & Environment

Registers and Data Sizes

Registers are the fastest storage your computer has. They sit inside the CPU itself, accessible in a single clock cycle — orders of magnitude quicker than RAM. x86-64 gives you 16 general-purpose registers, 64 bits wide each, plus a handful of special-purpose registers that steer how the processor behaves.

Every assembly operation either reads from or writes to them. This is your workspace; learn it.

For a deeper dive into register architecture, see x86 Registers.

The 16 General-Purpose Registers

x86-64 provides these 16 general-purpose registers:

RegisterTraditional UseCallee-saved?
raxReturn value, accumulatorNo
rbxBase registerYes
rcxCounter (loop operations)No
rdxData, I/O, multiply/divide extensionNo
rsiSource index for string operationsNo
rdiDestination index for string operationsNo
rspStack pointerYes
rbpBase/frame pointerYes
r8General purposeNo
r9General purposeNo
r10General purposeNo
r11General purposeNo
r12General purposeYes
r13General purposeYes
r14General purposeYes
r15General purposeYes

The first eight (rax through rbp) are descendants of the original x86 set. AMD bolted on r8 through r15 when they stretched the architecture to 64 bits.

Key Concept: 'General Purpose' Is Misleading

While all 16 registers can hold arbitrary data, many have implicit uses baked into the instruction set. For example, rcx is the implicit counter for loop and rep instructions, rax holds the return value from functions and syscalls, and rsp always points to the top of the stack. You can use them for other things, but you must be aware of instructions that read or modify them implicitly.

Register Sub-Parts

Each 64-bit register can also be poked at smaller widths. That's the fossil record of x86 climbing from 16-bit to 32-bit to 64-bit:

For rax through rsp:

  • 64-bit: rax, rbx, rcx, rdx, rsi, rdi, rsp, rbp
  • 32-bit (lower half): eax, ebx, ecx, edx, esi, edi, esp, ebp
  • 16-bit (lower quarter): ax, bx, cx, dx, si, di, sp, bp
  • 8-bit low: al, bl, cl, dl, sil, dil, spl, bpl
  • 8-bit high (first four only): ah, bh, ch, dh

For r8 through r15, the naming pattern is:

  • 64-bit: r8, r9, ... r15
  • 32-bit: r8d, r9d, ... r15d
  • 16-bit: r8w, r9w, ... r15w
  • 8-bit: r8b, r9b, ... r15b

Open the bit visualizer below to watch a value shift shape across the different register widths:

215
214
213
212
211
210
29
28
27
26
25
24
23
22
21
20
Decimal255
Hex0x00FF
Binary0000000011111111
Critical Rule: 32-bit Writes Zero-Extend

Writing to a 32-bit register (like eax) automatically zeros the upper 32 bits of the full 64-bit register. This is by design - it avoids partial register dependencies. However, writing to 16-bit or 8-bit sub-registers does NOT zero-extend. This inconsistency is a common source of bugs: mov al, 5 modifies only the lowest byte and leaves the upper 56 bits of rax unchanged, but mov eax, 5 zeros the upper 32 bits.

Special-Purpose Registers

Three registers come with dedicated jobs you can't repurpose:

rip (Instruction Pointer) holds the address of the next instruction. You don't write to it via mov; it advances on its own and shifts when jumps, calls, or returns execute.

rsp (Stack Pointer) points at the top of the stack. Technically general-purpose, but use it for anything else and your program crashes within microseconds.

rflags (Flags Register) is a 64-bit register whose individual bits record what just happened during the last arithmetic or logical operation. The ones you'll touch most:

FlagBitMeaning
CF (Carry)0Unsigned overflow occurred
ZF (Zero)6Result was zero
SF (Sign)7Result was negative
OF (Overflow)11Signed overflow occurred

Data Sizes

x86-64 has its own vocabulary for data widths:

NameSizeBitsNASM DirectiveRegister Example
byte1 byte8dbal
word2 bytes16dwax
dword (double word)4 bytes32ddeax
qword (quad word)8 bytes64dqrax

For memory accesses, you usually have to spell out the size:

nasm
mov byte [rdi], 0 ; write 1 byte of zeros
mov word [rdi], 0 ; write 2 bytes of zeros
mov dword [rdi], 0 ; write 4 bytes of zeros
mov qword [rdi], 0 ; write 8 bytes of zeros
Security Note: Register Leaks

In security contexts, registers can contain sensitive data from previous operations - encryption keys, passwords, or pointers to protected memory. When writing security-sensitive code, you should explicitly clear registers before returning from functions. A common pattern is xor eax, eax to zero a register (which zeros the full 64-bit rax via zero-extension). Failing to clear registers is a real vulnerability class in cryptographic implementations.

Practical Examples

nasm
; Loading different-sized values
mov rax, 0xDEADBEEFCAFEBABE ; 64-bit immediate into rax
mov eax, 0xCAFEBABE ; 32-bit, zeros upper 32 bits of rax
mov ax, 0xBABE ; 16-bit, preserves upper 48 bits
mov al, 0xBE ; 8-bit, preserves upper 56 bits
; Common register operations
xor eax, eax ; zero rax (preferred: 2 bytes, opcode 31 c0, zeros full 64-bit rax via zero-extension; xor rax, rax requires a REX prefix and is 3 bytes)
mov rcx, rdi ; copy rdi to rcx

Predict the Register

Each line below starts fresh from mov rax, -1, so every bit of rax is set. Write down rax in hex after the second instruction, then open the answer.

nasm
mov rax, -1 ; then one of:
mov eax, 5 ; (a)
mov ax, 5 ; (b)
mov al, 5 ; (c)
mov ah, 5 ; (d)
mov eax, eax ; (e)
Answers
  • (a) 0x0000000000000005. A 32-bit write zeroes the top half.
  • (b) 0xFFFFFFFFFFFF0005. Only bits 15-0 change.
  • (c) 0xFFFFFFFFFFFFFF05. Only bits 7-0 change.
  • (d) 0xFFFFFFFFFFFF05FF. ah is bits 15-8, so the 05 lands one byte up.
  • (e) 0x00000000FFFFFFFF. Copying eax onto itself looks like a no-op, but it's a 32-bit write, so it clears the upper half. Compilers use exactly this to zero-extend a 32-bit value.

What is Next

With the register file in your head, the next lesson digs into how assembly programs are organised into sections, and how NASM directives let you carve out data and structure your code.