Lesson 8 of 15
CMP & Flags
Comparison and the Flags Register
The cmp instruction compares two values by subtracting them, but discards the result -- it only sets the flags:
cmp rax, rbx ; computes rax - rbx, sets flags
The Flags Register
After cmp or arithmetic, these flags are set:
| Flag | Name | Meaning |
|---|---|---|
| ZF | Zero Flag | Result was zero (operands are equal) |
| SF | Sign Flag | Result was negative (high bit set) |
| CF | Carry Flag | Unsigned overflow/borrow |
| OF | Overflow Flag | Signed overflow |
TEST Instruction
test performs a bitwise AND and sets flags (discards result):
test rax, rax ; sets ZF=1 if rax is zero
This is the idiomatic way to check if a register is zero.
Conditional Jumps After CMP
After cmp a, b:
| Jump | Condition | Meaning (signed) |
|---|---|---|
je/jz | ZF=1 | a == b |
jne/jnz | ZF=0 | a != b |
jg | ZF=0, SF=OF | a > b |
jge | SF=OF | a >= b |
jl | SF!=OF | a < b |
jle | ZF=1 or SF!=OF | a <= b |
Your Task
Compare numbers and print the result. For each comparison, print "Y" if true, "N" if false:
- Is 5 == 5? (Y)
- Is 3 > 7? (N)
- Is 2 < 9? (Y)
Print: "Y\nN\nY\n"
x86_64 runtime loading...
Loading...
Click "Run" to execute your code.