Branching in Assembly Language involves altering the flow of execution based on conditions,
looping, or jumping to different parts of the code. Branching instructions are vital for
implementing decision-making structures like if-else, while, for loops, and function calls.
In x86 Assembly Language, branching is achieved with jump instructions (conditional and
unconditional). Here’s a breakdown of how branching works.
1. Unconditional Jump (JMP)
The JMP instruction allows you to jump to a different part of the program unconditionally,
meaning the jump always occurs regardless of any condition.
2. Conditional Jumps
Conditional jumps occur based on the state of flags in the FLAGS register (such as Zero, Carry,
Sign, Overflow, etc.) set by previous arithmetic or logical instructions. Conditional jumps are
commonly used to implement branching logic like if-else statements or loops.
Common conditional jump instructions include:
JE / JZ (Jump if Equal / Zero): Jump if the result of the last comparison was zero (i.e.,
equal).
JNE / JNZ (Jump if Not Equal / Not Zero): Jump if the result of the last comparison was
not zero.
JG / JNLE (Jump if Greater): Jump if greater (for signed comparisons).
JGE / JNL (Jump if Greater or Equal): Jump if greater or equal.
JL / JNGE (Jump if Less): Jump if less.
JLE / JNG (Jump if Less or Equal): Jump if less or equal.
JA / JNBE (Jump if Above): Jump if above (for unsigned comparisons).
JAE / JNB (Jump if Above or Equal): Jump if above or equal.
JB / JNAE (Jump if Below): Jump if below.