Skip to content

Lesson 2 of 13 · 22m

Reading x86-64 Without Panicking

Fifteen instructions plus the SysV calling convention cover most of real disassembly — the rest is recognizing compiler idioms like lea arithmetic, endbr64, and reciprocal division.

You do not need the 5,000-page Intel manual. Compiled x86-64 uses a small vocabulary obsessively — a dozen instructions cover ninety percent of what a disassembler shows you. The wrong mental model is "learn the ISA." The manual is a reference, not a teacher. What you need is the working vocabulary, the calling convention, and the shape of a stack frame — after that, pattern-matching carries the load.

The working vocabulary

mov  rax, [rdi+0x18]   ; load the value at address rdi+0x18
lea  rax, [rdi+0x18]   ; compute the address itself, no dereference
push rbp / pop rbp     ; save / restore on the stack
call 0x401040 / ret    ; call a function / return to caller
cmp  eax, 0x2a         ; set flags = eax - 0x2a, discard the result
test eax, eax          ; flags from eax AND eax — the classic null check
jz   / jnz / jl / jge  ; conditional jumps on those flags
jmp  0x401050          ; unconditional jump
xor  eax, eax          ; idiom: zero a register
nop                    ; do nothing — alignment padding and patch space

lea shows up constantly doing arithmetic in disguise — lea eax, [rdi+rdi*4] is x*5 wearing a trench coat. Compilers love it because it's a one-instruction multiply-add that doesn't touch flags.

Two more that confuse people on sight. movzx/movsxd widen a byte or word into a register — movzx eax, byte [rsi] reads a char; movsxd rax, dword [rbp-0x4] sign-extends a 32-bit int for pointer math. And endbr64, the four-byte nothing that opens almost every function in a modern binary: it's a landing-pad marker for CET indirect-branch protection (-fcf-protection, default on Fedora and recent Debian/Ubuntu builds). As a reader you ignore it; as a patcher you remember it's there.

The System V calling convention

On Linux x86-64, integer arguments go rdi, rsi, rdx, rcx, r8, r9 in order; the return value lands in rax. rbx, rbp, and r12–r15 are callee-saved — when a prologue pushes them, the function intends to use them and will restore them on the way out. This one fact lets you read any call site instantly: the register loads immediately before it are the arguments. One alignment fact worth internalizing now: rsp stays 16-byte aligned at every call. The ABI demands it, and SSE spills like movaps [rsp+0x20], xmm0 fault on a misaligned stack — which is why hand-written assembly that calls printf on a sloppily-aligned stack crashes inside libc, not in your code.

Anatomy of a function

push rbp
mov  rbp, rsp            ; establish frame pointer
sub  rsp, 0x30           ; reserve 48 bytes of locals
mov  [rbp-0x14], edi     ; first arg stored into a local
mov  [rbp-0x20], rsi     ; second arg (looks like a pointer)
...
mov  eax, [rbp-0x14]     ; compute return value into rax
leave                    ; = mov rsp,rbp ; pop rbp
ret

[rbp-0x14] is a local variable — Ghidra will name it local_14. Count the locals, watch which get written versus only read, and the function's working memory maps itself.

A leaf function — one that calls nothing — often skips sub rsp entirely and spills locals into the red zone: the 128 bytes below rsp the System V ABI guarantees signal handlers won't clobber. Writes to [rsp-8] and [rsp-0x10] with no frame setup in sight aren't disassembler noise; they're a leaf function's locals.

Idioms the compiler won't stop using

  • Division by constant becomes multiplication. x / 10 at -O2 compiles to imul by 0xCCCCCCCD followed by a shift right — the reciprocal magic number. When you see a bizarre multiply-and-shift where you expected div, that's division, recovered.
  • rep movs / rep stos — memcpy and memset inlined in place.
  • cdqe — sign-extend eax into rax; usually means an int is about to index a pointer.
  • The magic XOR. xor eax, eax is the canonical zero: two bytes against mov eax, 0's five, and the CPU recognizes xor r,r at the register renamer as a zeroing idiom — no execution unit, and no dependency on the register's old value, so the chain can't stall. It sets flags (ZF goes to 1, CF and OF clear); mov eax, 0 survives as the encoding chosen precisely when the previous compare's flags must live on.

Two syntaxes, one machine

GDB defaults to AT&T order (mov %rdi,%rax — source first); Ghidra, IDA, and x64dbg show Intel order (mov rax,rdi — destination first). One-time fix: set disassembly-flavor intel, preferably in ~/.gdbinit so you never see AT&T again.

Field notes

  • test eax,eax + jz is a zero/null check; cmp followed by jae/jb means the source compared unsigned, jge/jl means signed — the jump mnemonic preserves type information the decompiler will guess at.
  • Windows x64 uses a different convention: rcx, rdx, r8, r9, plus 32 bytes of caller-reserved shadow space below the return address. Know which world your binary lives in.
  • At -O2, frame pointers are usually omitted — locals become [rsp+...] and their offsets shift as rsp moves through the function. Same concept, harder to track by eye; this is where the decompiler earns its keep.

Keep reading

Related articles from the SudoSecurity blog.