806 words
4 minutes
FLARE Learning Hub - Chapter 5: Conditional Branching

Chapter 5 of “Malware Analysis Crash Course” (FLARE/Mandiant) is, to me, the most important chapter so far — because almost the entire “logic” of a program (if/else, loops, switch) ultimately comes down to conditional branching at the assembly level. Assembly genuinely has no real concept of if/else — the compiler turns it into a pair of a comparison plus a conditional jump.

mandiant
/
flare-learning-hub
Waiting for api.github.com...
00K
0K
0K
Waiting...

Comparison Instructions: CMP and TEST#

Neither of these writes a result anywhere or modifies either operand — they silently update EFLAGS so that the conditional jump instructions following them can make a decision based on it.

  • CMP a, b — internally computes a - b (a subtraction, result discarded), keeping only the flags. The Zero Flag (ZF) gets set if the result is 0 (i.e. a == b).
  • TEST a, b — internally computes a AND b (a bitwise AND, result also discarded). An extremely common pattern: test reg, reg (ANDing a register with itself) — ZF is only set when that register equals 0, commonly used to check for a NULL pointer.
cmp eax, ebx         ; internally computes EAX - EBX, EAX/EBX unchanged
test ecx, ecx         ; internally computes ECX AND ECX, ZF=1 if ECX == 0

Common Conditional Jumps#

Split into 2 distinct groups — signed and unsigned — because “greater than/less than” carries the same meaning but interprets the sign bit differently, so there are two completely separate sets of instructions:

Signed group (signed number comparisons):

InstructionMeaning
JL<
JLE≤
JG>
JGE≥

Unsigned group (unsigned number comparisons):

InstructionMeaning
JB<
JBE≤
JA>
JAE≥

Shared by both (only checking equal/not-equal, sign doesn’t matter):

InstructionMeaning
JZ / JE== (Zero Flag = 1)
JNZ / JNE!=

JZ/JE and JNZ/JNE are actually the exact same opcode, just displayed with different mnemonic names — the disassembler picks whichever name reads more naturally for the context.

A “Plain English” Trick for Reading Branches#

No need to memorize the EFLAGS mechanics in detail — just apply 2 steps:

  1. Translate the jump mnemonic into a comparison operator (JG → >)
  2. Read it as a sentence: “jump if [operand1] operator [operand2]”

Example: after cmp eax, 50 then jle somewhere → read it as “jump to somewhere if eax ≤ 50”. This approach completely skips having to understand exactly which flag gets set how — just translate the mnemonic directly into math.

If Statements — What the Compiler Generates#

For example, given source code if (a == b) { ... } else { ... }, the corresponding assembly typically looks like:

mov eax, [ebp-4]      ; load variable a
cmp eax, [ebp-8]       ; compare a with b
jnz short else_block    ; jump if DIFFERENT (note: the condition is INVERTED)
; --- if body (a == b) ---
call printf_equal
jmp short merge         ; jump over the else part
else_block:
; --- else body (a != b) ---
call printf_not_equal
merge:
; --- subsequent code ---

The most important thing to remember: the compiler typically inverts the condition in the jump instruction — instead of “jump if the condition is true”, it generates “jump if the condition is false” in order to skip the if body, letting execution fall naturally into the if body when the condition is true. An unconditional jmp at the end of the if body is used to jump entirely over the else part.

Static Analysis with IDA#

Static analysis means understanding a program without running it. When you open a binary, IDA automatically runs auto-analysis: disassembling everything, determining function boundaries, and pre-building the cross-reference tables — all done before you even get to interact with it.

The default interface includes:

  • Disassembly pane — code in linear or graph view
  • Functions list — every function IDA has identified, with metadata
  • Imports — functions imported from external DLLs
  • Exports — functions this binary itself exports
  • Strings — all strings already extracted
  • Navigation band — a color-coded overview bar of the entire address space

A practical workflow worth using: scan Imports for suspicious functions (file I/O, networking, etc.), then use xrefs (the X key) to find where they’re called — the fastest way to grasp program behavior without reading sequentially. The same applies to Strings — an unusual string (a URL, a path, an error message) traced via xref often leads straight to the most interesting routine.

Operations that “leave a trail” while analyzing (documenting directly inside the IDB):

  • Rename (N key) — rename a function/variable/address instead of leaving the default sub_401000
  • Regular comment (: key) — a comment at exactly one location
  • Repeatable comment (; key) — a comment that reappears at every location that references that spot
  • Repeatable function comment — a comment placed at the top of a function, which automatically shows up at every call site of that function

Documenting incrementally this way — renaming things, adding small comments bit by bit — is how a pile of hard-to-read assembly turns into logic you can understand at a higher level, instead of having to memorize every single address in your head.


Understanding conditional branching is the key to reading a program’s real logic — next chapter I’m moving to a completely different area: basic debugging techniques (forcing execution, patching).

FLARE Learning Hub - Chapter 5: Conditional Branching
https://stewmalwarehunter.id.vn/posts/flare-macc-ch5---conditional-branching-en/
Author
Stew
Published at
2026-09-05
License
CC BY-NC-SA 4.0