LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

What is the difference between the cmp and test instructions?

cmp sets flags from a subtraction (S1 - S2); test sets flags from a bitwise AND (S1 & S2); neither stores the result — they only update the flags.

Both are "throwaway-result" instructions: they perform an operation purely to set the condition flags, then discard the numeric answer. You follow them with a conditional jump/set/move that reads those flags.

Instruction (AT&T) Computes Flags it drives Typical use
cmp S2, S1 S1 - S2 CF, ZF, SF, OF Compare two values (<, ==, >)
test S2, S1 S1 & S2 ZF, SF (CF/OF cleared) Check if zero, or test specific bits
cmpq %rsi, %rdi     # flags from (rdi - rsi)
jg   .L1            # jump if rdi > rsi (signed)

testq %rdi, %rdi    # flags from (rdi & rdi) == rdi itself
jz   .L2            # jump if rdi == 0

Tip: test %rax, %rax is the idiomatic "is this register zero?" check. ANDing a value with itself yields the value, so ZF tells you if it was zero — and it's shorter/faster than cmp $0, %rax.

Go deeper:

From Quiz: REVE1 / Translation of C to Assembly | Updated: Jul 14, 2026