XOR register,register (assembler)

Aerus picture Aerus · Nov 20, 2011 · Viewed 7.6k times · Source

From time to time we have to analyze pieces of assembler code (IA32), and more than often i come across an instruction that looks like this:

xor ax, ax

or with other registers aswell: xor dx, dx, xor al, al, ...

What exactly does this do ? (ax xor ax always gives 0 ?)

Answer

CodesInChaos picture CodesInChaos · Nov 20, 2011

It's a common assembler idiom to set a register to 0.

xor ax, ax corresponds to ax = ax ^ ax which, as you already noticed, is effectively ax = 0.

If I recall correctly the main advantage is that its code-size is smaller than mov ax, 0