TExcelTAD
Home › Reverse Engineering & Binary Analysis › x86-64 Registers, Flags and the Instructions That Matter

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, 1 sets all of rax to 1, which is why compilers prefer the shorter 32-bit forms. Writing al or ax leaves the upper bits alone.
  • xor eax, eax is the standard way to set a register to zero (shorter than mov 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.

nasm · try it yourself
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
    syscall
ZF=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.

bash · try it yourself
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*
  

	.intel_syntax noprefix
is_zero:
	xor	eax, eax
	test	edi, edi
	sete	al
	ret
times3:
	lea	eax, [rdi+rdi*2]
	ret
elem:
	mov	rax, QWORD PTR [rdi+rsi*8]
	ret
max:
	cmp	esi, edi
	mov	eax, edi
	cmovge	eax, esi
	ret

is_zero is test plus sete (with xor eax, eax first, so the upper bits are zero). times3 is a single lea, no multiply. elem is one scaled load: rsi*8 because a long is 8 bytes. max has no branch at all: cmp then cmovge picks one of the two values. A reverse engineer who sees cmov thinks "ternary operator or max", not "loop".

How this knowledge is used in detection

  • Recognising idioms quickly is what makes code review of binaries feasible: a signed jump after a length comparison, a missing upper or lower bound, a call to a function that writes memory with a length taken from input.
  • Flags explain decompiler output: when Ghidra writes if ((int)x < (int)n) with casts, the casts come from the jump the compiler chose. If the decompiler shows a signed comparison on a size, look at the jump yourself.
  • Odd instructions stand out: rdtsc (timing), cpuid (environment checks), int3, syscall in a normal application, xor in a loop over a buffer (decoding). Each is a hint worth a note, covered in later lessons.

How mistakes are prevented

  • For authors: use unsigned types (size_t) for sizes and indexes so the compiler emits unsigned jumps; compile with -Wsign-compare -Wconversion.
  • For analysts: always read the jump after a cmp before deciding what a check means, and keep a cheat sheet of jcc mnemonics (jb/jnae/jc are the same instruction; jae/jnb/jnc too).

Pitfalls

  • AT&T syntax (mov %rdi,%rax, source first) is objdump's default; this course uses -M intel (destination first).
  • Aliased mnemonics: je/jz, jne/jnz, jb/jc, jae/jnb/jnc are identical encodings; tools pick different names.
  • lea does not read memory, even though it has brackets.
  • Partial registers: mov al, 1 leaves the rest of rax unchanged, unlike mov eax, 1.

Practice

1. After cmp eax, 10, the next instruction is ja .L5. Signed or unsigned, and when is the jump taken?

Unsigned (ja = above): taken when eax is greater than 10 as an unsigned number, which includes every negative int.

2. What does lea eax, [rdi+rdi*4] compute, and does it read memory?

eax = edi * 5 (low 32 bits); no memory access.

3. In the nasm program, change the third pair to 1, -1. Predict the six values, then run it.

1 - (-1) = 2: ZF=0, SF=0, OF=0. As unsigned, 1 is below 0xFFFF...FFFF, so CF=1 and jb=1; as signed 1 is not less than −1, so jl=0.

Test yourself

Which jump treats the comparison as unsigned?

  1. jl
  2. jg
  3. jb
  4. jle

What does xor eax, eax do in compiled code?

  1. Encrypts eax
  2. Sets rax to zero (all 64 bits, because a 32-bit write clears the upper half)
  3. Swaps eax with itself
  4. Tests eax for zero

max(a, b) compiled at -O2 has no jump. Which instruction replaces the branch?

  1. cmovge
  2. lea
  3. test
  4. sar

lea rax, [rdi+rsi*8] in a function taking long *a, long i. What is computed?

  1. The value a[i]
  2. The address &a[i], without reading memory
  3. a + i
  4. i * 8 only