What is the meaning of XOR in x86 assembly?

AssemblyXor

Assembly Problem Overview


I'm getting into assembly and I keep running into xor, for example:

xor     ax, ax

Does it just clear the register's value?

Assembly Solutions


Solution 1 - 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

Solution 2 - Assembly

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)

Solution 3 - Assembly

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.

Solution 4 - Assembly

xor register, register is commonly used to 'zero' a register, because all bits are compared with each other:

0-bits stay zero. 1-bits become zero, because 1 XOR 1 is also 0.

Solution 5 - Assembly

xor = exclusive or. See wikipedia's definition for Exclusive or.

If you xor a register with itself, it will zero that register.

0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0

Let's take the value 41 as example (in binary):

    101001
xor 101001
  = 000000

Solution 6 - Assembly

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

The XOR instruction does the above operation on every pair of bits in the two operands. So 0xFF xor 0xFF would be 0x00 and 0x55 xor 0xAA would be 0xFF. And yes, xor ax ax clears ax.

Solution 7 - Assembly

In this case it will clear the register... XOR is an "exclusive or"... so if ax contains 1010 and you exclusive or that with 1010 you'll get 0000 (cleared)

Solution 8 - Assembly

If I remember correctly xor ax, ax is a one byte assembly instruction, whilst mov ax, 0 would be at least 3 and would probably take slightly longer to execute. It will certainly take longer to decode than the xor instruction.

Solution 9 - Assembly

When I started programming a long time ago there was no exclusive or on either the processor or in the compiler. When I got around to it I stuck to the descriptions:

  • or: true if a=1 or b=1 or both=1
  • xor: true if a=1 or b=1 but not both=1

so:

0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1

and

0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0

Solution 10 - Assembly

It determines the logical eXclusive OR

0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

So, TRUE only if one of the expressions is true, not both.

Solution 11 - Assembly

xor ax,ax is used to set ax to 0.

Reason: typically xor instruction on any processor takes less bytes in assembling, than using movl 0,%ax

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChigginsView Question on Stackoverflow
Solution 1 - AssemblyorlpView Answer on Stackoverflow
Solution 2 - AssemblyLukaszView Answer on Stackoverflow
Solution 3 - AssemblySalman AView Answer on Stackoverflow
Solution 4 - AssemblyDaniel StelterView Answer on Stackoverflow
Solution 5 - AssemblyjweyrichView Answer on Stackoverflow
Solution 6 - AssemblynmichaelsView Answer on Stackoverflow
Solution 7 - AssemblyJohn K.View Answer on Stackoverflow
Solution 8 - AssemblySeanView Answer on Stackoverflow
Solution 9 - AssemblyOlof ForshellView Answer on Stackoverflow
Solution 10 - AssemblyDirkView Answer on Stackoverflow
Solution 11 - AssemblyVedhas DeshpandeView Answer on Stackoverflow