The Intel 8086, released in 1978, included an instruction named
XCHG
. All three of these instructions swapped registers with registers, or registers with memory, but were unable to swap the contents of two memory locations.On the common x86 architecture, the
XCHG
instruction with a memory operand has an implicit LOCK
prefix, so that the operation is atomic, requiring hundreds of cpu cycles to synchronize with every other device that can access main memory. By comparison, on many processors a single MOV
instruction can be issued at the same time as other common instructions in a single clock cycle. Between two registers, XCHG
may still be slower than three MOV
s, but is smaller, so it may be useful where code size matters. x86s XCHG
is primarily useful for its locking form, for writing locking primitives used in threaded or multiprocessing applications.