Comparisons and Jumps
High-level languages hand you if, else, while, for. Assembly has none of that. What you get is two primitives: compare two values (setting flags), and jump to a different instruction based on those flags. Every control-flow construct you've ever written compiles down to those two moves.
For additional context on how the x86 instruction set handles branching, see x86 Instruction Set.
The CMP Instruction (Review)
CMP subtracts the second operand from the first, sets the flags, and discards the result:
cmp rax, 10 ; compute rax - 10, set flags, discard resultcmp rdi, rsi ; compute rdi - rsi, set flags, discard resultcmp byte [rbx], 0 ; compare byte at address rbx with 0After CMP a, b, the flags hold the relationship between a and b. A conditional jump reads them and acts.
Unconditional Jump
JMP ships execution to a new address, no questions asked:
jmp .target ; always jump to label .targetjmp rax ; jump to the address stored in rax (indirect jump)jmp [rax] ; jump to the address stored at memory location raxA leading dot (.target) marks a NASM local label. Its scope ends at the next non-local label above it.
Conditional Jumps
Conditional jumps key off specific flag combinations. They come in matched pairs for signed and unsigned comparisons:
Equality (works for both signed and unsigned)
je .label ; Jump if Equal (ZF = 1)jz .label ; Jump if Zero (ZF = 1) -- same as JEjne .label ; Jump if Not Equal (ZF = 0)jnz .label ; Jump if Not Zero (ZF = 0) -- same as JNEUnsigned Comparisons (Above/Below)
Reach for these after comparing unsigned values: addresses, sizes, indices.
ja .label ; Jump if Above (CF=0 AND ZF=0)jae .label ; Jump if Above or Equal (CF=0)jb .label ; Jump if Below (CF=1)jbe .label ; Jump if Below or Equal (CF=1 OR ZF=1)Signed Comparisons (Greater/Less)
Reach for these after comparing signed values: temperatures, offsets, error codes.
jg .label ; Jump if Greater (ZF=0 AND SF=OF)jge .label ; Jump if Greater or Equal (SF=OF)jl .label ; Jump if Less (SF != OF)jle .label ; Jump if Less or Equal (ZF=1 OR SF!=OF)Other Useful Jumps
jo .label ; Jump if Overflow (OF=1)jno .label ; Jump if No Overflow (OF=0)js .label ; Jump if Sign (negative) (SF=1)jns .label ; Jump if No Sign (positive/zero) (SF=0)jc .label ; Jump if Carry (CF=1) -- same as JBjnc .label ; Jump if No Carry (CF=0) -- same as JAEUsing the wrong comparison family is a dangerous bug. Consider comparing the byte 0xFF with 0x01. As unsigned values, 255 > 1, so JA (jump if above) would be taken. As signed values, -1 < 1, so JG (jump if greater) would NOT be taken. If a bounds check uses signed comparison on an unsigned size value, an attacker can supply a negative number that passes the check but wraps to a huge positive number when used as an index. This is a real vulnerability pattern.
Here's that pattern with real numbers. A packet header supplies a 32-bit length in eax, the buffer holds 64 bytes, and the attacker sends 0xFFFFFFFF. Two versions of the check:
; Version A cmp eax, 64 jg .reject; Version B cmp eax, 64 ja .reject; ...then copy eax bytes into the 64-byte bufferWhich version rejects the packet?
Only version B. Read as unsigned, 0xFFFFFFFF is 4,294,967,295, far above 64, so ja jumps to .reject. Read as signed it's -1, which isn't greater than 64, so version A falls through and goes on to copy four gigabytes into a 64-byte buffer. A length is never negative, so it wants the unsigned family.
Building If/Else
A small if/else lowered into assembly:
// C codeif (x == 5) { result = 10;} else { result = 20;}; Assembly equivalent (x is in rdi, result goes in rax) cmp rdi, 5 jne .else_branch ; if x != 5, skip to else
; if body mov rax, 10 jmp .end_if ; skip the else body
.else_branch: ; else body mov rax, 20
.end_if: ; continue execution...The pattern: compare, jump over the if-body when the condition is false (note that's inverted from the C source), then jump over the else-body at the end of the if-body.
Drawn as basic blocks, that code is a diamond. This is the shape a disassembler's graph view shows you, and learning to read it backwards is most of reverse engineering. Note which edge is the taken jump: it's the else side, because the condition was inverted.
Chained Conditions (If / Else If / Else)
// C codeif (x < 0) { result = -1;} else if (x == 0) { result = 0;} else { result = 1;}; x in rdi, result in rax cmp rdi, 0 jl .negative ; signed less than 0 je .zero ; equal to 0 jmp .positive ; otherwise positive
.negative: mov rax, -1 jmp .done
.zero: xor rax, rax ; rax = 0 jmp .done
.positive: mov rax, 1
.done:Compound Conditions
Logical AND and OR in conditions need more than one comparison apiece:
// C code: if (x >= 1 && x <= 100); AND: both conditions must be true cmp rdi, 1 jl .outside ; if x < 1, skip (condition fails) cmp rdi, 100 jg .outside ; if x > 100, skip (condition fails) ; Both conditions passed -- x is in range [1, 100] jmp .in_range
.outside: ; handle out-of-range case jmp .done
.in_range: ; handle in-range case
.done:// C code: if (x == 0 || x == 1); OR: either condition can be true cmp rdi, 0 je .matched ; first condition true, skip to body cmp rdi, 1 je .matched ; second condition true, skip to body jmp .not_matched ; neither condition true
.matched: ; handle matched case jmp .done
.not_matched: ; handle not-matched case
.done:When a program takes different branch paths based on secret data (like a password comparison), the execution time can vary. An attacker who measures this timing can infer the secret one bit at a time. For example, a naive byte-by-byte string comparison exits early on the first mismatch - the attacker can tell how many bytes matched by how long the comparison took. This is why cryptographic comparison functions must be constant-time: they always execute the same number of instructions regardless of input, typically using XOR and OR to accumulate differences without branching.
Near vs Short Jumps
Jumps come in two encoding sizes:
- Short jump (
jmp short .label): target within -128 to +127 bytes, encoded in 2 bytes. - Near jump (
jmp near .label): target within +/- 2GB, encoded in 5 bytes for unconditional, 6 for conditional.
NASM picks the right encoding for you most of the time. In hand-written shellcode, where each byte costs you, you might pin it to short jumps yourself:
jmp short .nearby ; force 2-byte encodingPractical Example: Absolute Value
; Compute absolute value of rdi, store in rax; if rdi >= 0, rax = rdi; else rax = -rdi mov rax, rdi test rax, rax ; check sign jns .done ; if non-negative, we are done neg rax ; negate to make positive.done: ; rax now holds |rdi|Practical Example: Clamp to Range
; Clamp rdi to the range [0, 255], result in rax xor rax, rax ; rax = 0 (minimum) cmp rdi, 0 jl .done ; if rdi < 0, return 0
mov rax, 255 cmp rdi, 255 jg .done ; if rdi > 255, return 255
mov rax, rdi ; in range, return rdi.done:What is Next
With comparisons and jumps in hand, the next lesson knits them into loops — assembly's answer to while, for, and do-while.