| Slide 6.7: SAHF instruction Slide 6.9: Direct-offset operands Home |   | 
XCHG Instruction
 
XCHG instruction (exchange data) exchanges the contents of two operands.
| XCHG— Exchange | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 
 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| XCHGallows to exchange two operands without needing a third register or variable to hold a temporary value.  The rules for operands in theXCHGare the same as those for theMOVinstruction. | 
 | 
| Exchanging XandX+2(?) | ||
|---|---|---|
| .data X DWORD 12345678h .code mov eax, X xchg ax, X mov X+2, ax mov eax, X call WriteHex | .data X DWORD 12345678h .code mov eax, X mov ebx, OFFSET X xchg ax, [ebx+2] xchg [ebx], ax mov eax, X call WriteHex | .data X DWORD 12345678h .code mov eax, X mov ebx, OFFSET X xchg ax, [ebx] xchg [ebx+2], ax mov eax, X call WriteHex | 
| Output | Output | Output |