What is the meaning of XOR in x86 assembly?

A XOR B in english would be translated as "are A and B not equal". So xor ax, ax will set ax to zero since ax is always equal to itself.

A B | A XOR B
0 0 | 0
1 0 | 1
0 1 | 1
1 1 | 0

xor reg, reg is often used to clear register. It can be an alternative to mov reg, 0

AFAIR, it was faster (or shorter) in some cases.

And of course, XOR itself is eXclusive OR (a.k.a.: exclusive disjunction) operation (but it's a shame to describe here such basics - use Wikipedia)


xor ax, ax is the fastest possible way to set the ax register to 0. Fastest in terms of the size of instruction and number of instructions. For detail about how it works you need a little knowledge of bit arithmetic.

XOR operation between two bits returns 1 if one and only one of the two bits is 1; 0 otherwise. Another way to explain is that that it returns 1 if the two bits are different; 0 otherwise.

XOR operation between two binary numbers of same length works likewise on a bit-by-bit basis. XOR two numbers you get a number with bits set to 1 where corresponding bits of the two operands differ, 0 when corresponding bits are same.

From this knowledge its fairly easy to see that if the two operands are the same (ax and ax for example) the result will be 0.