Skip to content

Lesson 1 of 9 · 24m

Registers Are the Machine State You Actually Have

Learn x86-64 general-purpose registers, width aliases, flags, and the discipline of tracking state instead of memorizing mnemonics.

I once watched a smart engineer spend an hour staring at a crash because he read mov eax, 1 as if it assigned a variable. It does assign a value, but it also zeroes the upper half of rax, and the missing high bytes were the whole bug. Assembly is unforgiving because there is no hidden state. Every register is shared, mutable, and meaningful.

The machine is smaller than you think

On x86-64 Linux, your general-purpose integer registers are:

rax rbx rcx rdx rsi rdi rsp rbp r8-r15

They are not variables owned by one function. They are the processor's working storage. rsp points at the top of the stack, rip points at the next instruction, and rbp is conventionally used as a frame pointer, though optimized code often reuses it as a normal register.

Width aliases are the first trap

The same physical register appears under different names:

rax  64 bits      eax  low 32 bits
ax   low 16 bits  al   low 8 bits

These two instructions are not equivalent:

mov rax, 1        ; writes all 64 bits
mov eax, 1        ; writes low 32 bits and zeroes upper 32 bits
mov ax, 1         ; writes low 16 bits only
mov al, 1         ; writes low 8 bits only

That zeroing rule applies to 32-bit writes in 64-bit mode. It does not apply to 8-bit or 16-bit writes. Half the confusion in beginner assembly is stale upper bits.

Flags are state too

Arithmetic instructions update status flags in rflags. cmp is really subtraction without saving the result:

cmp rax, rbx
je equal          ; jump if ZF=1
jb below          ; unsigned comparison, CF=1
jl less           ; signed comparison, SF != OF

Signedness is not a property of the bits. It is a property of the instruction interpretation. The same 0xffffffff can be -1 or 4294967295 depending on whether you use signed or unsigned branches.

A practical register-reading drill

When reading a function, keep a small table beside you:

rdi  first argument
rsi  second argument
rax  running result / syscall number
rcx  loop counter or temporary
rsp  stack position

Update it line by line. Do not try to hold the whole function in your head. Good assembly reading is bookkeeping, not brilliance.

Field notes

  • rip cannot be written with mov; control-flow instructions change it.
  • rsp must remain correctly aligned before calls in the System V ABI.
  • Some registers have strong conventions, but most are only conventions, not hardware rules.
  • Write 32-bit results when you mean 32-bit results; accidental full-register assumptions create subtle bugs.
  • In Intel syntax the destination comes first: mov rax, rbx. AT&T syntax reverses operands and decorates registers with %.

Was this lesson helpful?

Sign in to rate