Here are some practical examples demonstrating conditional jumps in assembly. We'll use the x86 assembly language with MASM-style syntax to show how these instructions work. These examples assume you are using a 16-bit or 32-bit x86 environment, but the concepts apply to both.
JE or JZ)This example shows how to jump if two values are equal. The CMP instruction compares two values, and if they are equal, the Zero Flag (ZF) is set, causing the program to jump to the equal label.
section .data
msg db 'Equal', 0
msg_not_equal db 'Not Equal', 0
section .text
; Assume AX = 5, BX = 5
MOV AX, 5
MOV BX, 5
CMP AX, BX ; Compare AX with BX
JE equal ; Jump to "equal" if AX == BX (ZF = 1)
; If not equal
MOV DX, msg_not_equal
RET
equal:
; If equal
MOV DX, msg
RET
MOV AX, 5 and MOV BX, 5 load both registers with the value 5.CMP AX, BX compares AX with BX. Since the values are equal, the Zero Flag (ZF) will be set to 1.JE equal will jump to the equal label because ZF = 1 (indicating the two values are equal).JNE or JNZ)This example shows how to jump if two values are not equal. The CMP instruction compares the two values, and if they are not equal, the Zero Flag (ZF) is cleared, causing the program to jump to the not_equal label.
section .data
msg db 'Equal', 0
msg_not_equal db 'Not Equal', 0
section .text
; Assume AX = 5, BX = 10
MOV AX, 5
MOV BX, 10
CMP AX, BX ; Compare AX with BX
JNE not_equal ; Jump to "not_equal" if AX != BX (ZF = 0)
; If equal
MOV DX, msg
RET
not_equal:
; If not equal
MOV DX, msg_not_equal
RET
MOV AX, 5 and MOV BX, 10 load AX with 5 and BX with 10.CMP AX, BX compares AX with BX. Since the values are different, the Zero Flag (ZF) is cleared (ZF = 0).JNE not_equal will jump to the not_equal label because ZF = 0 (indicating the values are not equal).JC)This example demonstrates jumping if there was a carry. A carry can occur after an addition or subtraction that results in an overflow, but for simplicity, we'll use a subtraction.
section .data
msg db 'Carry Detected', 0
msg_no_carry db 'No Carry', 0
section .text
; Assume AX = 10, BX = 20
MOV AX, 10
MOV BX, 20
CMP AX, BX ; Compare AX and BX
JC carry_detected ; Jump to "carry_detected" if carry (CF = 1)
; No carry
MOV DX, msg_no_carry
RET
carry_detected:
; Carry occurred
MOV DX, msg
RET
MOV AX, 10 and MOV BX, 20 load AX and BX with values 10 and 20, respectively.CMP AX, BX compares AX with BX. Since AX < BX, there will be a borrow (carry in unsigned terms).JC carry_detected will jump to carry_detected because the Carry Flag (CF) is set after the comparison.JO)This example demonstrates how to jump if there is an overflow. Overflow occurs in signed arithmetic operations when the result exceeds the maximum value that can be represented.
section .data
msg db 'Overflow Detected', 0
msg_no_overflow db 'No Overflow', 0
section .text
; Assume AX = 32767, BX = 1 (Overflow in signed addition)
MOV AX, 32767 ; Largest positive signed 16-bit value
MOV BX, 1 ; Small value to cause overflow
ADD AX, BX ; AX = AX + BX
JO overflow_detected ; Jump to "overflow_detected" if overflow (OF = 1)
; No overflow
MOV DX, msg_no_overflow
RET
overflow_detected:
; Overflow detected
MOV DX, msg
RET
MOV AX, 32767 and MOV BX, 1 load AX with the largest positive signed 16-bit value (32767) and BX with 1.ADD AX, BX adds AX and BX, resulting in 32768, which exceeds the 16-bit signed integer limit.JO overflow_detected will jump to the overflow_detected label because the Overflow Flag (OF) is set after the addition.JS)This example shows how to jump if the result of a signed comparison is negative, based on the Sign Flag (SF).
section .data
msg_negative db 'Negative', 0
msg_non_negative db 'Non-negative', 0
section .text
; Assume AX = -5, BX = 5
MOV AX, -5
MOV BX, 5
CMP AX, BX ; Compare AX and BX
JS negative ; Jump to "negative" if AX < BX (SF = 1)
; Non-negative
MOV DX, msg_non_negative
RET
negative:
; Negative
MOV DX, msg_negative
RET
MOV AX, -5 and MOV BX, 5 load AX with -5 and BX with 5.CMP AX, BX compares AX and BX. Since AX < BX in signed terms, the Sign Flag (SF) will be set (SF = 1).JS negative will jump to the negative label because SF = 1 indicates a negative result.JNS)This example demonstrates how to jump if the result of a signed operation is non-negative, i.e., if the Sign Flag (SF) is clear.
section .data
msg_positive_or_zero db 'Positive or Zero', 0
msg_negative db 'Negative', 0
section .text
; Assume AX = -5, BX = 5
MOV AX, -5
MOV BX, 5
CMP AX, BX ; Compare AX and BX
JNS positive_or_zero ; Jump to "positive_or_zero" if AX >= BX (SF = 0)
; Negative
MOV DX, msg_negative
RET
positive_or_zero:
; Non-negative or Zero
MOV DX, msg_positive_or_zero
RET
MOV AX, -5 and MOV BX, 5 load AX with -5 and BX with 5.CMP AX, BX compares AX and BX. Since AX is less than BX, the Sign Flag (SF) is set (SF = 1), meaning it's a negative result.JNS positive_or_zero will jump to the positive_or_zero label if the result is non-negative (SF = 0).| 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) |
Open this section to load past papers