Jumps & Conditionals
A jump changes rip, so the CPU continues somewhere else. jmp label always jumps; a conditional jump (jcc) jumps only if the flags satisfy a condition and otherwise falls through to the next instruction. Together with cmp and test, that is all you need to build if, else, switch and loops.
The conditional jumps
| Jump | Condition | Flags | Typical meaning after cmp a, b |
|---|---|---|---|
je / jz |
equal / zero | ZF=1 | a == b |
jne / jnz |
not equal / not zero | ZF=0 | a != b |
jl / jnge |
less (signed) | SF≠OF | a < b |
jle / jng |
less or equal (signed) | ZF=1 or SF≠OF | a <= b |
jg / jnle |
greater (signed) | ZF=0 and SF=OF | a > b |
jge / jnl |
greater or equal (signed) | SF=OF | a >= b |
jb / jc / jnae |
below (unsigned) | CF=1 | a < b |
jbe / jna |
below or equal (unsigned) | CF=1 or ZF=1 | a <= b |
ja / jnbe |
above (unsigned) | CF=0 and ZF=0 | a > b |
jae / jnc / jnb |
above or equal (unsigned) | CF=0 | a >= b |
js / jns |
sign / no sign | SF | negative / not negative |
jo / jno |
overflow / no overflow | OF | signed overflow |
Memory aid: less/greater are for signed numbers, below/above for unsigned numbers.
if / else
To translate if (a > b) max = a; else max = b;, jump around the code that should not run. Often it is easiest to test the opposite condition and jump to the else part:
section .text
global _start
_start:
mov rbx, 17 ; a
mov r12, 42 ; b
cmp rbx, r12
jle .else ; NOT (a > b) -> else branch
mov rdi, rbx ; then: max = a
jmp .end_if
.else:
mov rdi, r12 ; else: max = b
.end_if:
call print_int ; max
mov eax, 60
xor edi, edi
syscall
; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
test rdi, rdi
jns print_uint ; not negative: print it as it is
neg rdi ; make it positive
push rdi ; keep it (write and syscall clobber registers)
push '-' ; the minus sign, as a byte on the stack
mov eax, 1 ; write(1, rsp, 1)
mov edi, 1
mov rsi, rsp
mov edx, 1
syscall
pop rdi ; drop the '-'
pop rdi ; the positive value again
jmp print_uint ; print_uint's ret returns to our caller
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret42
The jmp .end_if at the end of the then branch is essential: without it the CPU would continue straight into the else code.
An if / else-if chain
A chain of conditions becomes a sequence of compare-and-jump pairs. This program turns a score into a letter grade:
section .data
grade_a: db "A", 10
grade_b: db "B", 10
grade_c: db "C", 10
grade_f: db "F", 10
section .text
global _start
_start:
mov ebx, 83 ; the score
mov rsi, grade_a
cmp ebx, 90
jge .print ; score >= 90
mov rsi, grade_b
cmp ebx, 80
jge .print ; score >= 80
mov rsi, grade_c
cmp ebx, 70
jge .print ; score >= 70
mov rsi, grade_f ; everything else
.print:
mov eax, 1
mov edi, 1
mov edx, 2
syscall
mov eax, 60
xor edi, edi
syscallB
The trick here: load the answer first, then test, and jump out as soon as one test succeeds. mov doesn't touch the flags, so it can sit between instructions freely.
Signed vs unsigned: picking the wrong jump
The same bits compare differently depending on the jump. With -1 and 1, jl (signed) says "less" while jb (unsigned) says "not below", because -1 is 0xFFFFFFFFFFFFFFFF, the largest unsigned value:
section .data
yes: db "yes", 10
no: db "no", 10
section .text
global _start
_start:
mov rbx, -1
cmp rbx, 1
jl .signed_less
mov rsi, no
mov edx, 3
jmp .print1
.signed_less:
mov rsi, yes
mov edx, 4
.print1:
mov eax, 1
mov edi, 1
syscall ; is -1 < 1 (signed)?
cmp rbx, 1
jb .unsigned_below
mov rsi, no
mov edx, 3
jmp .print2
.unsigned_below:
mov rsi, yes
mov edx, 4
.print2:
mov eax, 1
mov edi, 1
syscall ; is -1 < 1 (unsigned)?
mov eax, 60
xor edi, edi
syscallyes
no
This is a real bug source: sizes, counts and addresses are unsigned (use jb/ja), while temperatures, differences and balances are signed (use jl/jg).
Combining conditions
&& and || turn into several jumps:
; if (x >= 10 && x <= 20) { ... }
cmp eax, 10
jl .skip ; first condition false -> skip
cmp eax, 20
jg .skip ; second condition false -> skip
; ... body ...
.skip:A neat trick checks a range with one comparison: low <= x <= high is the same as (unsigned)(x - low) <= high - low, because values below low wrap around to huge unsigned numbers.
section .text
global _start
_start:
mov ebx, 'k' ; is it a lowercase letter?
call is_lower
mov ebx, 'K'
call is_lower
mov ebx, '7'
call is_lower
mov eax, 60
xor edi, edi
syscall
; is_lower: print 1 if ebx is in 'a'..'z', else 0
is_lower:
lea eax, [rbx - 'a'] ; x - 'a' (lea leaves the flags alone)
xor edi, edi ; default answer 0
cmp eax, 'z' - 'a'
ja .done ; unsigned above 25 -> not a letter
mov edi, 1
.done:
jmp print_uint ; tail call
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret1
0
0
Jump distance
NASM picks the encoding automatically: a short jump (2 bytes, ±127 bytes) when the target is near, a near jump (5–6 bytes, ±2 GB) otherwise. You can jump to a label anywhere in your code — but keeping jumps short and forward-flowing makes code faster and much easier to read.
Test yourself
Which jump should follow cmp rax, rbx to branch when rax < rbx for UNSIGNED values?
jljbjgjs
Why does the 'then' branch of an if/else end with a jmp?
- To return from the program
- To skip over the else branch
- To set the flags
- It is optional decoration
What does jne test?
- CF = 0
- ZF = 0
- SF = OF
- ZF = 1