Skip to main content
Be Bitwise

Control Flow Reconstruction

Reverse Engineering Basics · Reverse Engineering track

4 min read

In this module

Seeing the Big Picture

Individual instructions tell you what's happening on each line. Understanding a function takes more: you need its control flow, the routes execution can take through the code. Compilers shred high-level if, while, for, and switch statements into sequences of comparisons and jumps. Putting them back together from assembly is core reverse-engineering muscle.

If/Else in Assembly

An if/else statement compiles down to a comparison plus a conditional jump. Compilers usually emit the "true" branch first, with a conditional jump leaping past it when the condition fails:

asm
cmp eax, 5 ; if (x == 5)
jne else_branch ; jump to else if not equal
; ... true branch code ...
jmp end_if ; skip the else branch
else_branch:
; ... false branch code ...
end_if:

That diamond, one entry point splitting into two paths that meet again, is the unmistakable shape of an if/else in a control flow graph.

Here's the snippet above cut into basic blocks. Each box ends at a jump or where a label starts, and the edges are the only ways execution can move between them:

Loops

Loops show up as backward jumps: a jump instruction whose target sits at an earlier address. Written out naively, a while loop puts the condition check up top with a conditional jump skipping the body. Compilers rarely leave it like that: GCC either jumps to a test at the bottom (as at -O0, below) or, from -O1 up, rotates the loop into one guard check before entry plus a do/while-style test at the bottom, so a while and a do/while can look almost identical. A do/while flips that around, with the check at the bottom and a backward conditional jump back to the start. A for loop is a while loop with an initialiser bolted on the front and an increment tacked on the back.

The recognition trick: find a backward edge (jump to a lower address) and you've found a loop. The instruction right before that backward jump is almost always the loop condition.

A typical -O0 for (i = 0; i < n; i++) as a graph. GCC jumps forward to the test first, then keeps the test at the bottom so each pass only needs one branch. The edge from the test back up to the body is the backward edge that gives the loop away:

Control Flow Graphs

A control flow graph (CFG) divides code into basic blocks - sequences of instructions with no branches except at the end. Each block is a node, and the jumps between them are edges. Tools like IDA and Ghidra build CFGs automatically and display them visually. Loops appear as cycles in the graph, if/else as diamonds, and switch statements as a single node with many outgoing edges.

Switch Statements

Compilers have two main strategies for switch. Dense cases (think 0-10) get a jump table: an array of addresses, one per case value. The code first subtracts the lowest case value and does an unsigned bounds check (cmp eax, 10 / ja default), then indexes the table and jumps. Sparse cases (1, 100, 10000) become comparisons instead: a short chain of cmp/je pairs for a few cases, or for many cases a binary search, a tree of cmp with jg/jl splitting the range in half at each step. Compilers mix the two, too, with a binary search choosing between several small jump tables.

The jump-table tell is distinctive once you've seen it: an indexed memory access like jmp [rax*8 + table_base], where table_base points to a block of addresses. Position-independent x86-64 code usually stores 32-bit offsets instead, so you'll see movsxd rax, [rdx+rax*4], add rax, rdx, jmp rax.

Tools of the Trade

Modern disassemblers do most of this work for you. IDA Pro is the long-standing industry favourite, prized for accurate analysis and a sprawling plugin ecosystem. Ghidra, released by the NSA, ships with a capable decompiler that lifts assembly back to C-like pseudocode and costs nothing. Binary Ninja brings a clean interface and an intermediate language that smooths out analysis. All three build CFGs, recover functions, and recognise common patterns. Pick whichever one fits your workflow; the underlying skills carry across.