Article View: comp.arch
Article #146Re: Performance of xchg reg,reg
From: Michael S
Date: Fri, 16 Oct 2026 08:34
Date: Fri, 16 Oct 2026 08:34
81 lines
5057 bytes
5057 bytes
On Fri, 16 Oct 2026 05:50:55 GMT anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote: > "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes: > >Well, for some reason the LOCK prefix is implied for XCHG. > > According to <https://www.felixcloutier.com/x86/xchg> (which is based > on (I think) Intel's documentation): > > |If a memory operand is referenced, the processor’s locking > protocol is |automatically implemented for the duration of the > exchange operation, |regardless of the presence or absence of the > LOCK prefix or of the |value of the IOPL. > > This does not cover the "xchg reg, reg" case. > > <https://www.felixcloutier.com/x86/lock> says: > |The XCHG instruction always asserts the LOCK# signal regardless of > the |presence or absence of the LOCK prefix. > > This seems to include the "xchg reg, reg" case, No, it does not. > but asserting the > LOCK# signal would make no difference, because: "the LOCK# signal > ensures that the processor has exclusive use of any shared memory > while the signal is asserted" and "the processor’s cache coherency > mechanism ensures that the operation is carried out atomically with > regards to memory", but "xchg reg, reg" does not use memory at all, > and other processors have no access to a processor's registers. > > - anton Intel text (325383-060US September 2016): Exchanges the contents of the destination (first) and source (second) operands. The operands can be two general- purpose registers or a register and a memory location. If a memory operand is referenced, the processor’s locking protocol is automatically implemented for the duration of the exchange operation, regardless of the presence or absence of the LOCK prefix or of the value of the IOPL. (See the LOCK prefix description in this chapter for more information on the locking protocol.) This instruction is useful for implementing semaphores or similar data structures for process synchronization. (See “Bus Locking” in Chapter 8 of the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A, for more information on bus locking.) The XCHG instruction can also be used instead of the BSWAP instruction for 16-bit operands. In 64-bit mode, the instruction’s default operation size is 32 bits. Using a REX prefix in the form of REX.R permits access to additional registers (R8-R15). Using a REX prefix in the form of REX.W promotes operation to 64 bits. See the summary chart at the beginning of this section for encoding data and limits. NOTE XCHG (E)AX, (E)AX (encoded instruction byte is 90H) is an alias for NOP regardless of data size prefixes, including REX.W. AMD Text (24594 Rev. 3.04 April 2003): Exchanges the contents of the two operands. The operands can be two general- purpose registers or a register and a memory location. If either operand references memory, the processor locks automatically, whether or not the LOCK prefix is used and independently of the value of IOPL. For details about the LOCK prefix, see “Lock Prefix” on page 10. The x86 architecture commonly uses the XCHG EAX, EAX instruction (opcode 90h) as a one-byte NOP. In 64-bit mode, the processor treats opcode 90h as a true NOP only if it would exchange rAX with itself. Without this special handling, the instruction would zero-extend the upper 32 bits of RAX, and thus it would not be a true no- operation. Opcode 90h can still be used to exchange rAX and r8 if the appropriate REX prefix is used. This special handling does not apply to the two-byte ModRM form of the XCHG instruction.
Technical Details
Message-ID:
<20261016113441.00002a5f@yahoo.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<2026Oct13.104347@mips.complang.tuwien.ac.at> <11arg45$2p70p$1@dont-email.me> <2026Oct16.075055@mips.complang.tuwien.ac.at>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Michael S <already5chosen@yahoo.com> Newsgroups: comp.arch Subject: Re: Performance of xchg reg,reg Date: Fri, 16 Oct 2026 11:34:41 +0300 Organization: A noiseless patient Spider Lines: 81 Message-ID: <20261016113441.00002a5f@yahoo.com> References: <2026Oct13.104347@mips.complang.tuwien.ac.at> <11arg45$2p70p$1@dont-email.me> <2026Oct16.075055@mips.complang.tuwien.ac.at> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Injection-Date: Fri, 16 Oct 2026 08:34:50 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3282467"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183W19GBkLwc7glvDJqz+o8RanlsmBS5Z8="; posting-host="3e1078d823f42cc48b95f64e5de69f38" Cancel-Lock: sha1:gE/kaHVDaDBTvF3Uuw2qfMZdWOs= sha256:bddFAv5sZh6IQ+uvJzuXgclDaj+4N162SCnD3FfoE/Y= sha1:h7MjW9R2d/y9AiRMuOAUpqcNASE= sha256:59xveSUmgLVxGpA19Yw0pI6rTlCMh/TefmFjBfi3qjE= X-Newsreader: Claws Mail 4.1.1 (GTK 3.24.34; x86_64-w64-mingw32)