LOGBOOK

HELP

Quiz Entry - updated: 2026.07.10

Why does the compiler use xorl %eax, %eax before a SetX instruction?

To zero the whole register cheaply before SetX writes only its low byte — and xorl on a 32-bit register conveniently zeros all 64 bits.

A setX instruction sets just one byte (e.g. %al); the other bytes keep whatever junk they held. To return a clean 0 or 1, the register must be cleared first. The idiomatic clear is xorl %eax, %eax:

xorl %eax, %eax    # %rax = 0  (32-bit op zeros the upper 32 bits too)
cmpq %rsi, %rdi
setg %al           # set only the low byte to 0 or 1

Why this exact instruction? Three reasons combine: (1) anything XORed with itself is 0; (2) a 32-bit operation auto-zeros the upper 32 bits, so xorl %eax,%eax clears the full %rax; and (3) xor reg,reg has a tiny 2-byte encoding and is even special-cased by the CPU as a zeroing idiom — cheaper than movq $0, %rax.

The alternative, zero-extending afterward with movzbl %al, %eax, also works but is one step heavier. Recognizing the xor/set pair is key to reading compiled boolean expressions.

Go deeper:

From Quiz: REVE1 / The Processor Interface | Updated: Jul 10, 2026