Hasty Briefsbeta

Bilingual

XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?

5 hours ago
  • #compiler design
  • #assembly optimization
  • #x86 architecture
  • XOR instruction is most compact for zeroing a register on x86, avoiding encoding a constant.
  • SUB encodes to same bytes and cycles as XOR but leaves AF flag cleared; XOR leaves AF undefined.
  • Historical popularity of XOR may be due to early compiler adoption, creating a snowball effect.
  • Intel added special detection for XOR and SUB to bypass execution and break dependency chains.
  • Concerns about CPU manufacturers supporting XOR but not SUB give XOR a slight edge.
  • The XOR trick doesn't work on Itanium, which has a dedicated zero register.
  • Some early processors or manuals may have favored XOR for performance or recommendation reasons.