x86-64 Registers, Flags and the Instructions That Matter
Open any x86-64 disassembly and it looks like hundreds of different instructions. In practice compiler output for ordinary C uses a few dozen, and a handful of them carry most of the meaning: data moves, address arithmetic, comparisons, conditional jumps, calls. An analyst who recognises those at a glance reads a function in minutes instead of looking up every line. This lesson is the reading list. The x86-64 Assembly course teaches the same instructions by writing them (Registers, Flags, cmp & test, Jumps & Conditionals); here the focus is on recognising them in code you didn't write.
Registers as an analyst sees them
| Register | 32 / 16 / 8-bit names | What it usually holds in compiled code (Linux, System V) |
|---|---|---|
rax |
eax, ax, al |
return value; scratch |
rdi, rsi, rdx, rcx, r8, r9 |
edi, esi, edx, ecx, r8d, r9d |
arguments 1 to 6 on entry to a function |
rbx, rbp, r12-r15 |
ebx, ebp, r12d... |
values that must survive calls (callee-saved) |
rsp |
esp |
stack pointer: locals live at [rsp+n] or [rbp-n] |
rip |
instruction pointer: [rip+0x2e5a] is how position-independent code reaches globals and strings |
|
rflags |
the flags below |
Two facts save a lot of confusion:
- Writing a 32-bit register clears the upper 32 bits of the 64-bit register.
mov eax, 1sets all ofraxto 1, which is why compilers prefer the shorter 32-bit forms. Writingaloraxleaves the upper bits alone. xor eax, eaxis the standard way to set a register to zero (shorter thanmov eax, 0). It is not encryption.
The flags
Arithmetic and comparison instructions set four flags that conditional jumps read:
| Flag | Set when | Read by |
|---|---|---|
| ZF zero | the result is 0 (after cmp a, b: a equals b) |
je/jz, jne/jnz, sete |
| SF sign | the top bit of the result is 1 | js, jns; part of signed jumps |
| CF carry | an unsigned borrow or carry happened (after cmp a, b: a is below b as unsigned numbers) |
jb, jae, ja, jbe (unsigned) |
| OF overflow | the result overflowed as a signed number | part of jl, jge, jg, jle (signed: "less" means SF differs from OF) |
cmp a, b computes a - b and keeps only the flags; test a, b computes a AND b and keeps only the flags (test eax, eax asks "is eax zero, is it negative?"). Whether the comparison was signed or unsigned is decided by the jump, not by cmp: that one mnemonic is often the whole difference between a correct bounds check and an out-of-bounds read (see Reading Compiler Output).
The program below compares three pairs of numbers and prints the four flags after each cmp, plus whether a signed (jl) and an unsigned (jb) "less than" jump would be taken.
section .data
pairs: dq 3, 5, 5, 5, -1, 1 ; (a, b) pairs for cmp a, b
line: db "ZF=? SF=? CF=? OF=? jl=? jb=?", 10
len equ $ - line
section .text
global _start
_start:
xor r12d, r12d ; pair index
.next:
mov rax, [pairs + r12*8]
mov rbx, [pairs + r12*8 + 8]
cmp rax, rbx ; flags from a - b
setz byte [line + 3] ; each setcc writes 0 or 1
sets byte [line + 8]
setc byte [line + 13]
seto byte [line + 18]
setl byte [line + 24] ; signed less: SF != OF
setb byte [line + 29] ; unsigned below: CF = 1
mov rcx, line ; turn 0/1 bytes into '0'/'1'
add byte [rcx + 3], '0'
add byte [rcx + 8], '0'
add byte [rcx + 13], '0'
add byte [rcx + 18], '0'
add byte [rcx + 24], '0'
add byte [rcx + 29], '0'
mov eax, 1 ; write(1, line, len)
mov edi, 1
mov rsi, line
mov edx, len
syscall
add r12, 2
cmp r12, 6
jb .next
mov eax, 60 ; exit(0)
xor edi, edi
syscallZF=0 SF=1 CF=1 OF=0 jl=1 jb=1
ZF=1 SF=0 CF=0 OF=0 jl=0 jb=0
ZF=0 SF=1 CF=0 OF=0 jl=1 jb=0
Read the third line carefully. -1 compared with 1: as signed numbers −1 is less (jl=1); as unsigned numbers 0xFFFFFFFFFFFFFFFF is enormous, so it is not below (jb=0, CF clear). The same bits, two meanings, chosen by the jump.
The instructions that carry the meaning
| Pattern | Means |
|---|---|
mov r, [base+index*scale+disp] |
load: an array element, a struct field, a local |
lea r, [base+index*scale+disp] |
address arithmetic only, no memory access: &a[i], or a cheap multiply-add such as x*3 = lea eax, [rdi+rdi*2] |
add, sub, imul, shl, sar, and, or |
arithmetic; sar is a signed shift right (division by a power of two, with a small fix-up for negative numbers because C rounds toward zero), shr the unsigned one |
cmp / test then jcc, setcc or cmovcc |
a condition: a branch, a boolean, or a branch-free select |
call f / ret |
function call and return; arguments were set up just before the call |
push / pop, sub rsp, n |
saving registers, reserving stack space |
movzx / movsx |
widen an unsigned / signed smaller value |
endbr64, nop, nop DWORD PTR [rax+rax*1+0x0] |
landing pads (CET) and alignment padding: skip them |
The session compiles four one-line C functions and prints the optimised code. Each one shows one idiom from the table.
cat > idioms.c <<'SRC'
int is_zero(int x) { return x == 0; }
int times3(int x) { return x * 3; }
long elem(const long *a, long i) { return a[i]; }
int max(int a, int b) { return a > b ? a : b; }
SRC
gcc -O2 -S -masm=intel -fno-asynchronous-unwind-tables -fcf-protection=none -o - idioms.c \
| grep -vE '^\s*\.(file|text|p2align|globl|type|size|ident|section)|^\s*