Instructions
x86-64SETcc (conditional set)
Writes 1 or 0 to a byte operand based on a flag condition. The idiomatic way to turn a comparison into a boolean value without branching.
SETcc dst8 sets its 8-bit destination to 1 if condition cc holds and
0 otherwise. It uses the same condition codes as the
conditional jumps and
CMOVcc. It's how the compiler materialises a C boolean.
mov eax, 7
cmp eax, 5
setg al ; al = (7 > 5) ? 1 : 0 → 1
movzx eax, al ; zero-extend the byte to the full registerWhy the MOVZX almost always follows
SETcc writes only the low byte and leaves the upper bits of the register
untouched. Since a C int/bool result occupies the whole register, the
compiler clears the rest with MOVZX (or by
zeroing the register before the compare with xor eax, eax). The pair
setcc al + movzx eax, al is one of the most recognisable boolean idioms in
optimized code.
Condition suffixes
Identical to Jcc/CMOVcc: sete/setz, setne, setl/setge (signed),
setb/setae (unsigned), seta/setbe, sets/setns, and so on.
Reverse-engineering notes
cmp/test→setcc→movzxreconstructs directly to a comparison whose result is stored or returned as a value (e.g.return a > b;orint flag = (x == y);), as opposed to one used to branch (which would useJcc).- A function whose body is essentially
xor eax, eax; cmp …; sete al; retis a predicate — a boolean-returning helper. Very common for comparison callbacks andis*()checks. - Chained
setccresults combined withand/orare how compilers flattena && b/a || binto branchless code when both sides are cheap. - The destination must be 8-bit.
setg eaxis invalid; you'll always seesetg al/setg bl/setg byte ptr [mem].
See also: CMOVcc · Conditional jumps · CMP / TEST · MOVZX / MOVSX.
Try it — Virtual CPU
open full playground →- 1 mov eax, 7
- 2 cmp eax, 5
- 3 setg al
- 4 movzx eax, al
- 5