Search code examples
assemblyx86

Why no zero register instruction on x86


Why does the x86 instruction set not have a dedicated instruction for zeroing a register? Devs have to use idioms like xor reg, reg to perform this type of function.


Solution

  • Historically, CPU architectures have been minimalistic.

    The op-code space is the most valuable resource of a CPU: the number of available 8-bit numbers is very small, so every op-code counts. A feature that would reserve an extra opcode would have to be extremely valuable in order to be considered. If the same feature can be achieved by an already existing instruction, then this is always a winner.

    So, every single feature that is offered by a CPU is almost never for the programmer's convenience, but instead in order to achieve something that would otherwise be impossible, or in order to achieve better performance. (And of course these two are equivalent, because everything is possible given enough clock cycles, so ultimately there is only one goal, and this is performance.)

    The xor reg, reg idiom of x86/x64 satisfies this minimalistic need: it does get the job done, and its performance cannot be improved.

    The fact that "Devs have to use idioms like xor reg, reg to perform this type of function" is of no concern whatsoever: every single instruction that you use in Assembly is an idiom. Give or take one (or a few, or even many) does not make any difference.