What Is a Conditional Jump?
A conditional jump is an instruction that transfers control to a different point in the program only if a specific condition, encoded in the CPU's FLAGS register, is true. Unlike a plain JMP which always branches, instructions such as JE (jump if equal), JNE (jump if not equal), JG (jump if greater), and JL (jump if less) inspect flags like ZF (zero flag), SF (sign flag), and OF (overflow flag) that were set by a preceding CMP or arithmetic instruction. This is how assembly implements the equivalent of an if statement: compare two values, then branch based on the result.
Cricket analogy: It's like a third umpire reviewing a run-out: the TV replay sets a 'flag' (out or not-out), and only then does the umpire raise the finger — the decision instruction depends entirely on the flag set by the prior review, exactly as JE depends on ZF set by CMP.
CMP and the Flags It Sets
CMP performs a subtraction internally (dest - src) but discards the numeric result, keeping only the flags. If the operands are equal, ZF is set to 1; if the destination is less than the source (signed), SF and OF end up different; if unsigned, CF (carry flag) tracks a borrow. This distinction between signed and unsigned comparison is critical: JG/JL rely on SF and OF for signed comparisons, while JA/JB (jump above/below) rely on CF and ZF for unsigned comparisons. Using the wrong pair for the data type is a classic source of subtle bugs, especially near the boundary between positive and negative numbers.
Cricket analogy: A DRS review computes the ball's trajectory internally but only reports 'out' or 'not out' to the umpire — CMP similarly computes dest-src internally but only exposes the flags, not the actual difference.
Signed vs. Unsigned Jump Families
x86 provides two parallel families of conditional jumps: the signed family (JG, JGE, JL, JLE) built from SF XOR OF, and the unsigned family (JA, JAE, JB, JBE) built from CF and ZF. Mnemonically, 'greater/less' implies signed comparison while 'above/below' implies unsigned comparison — a naming convention worth memorizing because mixing them up produces code that behaves correctly for small positive numbers but breaks the moment a value is negative or exceeds 0x7FFFFFFF as an unsigned quantity.
Cricket analogy: Net run rate calculations use signed differences (a team can be below zero), while overs bowled is an unsigned count that can never go negative — mixing the two formulas the way JG and JA get confused would produce nonsense standings.
section .text
global _start
_start:
mov eax, 10
mov ebx, 20
cmp eax, ebx ; sets flags based on eax - ebx
jg eax_is_bigger ; signed: jump if eax > ebx
jl ebx_is_bigger ; signed: jump if eax < ebx
je values_equal
ebx_is_bigger:
mov ecx, 1 ; result code: ebx wins
jmp done
eax_is_bigger:
mov ecx, 2 ; result code: eax wins
jmp done
values_equal:
mov ecx, 0
done:
mov eax, 1 ; sys_exit
mov ebx, ecx
int 0x80Conditional jumps must be preceded by an instruction that actually sets the relevant flags. If you insert an instruction between CMP and the jump that itself modifies flags (such as ADD, SUB, or INC), the jump will test the wrong condition — a bug that is invisible in the source but deterministic in the assembled binary.
Building if/else and switch-like Structures
High-level if/else statements compile down to a CMP followed by a conditional jump to an else-label, with an unconditional JMP at the end of the if-block to skip over the else-block. Chains of else-if become chains of CMP/Jcc pairs. A switch statement with dense, sequential case values is often compiled into a jump table — an array of addresses indexed by the switch variable, dereferenced with a single indirect JMP — while sparse cases fall back to a linear chain of CMP/JE comparisons, since a jump table would waste memory on unused slots.
Cricket analogy: A jump table is like a scoreboard operator with a pre-printed card for every possible dismissal type, instantly flipping to the right card, whereas a sparse switch is like checking each rule one by one for an unusual dismissal — mirroring dense vs. sparse switch-case compilation.
GCC and Clang typically emit a jump table when a switch statement has four or more consecutive, densely packed case values; sparse or few cases usually compile to a chain of compares instead. You can observe this directly by compiling with -O2 and inspecting the disassembly.
- Conditional jumps (JE, JNE, JG, JL, JA, JB, etc.) branch based on flags set by a prior CMP or arithmetic instruction.
- CMP performs dest - src internally but discards the numeric result, keeping only ZF, SF, OF, and CF.
- Signed comparisons (JG/JL family) use SF and OF; unsigned comparisons (JA/JB family) use CF and ZF — mixing them causes subtle bugs.
- An instruction that modifies flags between CMP and the jump will silently change which branch is taken.
- if/else compiles to CMP + Jcc + JMP; else-if chains compile to repeated CMP/Jcc pairs.
- Dense switch statements often compile to a jump table (indirect JMP through an address array) for O(1) dispatch.
- Sparse switch statements typically fall back to a linear chain of CMP/JE comparisons.
Practice what you learned
1. Which flag does JE (jump if equal) primarily test?
2. Which pair of jumps should be used for comparing two unsigned integers?
3. What does the CMP instruction actually do internally?
4. What is a common risk of placing an instruction like INC between a CMP and a conditional jump?
5. Why might a compiler use a jump table instead of a chain of CMP/JE instructions for a switch statement?
Was this page helpful?
You May Also Like
The Flags Register
Understand the x86 FLAGS register — the bit fields like ZF, CF, SF, and OF that record the outcome of arithmetic and comparison instructions.
Loops in Assembly
Discover how high-level for and while loops are built from conditional jumps, counters, and the dedicated LOOP instruction in x86 assembly.
Procedures and Calling Conventions
Learn how assembly functions are called, how arguments and return values are passed, and how the stack frame is built and torn down.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics