stwcx.
Store Word Conditional Indexed
PPC440x5 CPU Core User’s Manual Preliminary
Page 424 of 589
instrset.fm.
September 12, 2002
stwcx.
Store Word Conditional Indexed
EA ← (RA|0) + (RB)
if RESERVE = 1 then
MS(EA, 4)
← (RS)
RESERVE
← 0
(CR[CR0])
←
2
0 || 1 || XER[SO]
else
(CR[CR0])
←
2
0 || 0 || XER[SO]
An effective address (EA) is formed by adding an index to a base address. The index is the contents of
register RB. The base address is 0 when the RA field is 0, and is the contents of register RA otherwise.
If the reservation bit contains 1 when the instruction is executed, the contents of register RS are stored into
the word at the EA and the reservation bit is cleared. If the reservation bit contains 0 when the instruction is
executed, no store operation is performed.
CR[CR0] is set as follows:
• CR[CR0]
0:1
are cleared
• CR[CR0]
2
is set to indicate whether or not the store was performed (1 indicates that it was)
• CR[CR0]
3
is set to the contents of the XER[SO] bit
Registers Altered
• CR[CR0]
Programming Notes
The lwarx and stwcx. instructions are typically paired in a loop, as shown in the following example, to create
the effect of an atomic operation to a memory area used as a semaphore between multiple processes. Only
lwarx can set the reservation bit to 1. stwcx. sets the reservation bit to 0 upon its completion, whether or not
stwcx. actually stored (RS) to memory. CR[CR0]
2
must be examined to determine whether (RS) was sent to
memory.
loop: lwarx # read the semaphore from memory; set reservation
“alter” # change the semaphore bits in the register as required
stwcx. # attempt to store the semaphore; reset reservation
bne loop # some other process intervened and cleared the reservation prior to the above
# stwcx.; try again
The PowerPC Book-E architecture specifies that the EA for the lwarx instruction must be word-aligned (that
is, a multiple of 4 bytes); otherwise, the result is undefined. Although the PPC440x5 will execute stwcx.
regardless of the EA alignment, in order for the operation of the pairing of lwarx and stwcx. to produce the
desired result, software must ensure that the EA for both instructions is word-aligned. This requirement is due
to the manner in which misaligned storage accesses may be broken up into separate, aligned accesses by
the PPC440x5.
stwcx. RS, RA, RB
31 RS RA RB 150 1
0 6 11 16 21 31