Assembly Language - Conditional Judgement
Conditional judgement is the cornerstone of program control flow. Assembly language implements judgement logic such as if-else and switch via comparison instructions and conditional jump instructions.
CMP - compare instruction
CMP Performs a subtraction on two operands (destination - source) but does not save the result; it only updates the flags.
In essence it is CMP dest, src is equivalent to SUB dest, src but discards the computation result.
Example
; CMP compare instruction example
section
.text
global
_start
_start
:
mov
eax
,
10
cmp
eax
,
10
; eax == 10?
; ZF = 1 (equal, result is zero)
; CF = 0 (no borrow)
; SF = 0 (result is non-negative)
mov
eax
,
5
cmp
eax
,
10
; eax == 10?
; ZF = 0 (not equal, result is non-zero)
; CF = 1 (borrow: 5 < 10)
mov
eax
,
20
cmp
eax
,
10
; eax == 10?
; ZF = 0 (not equal)
; CF = 0 (no borrow: 20 >= 10)
; SF = 0 (result non-negative: 10 > 0)
mov
eax
,
1
mov
ebx
,
0
int
0x80
Conditional jump instructions
Conditional jump instructions decide whether to jump based on the state of the flags. If the condition holds, jump to the target label; otherwise continue with the next instruction.
| Instruction | Meaning | Flag checked | Applicable scenarios |
|---|---|---|---|
| JE / JZ | Jump if equal / zero | ZF = 1 | cmp a, b check a == b after CMP |
| JNE / JNZ | Jump if not equal / not zero | ZF = 0 | cmp a, b check a != b after CMP |
| JG / JNLE | Jump if greater (signed) | ZF=0 and SF=OF | signed a > b |
| JGE / JNL | Jump if greater or equal (signed) | SF = OF | signed a >= b |
| JL / JNGE | Jump if less (signed) | SF != OF | signed a < b |
| JLE / JNG | Jump if less or equal (signed) | ZF=1 or SF!=OF | signed a <= b |
| JA / JNBE | Jump if greater (unsigned) | CF=0 and ZF=0 | unsigned a > b |
| JAE / JNB | Jump if greater or equal (unsigned) | CF = 0 | unsigned a >= b |
| JB / JNAE | Jump if less (unsigned) | CF = 1 | unsigned a < b |
| JBE / JNA | Jump if less or equal (unsigned) | CF=1 or ZF=1 | unsigned a <= b |
Many instructions have two aliases (such as JE and JZ) that are exactly the same at the machine-code level. Which one to use depends on the context: use JE/JNE after a comparison, and JZ/JNZ after checking an arithmetic result. This makes the code more readable.
Single-branch IF structure
Example
; File path: if_demo.asm
; Implement: if (x > 10) x = 10;
section
.data
x
dd
15
; Test value
limit
dd
10
section
.text
global
_start
_start
:
mov
eax
,
[
x
]
; Load x into eax
cmp
eax
,
[
limit
]
; x > 10 ?
jle
skip_update
; If x <= 10, skip the update
mov
dword
[
x
]
,
10
; x = 10
skip_update
:
; Program continues... (here x is already 10)
mov
eax
,
1
mov
ebx
,
0
int
0x80
IF-ELSE two-branch structure
Example
; File path: if_else_demo.asm
; Implement: if (score >= 60) grade = 'P' else grade = 'F'
section
.data
score
dd
75
; Exam score
grade
db
0
; Grade level
PASS_SCORE
equ
60
section
.text
global
_start
_start
:
mov
eax
,
[
score
]
; Load the score
cmp
eax
,
PASS_SCORE
; score >= 60 ?
jge
pass_label
; If >= 60, jump to pass
; else branch: fail
mov
byte
[
grade
]
,
'F'
; grade = 'F'
jmp
end_if
; Skip the if branch
pass_label
:
; if branch: pass
mov
byte
[
grade
]
,
'P'
; grade = 'P'
end_if
:
; The grade variable is now set
; Output the grade level
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
grade
mov
edx
,
1
int
0x80
mov
eax
,
1
mov
ebx
,
0
int
0x80
Running result:
$ nasm -f elf32 if_else_demo.asm -o if_else_demo.o
$ ld -m elf_i386 if_else_demo.o -o if_else_demo
$ ./if_else_demo
P
IF-ELSE IF-ELSE multi-branch structure
The following uses a score grading system as an example to show the flow of multi-branch judgement:
Example
; File path: multi_branch.asm
; Score grading: >=90 -> A, >=80 -> B, >=70 -> C, >=60 -> D, <60 -> F
section
.data
score
dd
85
result
db
0
newline
db
0xA
section
.text
global
_start
_start
:
mov
eax
,
[
score
]
; Load the score
; Check >= 90
cmp
eax
,
90
jl
check_80
; If < 90, continue checking
mov
byte
[
result
]
,
'A'
jmp
print_result
check_80
:
cmp
eax
,
80
jl
check_70
mov
byte
[
result
]
,
'B'
jmp
print_result
check_70
:
cmp
eax
,
70
jl
check_60
mov
byte
[
result
]
,
'C'
jmp
print_result
check_60
:
cmp
eax
,
60
jl
fail_label
mov
byte
[
result
]
,
'D'
jmp
print_result
fail_label
:
mov
byte
[
result
]
,
'F'
print_result
:
; Output the grade
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
result
mov
edx
,
1
int
0x80
; Output a newline
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
newline
mov
edx
,
1
int
0x80
mov
eax
,
1
mov
ebx
,
0
int
0x80
TEST - non-destructive test instruction
TEST Performs a bitwise AND but does not save the result; it only updates the flags.
TEST Commonly used to check whether a specific bit is 1, or whether a register is 0.
Example
; TEST instruction example
; Check whether eax is 0 (more efficient than CMP eax, 0)
test
eax
,
eax
; eax & eax = eax, only updates ZF
jz
eax_is_zero
; If ZF=1, it means eax=0
; Check a specific bit
test
al
,
0x01
; Check whether bit0 is 1
jnz
bit0_is_set
; If bit0=1, jump
; Check multiple bits
test
al
,
0x03
; Check whether at least one of bit0 and bit1 is 1
jnz
some_bit_set
SETcc - conditional set instruction
Does not jump; instead it sets the target byte to 1 or 0 based on the condition:
Example
; SETcc conditional set example
; Store the result of a > b into al
mov
eax
,
10
cmp
eax
,
5
; 10 > 5 ?
setg
al
; al = 1 (greater holds)
; If eax = 3, then al = 0
; Other SETcc instructions:
; sete / setz -> equal/zero
; setne / setnz -> not equal/not zero
; setl -> less than (signed)
; setb -> less than (unsigned)
; setg -> greater than (signed)
; seta -> greater than (unsigned)
Complete example: determine parity and sign
Example
; File path: number_check.asm
; Determine the parity and sign of a number
section
.data
number
dd
42
msg_even
db
'Even'
,
0xA
msg_even_len
equ
$
msg_even
msg_odd
db
'Odd'
,
0xA
msg_odd_len
equ
$
msg_odd
msg_pos
db
'Positive'
,
0xA
msg_pos_len
equ
$
msg_pos
msg_neg
db
'Negative'
,
0xA
msg_neg_len
equ
$
msg_neg
msg_zero
db
'Zero'
,
0xA
msg_zero_len
equ
$
msg_zero
section
.text
global
_start
_start
:
mov
eax
,
[
number
]
; Check whether it is 0
cmp
eax
,
0
jne
check_sign
; Is zero
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
msg_zero
mov
edx
,
msg_zero_len
int
0x80
jmp
exit
check_sign
:
; Determine the sign
cmp
eax
,
0
jg
is_positive
; eax > 0
; Negative
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
msg_neg
mov
edx
,
msg_neg_len
int
0x80
jmp
check_parity
is_positive
:
; Positive
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
msg_pos
mov
edx
,
msg_pos_len
int
0x80
check_parity
:
; Determine parity (only need to look at the lowest bit)
mov
eax
,
[
number
]
test
eax
,
1
; Check bit0
jnz
is_odd
; Even
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
msg_even
mov
edx
,
msg_even_len
int
0x80
jmp
exit
is_odd
:
; Odd
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
msg_odd
mov
edx
,
msg_odd_len
int
0x80
exit
:
mov
eax
,
1
mov
ebx
,
0
int
0x80
Conditional jump instructions can only jump to labels within the current code segment, and the jump range is usually limited to about 128 bytes (short jump) up to 2GB (near jump). NASM automatically selects the appropriate jump encoding.