54

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

xor     ax, ax

Does it just clear the register's value?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Chiggins
  • 8,197
  • 22
  • 56
  • 81
  • 11
    As a sweetener, this is now the *preferred* way to zero a register on modern x86-64 micro-architectures. It doesn't require any execution units (essentially handled in the decoder), effectively eliminates stalls (waiting) on the dst=src register, and breaks partial flags register stalls. – Brett Hale Feb 08 '14 at 18:02
  • 1
    possible duplicate of [Any reason to do a "xor eax, eax"?](http://stackoverflow.com/questions/1396527/any-reason-to-do-a-xor-eax-eax) – Ciro Santilli OurBigBook.com Jul 08 '15 at 15:07

11 Answers11

70

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
orlp
  • 112,504
  • 36
  • 218
  • 315
  • 1
    This is really helpful. This answer helped me in RE, so just in case: It would translate to something like `if(A != B)` in a high level language. – TheRookierLearner Oct 10 '13 at 05:13
  • 10
    @TheRookierLearner: `A XOR B` is a primitive building block for higher-level constructs. It *can be used* to implement `(A != B)`, but it is a distinctly different operation in its own right. Also, nightcracker's statement that `A XOR B` in english would be translated as "are A and B not equal" is only correct when you're looking at the result from a Boolean zero/nonzero perspective. A popular application of XOR is to toggle one or more bits of some input bitfield: the result of `A XOR 1` is the inversion of only the low bit of A. – phonetagger Mar 21 '14 at 16:43
26

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)

Lukasz
  • 7,572
  • 4
  • 41
  • 50
  • if I run `xor a, b` then will both register's values would be changed based on the condition? I mean if `a == b` then after running the code will they become `a = b = 0`? – Sunlight Jan 07 '23 at 13:05
  • @Sunlight no it doesn't work this way. Only one register would be affected (the first one) – Lukasz Jan 08 '23 at 14:09
18

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.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • 2
    In 32bit or 64bit mode, it's faster to zero ax with `xor eax, eax`. Shorter encoding, and no false dependency on the previous value of the upper 16b. In 16b mode on a modern CPU, probably `xor eax,eax` is still better, because of the [many special benefits of using a recognized zeroing idiom](http://stackoverflow.com/questions/33666617/which-is-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and/33668295#33668295) – Peter Cordes Jan 31 '16 at 12:34
7

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.

Daniel Stelter
  • 466
  • 3
  • 6
5

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
jweyrich
  • 31,198
  • 5
  • 66
  • 97
3
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.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
2

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)

John K.
  • 5,426
  • 1
  • 21
  • 20
2

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.

Sean
  • 60,939
  • 11
  • 97
  • 136
2

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
Olof Forshell
  • 3,169
  • 22
  • 28
1

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

1

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.

Dirk
  • 2,167
  • 2
  • 20
  • 29