In assembly language, conditional jumps allow you to alter the flow of a program depending on the state of certain processor flags. These flags are set by various arithmetic and logical instructions (such as CMP, ADD, SUB, etc.) and represent the results of the most recent operation. Depending on the condition, you can use different jump instructions to control the program’s execution flow.
The most commonly used flags for conditional jumps are part of the EFLAGS register (in x86 and x86-64 systems). These flags reflect the result of the most recent arithmetic or logical operation.
Here are the key flags:
These flags are automatically set by operations like ADD, SUB, CMP, MUL, DIV, etc. Conditional jumps use these flags to make decisions based on the outcome of the last operation.
In assembly, the conditional jump instructions typically test a specific flag, and if the condition is met, they jump to the specified label. Here’s a list of common jump instructions based on specific flags:
JE (Jump if Equal) / JZ (Jump if Zero):
CMP AX, BX ; Compare AX and BX (sets the flags)
JE equal ; Jump to "equal" if AX == BX (ZF = 1)
; Other code
This instruction is used after a CMP (compare) instruction to check if two values are equal.
JNE (Jump if Not Equal) / JNZ (Jump if Not Zero):
CMP AX, BX ; Compare AX and BX (sets the flags)
JNE not_equal ; Jump to "not_equal" if AX != BX (ZF = 0)
; Other code
This instruction is used to jump when two values are not equal.
JC (Jump if Carry):
CMP AX, BX ; Compare AX and BX (sets the flags)
JC carry ; Jump if carry (CF = 1)
JNC (Jump if No Carry):
CMP AX, BX ; Compare AX and BX (sets the flags)
JNC no_carry ; Jump if no carry (CF = 0)
These instructions are typically used when performing unsigned arithmetic operations.
JS (Jump if Sign):
CMP AX, BX ; Compare AX and BX (sets the flags)
JS negative ; Jump if the result was negative (SF = 1)
JNS (Jump if No Sign):
CMP AX, BX ; Compare AX and BX (sets the flags)
JNS positive ; Jump if the result was positive (SF = 0)
These instructions are used in signed arithmetic operations to test whether the result is positive or negative.
JO (Jump if Overflow):
CMP AX, BX ; Compare AX and BX (sets the flags)
JO overflow ; Jump if overflow (OF = 1)
JNO (Jump if No Overflow):
CMP AX, BX ; Compare AX and BX (sets the flags)
JNO no_overflow ; Jump if no overflow (OF = 0)
These instructions are typically used for signed arithmetic operations to handle overflows.
JP (Jump if Parity):
CMP AX, BX ; Compare AX and BX (sets the flags)
JP even_parity ; Jump if parity is even (PF = 1)
JNP (Jump if No Parity):
CMP AX, BX ; Compare AX and BX (sets the flags)
JNP no_parity ; Jump if parity is odd (PF = 0)
Parity is often used in error checking and communication protocols.
JL (Jump if Less):
CMP AX, BX ; Compare AX and BX (sets the flags)
JL less ; Jump if AX < BX (signed comparison)
JGE (Jump if Greater or Equal):
CMP AX, BX ; Compare AX and BX (sets the flags)
JGE greater_or_equal ; Jump if AX >= BX (signed comparison)
These jumps are commonly used for signed comparisons between two values.
JB (Jump if Below):
CMP AX, BX ; Compare AX and BX (sets the flags)
JB below ; Jump if AX < BX (unsigned comparison)
JAE (Jump if Above or Equal):
CMP AX, BX ; Compare AX and BX (sets the flags)
JAE above_or_equal ; Jump if AX >= BX (unsigned comparison)
section .data
num1 dw 10
num2 dw 5
section .text
MOV AX, [num1] ; Load AX with num1 (10)
MOV BX, [num2] ; Load BX with num2 (5)
CMP AX, BX ; Compare AX and BX (sets flags)
JE equal ; Jump to "equal" if AX == BX (ZF = 1)
JNE not_equal ; Jump to "not_equal" if AX != BX (ZF = 0)
JC carry ; Jump to "carry" if there was a carry (CF = 1)
JNC no_carry ; Jump to "no_carry" if no carry (CF = 0)
JS negative ; Jump to "negative" if result was negative (SF = 1)
JNS positive ; Jump to "positive" if result was positive (SF = 0)
equal:
; Code for when AX == BX
MOV DX,
Open this section to load past papers