Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Assembly and Reverse Engineering Basics

Control Flow in Disassembly: Branches, Loops and Graphs

Recognise if/else, loops, switches and function calls in assembly, read control flow graphs, and rebuild basic blocks with a runnable Python stand-in.

High-level languages give you if, while, for and switch. The CPU has none of them. It has compares and jumps, and compilers turn structured code into patterns of those. Learning to see the original structure behind the jumps is what lets you read a 200-line function in two minutes instead of two hours. A disassembler helps by drawing a control flow graph (CFG): boxes of straight-line code connected by arrows for the possible jumps.

All listings below are small, made-up examples. The runnable stand-ins split a listing into basic blocks and walk the graph in Python; they model what the disassembler does, and are not one.

From structure to jumps

if / else Compare, then jump over a block.
Loops A jump backward to the top.
switch A table of jump targets.

Look for the compare, then the jump.

1 / 2

Compare and jump

A decision is nearly always two instructions: one that sets flags (cmp or test) and a conditional jump that reads them. The conditional jumps come in pairs that read naturally once you know the vocabulary:

Jump Reads as Flags
jz / je jump if zero / equal ZF = 1
jnz / jne jump if not zero / not equal ZF = 0
jl, jg less / greater (signed) SF, OF, ZF
jb, ja below / above (unsigned) CF, ZF
jmp always none

Signed and unsigned variants differ, so jl versus jb hints at the type of the compared values. The classic test of a function's result is test eax, eax followed by jz or jnz: it asks "did the call return zero?" Many Windows API calls return zero on failure, so call X; test eax, eax; jz error means "if X failed, go to the error handler".

if and if/else

A compiler usually generates the inverse jump: the condition for skipping the body.

TEXT
cmp   dword ptr [ebp-4], 10     ; if (count > 10)
jle   skip                      ;   jump over the body when count <= 10
call  send_report               ;   body
skip:

For if/else there are two blocks and an unconditional jmp at the end of the first to hop over the second:

TEXT
cmp   eax, 0
jne   else_part
call  handle_zero
jmp   done
else_part:
call  handle_other
done:

Loops

A loop is recognisable because a jump goes backward, to a lower address. A for loop has an initialiser, a test and an increment; compilers place the test at the top or the bottom:

TEXT
xor   ecx, ecx               ; i = 0
top:
cmp   ecx, 16                ; i < 16 ?
jge   end
mov   al, [esi+ecx]          ; read byte i
xor   al, 0x5a               ; transform it
mov   [edi+ecx], al          ; write byte i
inc   ecx                    ; i++
jmp   top
end:

This particular shape, a counter walking a buffer and applying xor with a constant, is the signature of a simple decoding loop. You will meet it again in string obfuscation. The graph view shows it immediately as a box with a back-edge.

switch statements and jump tables

A switch with many cases is often compiled to a jump table: an array of code addresses indexed by the value.

TEXT
cmp   eax, 5                 ; bounds check
ja    default_case
jmp   dword ptr [table+eax*4] ; indirect jump through the table

A jump whose target is a register or memory slot is an indirect jump. Static tools sometimes cannot resolve it, leaving gaps in the graph; analysts then work out the table by hand. Malware command handlers (case 1: upload; case 2: download; ...) are commonly written this way, so a jump table in a function that reads data from the network is a strong clue.

Function calls in the graph

Direct calls (call 0x401230) name their target. Indirect calls (call eax, call [ebx+10h]) do not, and are used for imported functions, C++ virtual methods and callbacks. A function that resolves API addresses at runtime and then calls them indirectly hides its capabilities from the import table, which is a theme of packers and obfuscation.

Basic blocks, in code

A basic block is a run of instructions with one entry and one exit. A new block starts at every jump target and after every jump. This stand-in finds the blocks of a tiny listing and prints the edges, the same information a CFG draws.

Python Split a listing into basic blocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output shows four blocks. The block that begins at the cmp has two outgoing edges, one to the exit and one falling into the body, and the body block jumps back to the top: a loop in graph form.

Walking the graph to find a loop

The second stand-in takes a CFG as a dictionary and uses a depth-first search to find back-edges, edges that return to a block already on the current path. Back-edges mean loops, and loops are where decoders, scanners and encryption routines live.

Python Find loops with a depth-first search

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real analysis frameworks apply the same idea at scale. When you open a large function, scanning the graph for back-edges and for blocks with many incoming arrows quickly shows you the interesting regions.

Rename blocks and functions as you learn what they do (decode_loop, parse_command). A graph full of loc_401A2C labels is unreadable; a graph with names tells a story.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How can you spot a loop in a control flow graph?

    A back-edge, a jump to a lower address or an earlier block, is the signature of a loop.
  2. 2. What does call X followed by test eax, eax and jz error usually mean?

    test eax, eax sets ZF when eax is zero, and many Windows API calls return zero on failure.
  3. 3. Why does a compiler usually jump when an if condition is FALSE?

    The body sits right after the compare, so the generated jump skips it when the condition does not hold.
  4. 4. What is an indirect jump through a table indexed by a value typically used for?

    Dense switch statements compile to jump tables, such as command handlers.

Summary

  • Decisions are a flag-setting instruction (cmp, test) plus a conditional jump.
  • Compilers often jump on the inverse condition to skip the body; if/else adds an unconditional jmp.
  • Loops show up as backward jumps or graph back-edges; a counter walking a buffer with xor suggests decoding.
  • Jump tables implement big switch statements, such as command dispatchers.
  • Basic blocks and edges are what the CFG displays; name them as you understand them.
  • The block-splitting and loop-finding code is a simplified stand-in for disassembler logic.

Next: Packers and Protectors