What is the meaning of XOR in x86 assembly?

前端 未结 11 1825
醉梦人生
醉梦人生 2020-12-13 08:50

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

xor     ax, ax

Does it just clear the register\'s value?

相关标签:
11条回答
  • 2020-12-13 09:39

    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.

    0 讨论(0)
  • 2020-12-13 09:41

    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)

    0 讨论(0)
  • 2020-12-13 09:43

    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
    
    0 讨论(0)
  • 2020-12-13 09:44

    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.

    0 讨论(0)
  • 2020-12-13 09:47

    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.

    0 讨论(0)
提交回复
热议问题