Conditional jumps

The jump family

Conditional jumps read the flags from the last CMP or arithmetic instruction. JE and JZ are the same jump (zero flag), as are JNE and JNZ:

cmp rax, rbx je equal ; taken when rax = rbx jne not_equal ; taken when rax != rbx

Signed versus unsigned

JG, JGE, JL, JLE compare signed values. JA, JAE, JB, JBE compare unsigned. The same bits mean different things in each reading, so pick the family that matches your data:

cmp rax, rbx jg greater ; signed: rax > rbx ja above ; unsigned: rax > rbx

The compare-then-jump pattern

The universal pattern is CMP then Jcc: compare first, branch second, and never put anything between them that could overwrite the flags. A jump target is a label, and the assembler picks the shortest encoding that reaches it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which two jumps are the same instruction with different names?

    Answer

    JE and JZ

    Both JE and JZ test the zero flag; the assembler encodes them the same way.

  2. Which jump family compares signed values?

    Answer

    JG, JGE, JL, JLE

    G and L mean greater and less for signed numbers; A and B are above and below for unsigned.

  3. Which flags does JS read?

    Answer

    SF

    JS jumps when the sign flag is set, which means the result was negative.

  4. Why must nothing that changes the flags sit between CMP and Jcc?

    Answer

    The jump would read a different result

    The jump tests the flags left by the last instruction, so a write between them changes the branch.

  5. Which jump goes to the label when rax is below rbx as unsigned values?

    Answer

    JB

    B means below in unsigned comparison; L is the signed form.

  6. What does cmp rax, rbx followed by je equal test?

    Answer

    Whether rax equals rbx

    CMP sets ZF when the difference is zero, and JE reads ZF.

  7. What does this program print? default rel section .data gtmsg db "gt", 10 lemsg db "le", 10 section .text global _start _start: mov rax, 9 mov rbx, 4 cmp rax, rbx jg greater mov rsi, lemsg jmp print greater: mov rsi, gtmsg print: mov rax, 1 mov rdi, 1 mov rdx, 3 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    gt

    9 is greater than 4, so the signed jg takes the gt branch.

  8. What does this program print? default rel section .data under db "under", 10 over db "over", 10 section .text global _start _start: mov eax, -1 cmp eax, 0 jl less mov rsi, over jmp print less: mov rsi, under print: mov rax, 1 mov rdi, 1 mov rdx, 6 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    under

    The signed value -1 is less than 0, and jl is the signed jump for it.

  9. What is a jump target in NASM?

    Answer

    A label

    The assembler turns the label into a relative or absolute address.

  10. Which jump is taken when the carry flag is set?

    Answer

    JC

    JC and JB are the carry jump, which means an unsigned borrow or overflow.

The snippets on this page are fragments, not whole programs: they show the jump families and the compare-then-jump pattern. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev