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:

FlagNameMeaning
ZFZero FlagResult was zero (operands are equal)
SFSign FlagResult was negative (high bit set)
CFCarry FlagUnsigned overflow/borrow
OFOverflow FlagSigned 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:

JumpConditionMeaning (signed)
je/jzZF=1a == b
jne/jnzZF=0a != b
jgZF=0, SF=OFa > b
jgeSF=OFa >= b
jlSF!=OFa < b
jleZF=1 or SF!=OFa <= b

Your Task

Compare numbers and print the result. For each comparison, print "Y" if true, "N" if false:

  1. Is 5 == 5? (Y)
  2. Is 3 > 7? (N)
  3. Is 2 < 9? (Y)

Print: "Y\nN\nY\n"

x86_64 runtime loading...
Loading...
Click "Run" to execute your code.