Jump to content

Xor swap algorithm/Assembler Code

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Zundark (talk | contribs) at 18:30, 8 March 2003 (wikify and correct (all x86 CPUs support XCHG AX, BX)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The following code in x86 assembly language uses the xor swap algorithm to swap the value in the AX register with the value in the BX register without using a temporary buffer.

           XOR AX, BX
           XOR BX, AX
           XOR AX, BX

However, all x86 microprocessors have an XCHG instruction which does the same work on its operands more efficiently than the above sequence of XORs.