Everything so far has run straight down the page, one instruction after the next. Control flow is how a program decides what to run instead — the if, the while, the function call. At the machine level it always comes in two steps: an instruction that inspects some data and records the result in the flags, and a jump that reads those flags and either moves execution elsewhere or lets it fall through.
cmp and testTwo instructions exist purely to set flags. Neither stores a result:
cmp a, b computes a - b exactly like sub, sets the flags from it, and discards the difference. It answers "how does a relate to b?" If they are equal the subtraction is zero, so ZF is set; if a < b it borrows, and so on.test a, b computes a & b like and, sets the flags, and discards the result. Its everyday form is test rax, rax, which sets ZF when rax is zero (covered in Bitwise Ops).The flags they leave behind — ZF, SF, CF, OF from Arithmetic — are the entire channel between the comparison and the jump that follows it.
A conditional jump takes the branch only when the flags describe its condition; otherwise execution falls through to the next instruction. They almost always sit right after a cmp:
| Jump | Branch taken when | Signedness |
|---|---|---|
je / jz | equal (ZF = 1) | either |
jne / jnz | not equal (ZF = 0) | either |
jl / jle | less / less-or-equal | signed |
jg / jge | greater / greater-or-equal | signed |
jb / jbe | below / below-or-equal | unsigned |
ja / jae | above / above-or-equal | unsigned |
There are two families for the same comparison for the same reason there is one add for signed and unsigned (from Arithmetic): the bits are identical, so you choose the interpretation by picking the jump. The mnemonics encode which: less/greater are the signed words (they read SF, OF, and ZF), below/above are the unsigned ones (they read CF and ZF). Comparing two values as signed with jl when they are really unsigned — or vice versa — is a classic bug.
jmp always branches, no flags consulted. It is the glue of control flow: the jump at the bottom of a loop that goes back to the top, or the one that skips over an else block. Its target is usually a fixed nearby address, but jmp can also take a register or memory operand — an indirect jump — which is how switch tables and function pointers work, and a mechanism the Binary Exploitation course examines closely.
A loop is nothing more than these pieces assembled: set up a counter, run the body, update the counter, cmp it against the limit, and conditionally jump back. This C —
1int sum = 0;
2for (int i = 1; i <= 5; i++)
3 sum — compiles to the same shape by hand. Assembled and disassembled, the structure is plain to see:
objdump -d -M intel loop0000000000401000 <_start>: 401000: 48 31 c0 xor rax,rax 401003: b9 01 00 00 00 mov ecx,0x10000000000401008 <_start.loop>: 401008: 48 01 c8 add rax,rcx 40100b: 48 ff c1 inc rcx 40100e: 48 83 f9 05 cmp rcx,0x5 401012: 7e f4 jle 401008 <_start.loop> 401014: 48 89 c7 mov rdi,rax 401017: b8 3c 00 00 00 mov eax,0x3c 40101c: 0f 05 syscallrax is the running sum (started at zero with the xor idiom), rcx is i. Each pass adds rcx to the sum, increments it, compares it with 5, and jle jumps backward to 0x401008 while i ≤ 5. When rcx reaches 6 the comparison fails, the jump is skipped, and execution falls through to the exit. The telltale sign of a loop in any disassembly is exactly this: a conditional jump whose target is a lower address than itself. The program hands its result back as the process exit code, which the shell can print:
./loop; echo $?1515 is 1 + 2 + 3 + 4 + 5 — the loop ran its five passes and summed them.