Skip to content
Registersx86-64

RAX Register

RAX is the x86-64 accumulator: return values, syscall numbers, the implicit operand of mul, div and string instructions, and what EAX, AX, AL and AH alias.

RAX is where answers come back. Integer and pointer return values land in it on every mainstream x86-64 ABI, system call numbers go in through it, and a long list of instructions use it without naming it. When you are lost in a function, the value of RAX after each call is the cheapest thing to follow.

Return values

What is returnedWhere it lands
Integer or pointer up to 64 bitsrax
128-bit integer, or a small struct of two wordsrdx:rax
float / doublexmm0, not rax
Struct too big for registerscaller passes a hidden buffer pointer; the callee returns that pointer in rax

Narrow types only define the low part. A function returning bool sets al and leaves the upper 56 bits holding whatever was there, which is why callers test al and not rax:

asm
call  is_licensed
test  al, al
je    .unregistered

Patch that branch, or patch the callee to mov al, 1; ret (b0 01 c3), and you have the oldest crack in the book.

Sub-registers and the zero-extension rule

asm
mov rax, 0x1122334455667788
; eax = 0x55667788   low 32 bits
; ax  = 0x7788       low 16 bits
; al  = 0x88         low 8 bits
; ah  = 0x77         bits 8 to 15

Writing EAX zeroes the upper 32 bits of RAX. Writing AX, AL or AH leaves the rest untouched. That asymmetry is why compilers emit xor eax, eax (31 c0, two bytes) to clear the full register, and why mov eax, 1 is a complete 64-bit assignment.

Uses nobody spells out

RAX is an implicit operand in more instructions than any other register:

  • mul and imul (one-operand form) put the product in rdx:rax. div and idiv divide rdx:rax and leave the quotient in rax.
  • cdqe sign-extends eax into rax; cqo sign-extends rax into rdx ahead of a signed divide.
  • lods loads from [rsi] into it, stos stores it to [rdi], scas compares against it. rep stosb with al = 0 is an inlined memset.
  • cmpxchg compares against it, and writes the current value back into it on failure.
  • cpuid takes the leaf number in eax; rdtsc returns the timestamp in edx:eax. Both are staples of anti-VM and timing checks.
  • syscall takes the system call number in rax on Linux and in eax on Windows (mov r10, rcx; mov eax, 0x55; syscall is the shape of every ntdll stub).

On Linux the kernel also returns through it: a value from -4095 to -1 is a negated errno, anything else is success.

The variadic count

On System V, a call to a variadic function sets al to the number of vector registers used for arguments. That is the reason for the xor eax, eax you see right before printf:

asm
lea  rdi, [rip+0xe8a]    ; format string
mov  esi, ebx
xor  eax, eax            ; no floating-point arguments
call printf

It is not a return value being cleared, and decompilers that show it as an extra argument are wrong.

Reverse-engineering notes

RAX is caller-saved everywhere. Nothing in it survives a call, so a value read from rax after a call came from that call.

Functions end with xor eax, eax (return 0), mov eax, 1, or or eax, 0xffffffff / mov eax, 0xffffffff (return -1). Reading those three patterns gets you a function's success and failure paths before you understand anything else about it.

Related: RDX, which pairs with it for wide multiply and divide, calling conventions, syscall, and the full register map.