In assembly language, jumps based on a specific condition allow you to alter the flow of a program depending on the result of an operation. These conditional jumps are typically used after a comparison operation (CMP) or any arithmetic/logical operation that affects the flags in the EFLAGS register. Depending on the state of the flags (such as the Zero Flag (ZF), Carry Flag (CF), Sign Flag (SF), etc.), you can control the program's execution path.
Conditional jumps are instructions that cause a jump to a specific label if a certain condition (based on the processor's flags) is met. The key flags used for these jumps are set by the previous instructions and dictate the flow.
Here’s a breakdown of common conditional jumps based on specific conditions:
JE or JZ)CMP, SUB, etc.) was zero. This indicates that two compared values are equal.JE label ; Jump if Zero Flag (ZF) is set (i.e., if the values are equal)
JZ label ; Same as JE
CMP AX, BX ; Compare AX and BX
JE equal ; Jump to "equal" if AX == BX (ZF = 1)
MOV DX, 'Not Equal'
RET
equal:
MOV DX, 'Equal'
RET
Here, CMP AX, BX compares the contents of AX and BX. If they are equal, the Zero Flag (ZF) is set, and the program will jump to the equal label.
JNE or JNZ)JNE label ; Jump if Zero Flag (ZF) is clear (i.e., if the values are not equal)
JNZ label ; Same as JNE
CMP AX, BX ; Compare AX and BX
JNE not_equal ; Jump to "not_equal" if AX != BX (ZF = 0)
MOV DX, 'Equal'
RET
not_equal:
MOV DX, 'Not Equal'
RET
In this example, the program jumps to not_equal if the values in AX and BX are different (i.e., ZF = 0).
JC)JC label ; Jump if Carry Flag (CF) is set (i.e., there was an unsigned overflow or borrow)
CMP AX, BX ; Compare AX and BX (sets flags)
JC carry_set ; Jump to "carry_set" if there was a carry (CF = 1)
MOV DX, 'No Carry'
RET
carry_set:
MOV DX, 'Carry Occurred'
RET
Here, the CMP instruction compares AX and BX, and the jump will happen if there is a carry flag set (i.e., an unsigned overflow or borrow).
JNC)JNC label ; Jump if Carry Flag (CF) is clear (i.e., no carry occurred)
CMP AX, BX ; Compare AX and BX (sets flags)
JNC no_carry ; Jump to "no_carry" if no carry occurred (CF = 0)
MOV DX, 'Carry Occurred'
RET
no_carry:
MOV DX, 'No Carry'
RET
This example demonstrates how to check if there was no carry after a comparison.
JS)JS label ; Jump if Sign Flag (SF) is set (i.e., the result was negative)
CMP AX, BX ; Compare AX and BX (sets flags)
JS negative ; Jump to "negative" if AX < BX (SF = 1)
MOV DX, 'Positive or Zero'
RET
negative:
MOV DX, 'Negative Result'
RET
Here, the jump to negative occurs if the comparison shows that AX is less than BX (i.e., SF = 1 after a signed comparison).
JNS)JNS label ; Jump if Sign Flag (SF) is clear (i.e., the result was non-negative)
CMP AX, BX ; Compare AX and BX (sets flags)
JNS positive_or_zero ; Jump if AX >= BX (SF = 0)
MOV DX, 'Negative Result'
RET
positive_or_zero:
MOV DX, 'Positive or Zero'
RET
This example shows how to check for non-negative results in signed comparisons.
JO)JO label ; Jump if Overflow Flag (OF) is set (i.e., signed overflow occurred)
CMP AX, BX ; Compare AX and BX (sets flags)
JO overflow ; Jump to "overflow" if signed overflow occurred (OF = 1)
MOV DX, 'No Overflow'
RET
overflow:
MOV DX, 'Overflow Occurred'
RET
Here, the program jumps to overflow if there was a signed overflow in the comparison.
JNO)JNO label ; Jump if Overflow Flag (OF) is clear (i.e., no signed overflow)
CMP AX, BX ; Compare AX and BX (sets flags)
JNO no_overflow ; Jump to "no_overflow" if no signed overflow (OF = 0)
MOV DX, 'Overflow Detected'
RET
no_overflow:
MOV DX, 'No Overflow'
RET
In this case, the program jumps to no_overflow if there was no overflow in the signed comparison.
| Jump Instruction | Condition | Flag Checked | Meaning |
|---|---|---|---|
JE / JZ |
Equal | ZF (Zero Flag) | Jump if the two values are equal |
JNE / JNZ |
Not Equal | ZF (Zero Flag) | Jump if the two values are not equal |
JC |
Carry | CF (Carry Flag) | Jump if there was a carry (unsigned) |
JNC |
No Carry | CF (Carry Flag) | Jump if there was no carry (unsigned) |
JS |
Negative | SF (Sign Flag) | Jump if the result was negative |
JNS |
Positive or Zero | SF (Sign Flag) | Jump if the result was non-negative |
JO |
Overflow | OF (Overflow Flag) | Jump if there was signed overflow |
JNO |
No Overflow | OF (Overflow Flag) | Jump if there was no signed overflow |
section .data
msg db 'Comparison Result: ', 0
section .text
MOV AX, 5
MOV BX, 10
CMP AX, BX ; Compare AX and BX
JE equal ; Jump if equal (ZF = 1)
JNE not_equal ; Jump if not equal (ZF =
Open this section to load past papers