3

I have the following line in an assembler code:

 XOR EAX, EBX

So, then I've searched a little bit and found out that XOR represents a "swap algorithm". You can read it here: http://en.wikipedia.org/wiki/XOR_swap_algorithm

But when I look in register window of ollydbg, then I have the following

 EAX = 00000068
 EBX = 0000003B

Now, after the line the register window says

EAX = 000000053
EBX = 0000003B

But from that what I have read in wikipedia article I would expect the following

EAX = 0000003B
EBX = 00000053

At the end, i can say that a normal XOR operation is performed because:

0111011   =>EAX=0000003B 
1101000   =>EBX=00000068
-------
1010011   =>EAX=00000053

So my question would be why the swap algorithm is not performed. Or in other words: When can I expect the swap algorithm?

user3097712
  • 1,541
  • 1
  • 25
  • 44

2 Answers2

13

As the first answer states, XOR is a bitewise XOR, not an XOR swap.

To do the Xor swap that you referenced from wikipedia it takes 3 instructions :

xor eax, ebx
xor ebx, eax
xor eax, ebx

Since you asked about the purpose of XOR I thought I would include some other concepts for you to read up on, so you might have an idea of what to expect from XORs

You can use XOR to clear a register:

xor eax,eax

Calculate absolute value:

cdq
xor eax,edx
sub eax,edx

XORs can be used for Crypto: http://en.wikipedia.org/wiki/XOR_swap_algorithm

XORs are used in the CRC checksum algorithm: http://en.wikipedia.org/wiki/Cyclic_redundancy_check

XORs can be used to calculate Gray codes: http://www.morkalork.com/mork/article/74/How_to_understand_and_use_Gray_code.htm#.U6RhN_ldXvI

This is just the tip of the iceberg. The instruction can be used in a large number of situations.

MrSynAckSter
  • 1,258
  • 1
  • 10
  • 24
11

The XOR mnemonic does not represent an XOR swap; it represents a bitwise XOR.

Jason Geffner
  • 20,681
  • 1
  • 36
  • 75