In assembly, jumps based on equality are used to alter the flow of the program when two values are equal or not equal. These types of jumps are commonly used after performing a comparison (CMP) between two values.
When you perform a comparison (CMP), it subtracts one operand from another but does not store the result—rather, it updates the flags (Zero Flag (ZF), Carry Flag (CF), Sign Flag (SF), etc.) based on the result of the subtraction. The Zero Flag (ZF) is particularly important when checking for equality.
JE (Jump if Equal):
JZ (Jump if Zero), because both check the Zero Flag (ZF).JNE (Jump if Not Equal):
JNZ (Jump if Not Zero), as both check if the Zero Flag (ZF) is not set.CMP Instruction: This is a typical instruction used to compare two values, and it works by subtracting the second operand from the first operand. It updates the flags in the EFLAGS register based on the result.
After using CMP, you can use JE or JNE to jump based on whether the values are equal or not equal.
JE or JZ)The JE (Jump if Equal) instruction is used when you want to jump to a label if the two values compared are equal. It checks the Zero Flag (ZF) to determine if the comparison resulted in equality.
JEsection .data
num1 dw 10
num2 dw 10
section .text
MOV AX, [num1] ; Load AX with num1 (10)
MOV BX, [num2] ; Load BX with num2 (10)
CMP AX, BX ; Compare AX and BX (sets flags)
JE values_equal ; Jump to "values_equal" if AX == BX (ZF = 1)
; Code here will execute if AX != BX (ZF = 0)
; For example, output something indicating they are not equal
MOV DX, 'Not Equal'
; Continue with program
RET
values_equal:
; Code here will execute if AX == BX (ZF = 1)
; For example, output something indicating they are equal
MOV DX, 'Equal'
RET
In this example, the CMP instruction compares the values in AX and BX. If they are equal, the Zero Flag (ZF) will be set, and the program will jump to the values_equal label.
JNE or JNZ)The JNE (Jump if Not Equal) instruction is used when you want to jump to a label if the two values compared are not equal. It checks if the Zero Flag (ZF) is not set, indicating that the comparison resulted in a non-zero difference.
JNEsection .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)
JNE values_not_equal ; Jump to "values_not_equal" if AX != BX (ZF = 0)
; Code here will execute if AX == BX (ZF = 1)
MOV DX, 'Equal'
; Continue with program
RET
values_not_equal:
; Code here will execute if AX != BX (ZF = 0)
MOV DX, 'Not Equal'
RET
In this example, the CMP instruction compares AX and BX. Since they are not equal (10 != 5), the Zero Flag (ZF) will be clear, and the program will jump to the values_not_equal label.
The CMP instruction subtracts the second operand from the first and updates the processor flags based on the result of the subtraction. Importantly, CMP does not store the result, it only modifies the flags.
CMP:Example of CMP Instruction Behavior:
CMP AX, BX ; AX - BX (set flags)
JE (Jump if Equal): Jumps if Zero Flag (ZF) = 1 (i.e., the values are equal).
JZ (Jump if Zero).JNE (Jump if Not Equal): Jumps if Zero Flag (ZF) = 0 (i.e., the values are not equal).
JNZ (Jump if Not Zero).CMP.JE (if the Zero Flag is set).JNE (if the Zero Flag is cleared).This mechanism allows for efficient decision-making in assembly language programs based on the outcome of previous operations.
Open this section to load past papers