Control Flow

Polished on 8 September 2025 (First written in May 2025)


Control flow helps in breaking the linearity of execution in assembly.

These three high level constructs in C are all implemented using jump statements.

  1. If else ladder: if, else if and else.
  2. Iteration: for, while and do-while loops.
  3. Switch case.

Jump Statements

  1. Unconditional jumps are the jumps that are certain to happen. Nothing can stop them. jmp mnemonic is used for this.
  2. Conditional jumps are based on a conditions. The cmp mnemonic helps in comparing two entities.
  3. The comparison instruction affects certain CPU Flags which the jump statements use to make their decision of jumping.
  4. The jump is made to a code symbol.

Comparison Operation

cmp compares two operands by subtracting the second operand from the first operand.

cmp op1, op2
=> op1 - op2

The result is used to set certain CPU flags. There is a special purpose register called RFLAGS in x64 and EFLAGS in x86 which holds these flags. And other architectures have similar mechanisms.

  • Note: cmp is just one instruction that changed RFLAGS; there are other operations that set them as well.
  • Although RFLAGS is a 64-bit wide register, the total CPU flags aren’t 64. Most of the bits are reserved by the CPU for internal things. The most common ones include: CF ZF OF PF AF SF TF .
FlagBit PlaceMeaning (Set When)How it’s SetAssumption
CF(Carry Flag)0Unsigned overflow (carry/borrow out of MSB)From final carry out of the adder/subtractor
PF(Parity Flag)2Result has even parity in low 8 bits1 if least-significant byte of result has even number of 1s
AF(Auxiliary Carry)4Carry out from bit 3 → bit 4Set if lower nibble overflowed
ZF(Zero Flag)6Result is zeroSet if result == 0Both the operands are same.
SF(Sign Flag)7Sign of result (MSB)Set to MSB of result
OF(Overflow Flag)11Signed overflow occurredSet if (carry into MSB) ≠ (carry out of MSB)

Based on these CPU flags the conditional jump statements make an assumption about the result of comparison.


Unsigned Comparison

InstructionHigh-level EquivalentMeaningFlags TestedCondition (Flag State)
JE / JZ==Jump if equal / zeroZFZF = 1
JNE / JNZ!=Jump if not equal / not zeroZFZF = 0
JB / JC / JNAE<Jump if below / carry / not above or equalCFCF = 1
JAE / JNB / JNC>=Jump if above or equal / not below / not carryCFCF = 0
JA>Jump if above (strictly greater, unsigned)CF, ZF(CF = 0) AND (ZF = 0)
JBE<=Jump if below or equal (unsigned)CF, ZF(CF = 1) OR (ZF = 1)

Since unsigned integers uses full set of bits available to store the magnitude of the value, we don’t require other flags.

Signed Comparison

InstructionHigh-level EquivalentMeaningFlags TestedCondition (Flag State)
JE / JZ==Jump if equal / zeroZFZF = 1
JNE / JNZ!=Jump if not equal / not zeroZFZF = 0
JL / JNGE<Jump if less (signed)SF, OFSF ≠ OF
JGE / JNL>=Jump if greater or equal (signed)SF, OFSF = OF
JG / JNLE>Jump if greater (signed)ZF, SF, OF(ZF = 0) AND (SF = OF)
JLE / JNG<=Jump if less or equal (signed)ZF, SF, OF(ZF = 1) OR (SF ≠ OF)

Last updated on