🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #153

Re: Performance of xchg reg,reg

#153
From: Dan Cross
Date: Fri, 16 Oct 2026 18:23
64 lines
3754 bytes
In article <20261016113441.00002a5f@yahoo.com>,
Michael S  <already5chosen@yahoo.com> wrote:
>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.

Unfortunately, the information is spread across two sections,
and they contradict.  The description in the `LOCK` prefix
section in the latest version of the Intel SDM includes the text
Anton quoted, while the description of `XCHG` itself makes clear
that the destination operand must be a memory operand for the
locking protocol to automatically apply.  Which is correct?

Personally, I suspect it's the one in the `XCHG` description.
Context is important, and the full paragraph that talks about
`XCHG` includes more:

|The LOCK prefix can be prepended only to the following
|instructions and only to those forms of the instructions where
|the destination operand is a memory operand: `ADD`, `ADC`,
|`AND`, `BTC`, `BTR`, `BTS`, `CMPXCHG`, `CMPXCH8B`,
|`CMPXCHG16B`, `DEC`, `INC`, `NEG`, `NOT`, `OR`, `SBB`, `SUB`,
|`XOR`, `XADD`, and `XCHG`.  If the `LOCK` prefix is used with
|one of these instructions and the source operand is a memory
|operand, an undefined opcode exception (`#UD`) may be
|generated.  An undefined opcode exception will also be
|generated if the `LOCK` prefix is used with any instruction not
|in the above list.  The `XCHG` instruction always asserts the
|`LOCK#` signal regardless of the presence or absence of the
|`LOCK` prefix.

So while the last sentence seems independent of operands, its
presence in this paragraph leads me to believe that it only
applies in the context of a destination operand that is a
memory operand.

If not, however, an interesting question is, does an `XCHG` that
only specifies register operands affect memory ordering?
Looking at secs 11.1 and 11.2, I believe the answer is no: the
description of locked instructions explicitly only applies to
those with memory operands.

	- Dan C.
Technical Details
Message-ID: <11atq3a$nqi$1@reader2.panix.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
References: <2026Oct13.104347@mips.complang.tuwien.ac.at> <11arg45$2p70p$1@dont-email.me> <2026Oct16.075055@mips.complang.tuwien.ac.at> <20261016113441.00002a5f@yahoo.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
From: cross@spitfire.i.gajendra.net (Dan Cross)
Newsgroups: comp.arch
Subject: Re: Performance of xchg reg,reg
Date: Fri, 16 Oct 2026 18:23:38 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <11atq3a$nqi$1@reader2.panix.com>
References: <2026Oct13.104347@mips.complang.tuwien.ac.at> <11arg45$2p70p$1@dont-email.me> <2026Oct16.075055@mips.complang.tuwien.ac.at> <20261016113441.00002a5f@yahoo.com>
Injection-Date: Fri, 16 Oct 2026 18:23:38 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
	logging-data="24402"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)