Skip to content

Instructions

x86-64

SETcc (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.

asm
mov eax, 7
cmp eax, 5
setg al           ; al = (7 > 5) ? 1 : 0  →  1
movzx eax, al     ; zero-extend the byte to the full register

Why 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/testsetccmovzx reconstructs directly to a comparison whose result is stored or returned as a value (e.g. return a > b; or int flag = (x == y);), as opposed to one used to branch (which would use Jcc).
  • A function whose body is essentially xor eax, eax; cmp …; sete al; ret is a predicate — a boolean-returning helper. Very common for comparison callbacks and is*() checks.
  • Chained setcc results combined with and/or are how compilers flatten a && b / a || b into branchless code when both sides are cheap.
  • The destination must be 8-bit. setg eax is invalid; you'll always see setg al/setg bl/setg byte ptr [mem].

See also: CMOVcc · Conditional jumps · CMP / TEST · MOVZX / MOVSX.

Try it — Virtual CPU

open full playground →
  1. 1 mov eax, 7
  2. 2 cmp eax, 5
  3. 3 setg al
  4. 4 movzx eax, al
  5. 5
step 0
Loading emulator…