Intel x86 Assembly Language & Microarchitecture Optimization Zeroing a register


The obvious way to zero a register is to MOV in a 0—for example:

B8 00 00 00 00    MOV eax, 0

Notice that this is a 5-byte instruction.

If you are willing to clobber the flags (MOV never affects the flags), you can use the XOR instruction to bitwise-XOR the register with itself:

33 C0             XOR eax, eax

This instruction requires only 2 bytes and executes faster on all processors.