1

In Programming from the Ground Up one reads that

the XOR operation is faster than the loading operation, so many programmers use it to load a register with a zero. For example, the code

movl $0, %eax

is often replaced by

xorl %eax, %eax

Now, since the book is old, a first question to ask would be whether this observation still applies today.

But in either case, the main question I have is why the two operations have (or had) different performance? What makes (or made) xorl be faster than movl?

I guess a hardware perspective is necessary to understand this?

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 3
    This answer may provide info you may be interested in: https://stackoverflow.com/a/33668295/3857942 . Note the mov immediate is a 5 byte instruction vs a 2 byte xor. – Michael Petch Aug 20 '23 at 20:09
  • There was a recent comment or answer saying that on today's ARM processors it makes no difference, since both take a single clock cycle, but I can't find it. Obviously though, they don't have the `eax` register. – Weather Vane Aug 20 '23 at 20:27
  • 2
    Modern x86-64 processors do not even need to execute `xor` in the main back-end parts (scheduler, execution units, etc.) : it can be filtered out before being scheduled so it does not reserve (much) backend resources. Basic `mov`s can be eliminated on some architectures too. SandyBridge processors (10 years old) was already capable of doing that like newer CPUs. `mov`s does not seems to be filtered out on Intel processors, though they are cheap (still more expensive than a `xor`). I guess this is the same on AMD Zen CPUs. Anyway both are *very* cheap on most CPUs. – Jérôme Richard Aug 20 '23 at 20:30
  • `xor` zeroing is a rather x86-specific solution. See this answer to take a broader view across instruction set architectures beyond x86 to other hardware: https://stackoverflow.com/a/76933357/471129 – Erik Eidt Aug 20 '23 at 20:37
  • 2
    The key thing is that `xor reg, reg` is a *zeroing idiom* recognised by the CPU. This means that the register is cleared without any µops at all. I've linked some relevant duplicated. Also check out [this article](https://randomascii.wordpress.com/2012/12/29/the-surprising-subtleties-of-zeroing-a-register/). – fuz Aug 20 '23 at 21:07

0 Answers0