ldarx (Load Doubleword Reserve Indexed) instruction
Purpose
The ldarx instruction is used in conjunction with a subsequent stdcx instruction to emulate a read-modify-write operation on a specified memory location.
Syntax
Bits | Value |
---|---|
0 - 5 | 31 |
6 - 10 | RT |
11 - 15 | RA |
16 - 20 | RB |
21 - 30 | 84 |
31 | / |
Description
The ldarx and stdcx (Store Doubleword Conditional Indexed) instructions are used to perform a read-modify-write operation to storage. If the store operation is performed, the use of the ldarx and stdcx instructions ensures that no other processor or mechanism changes the target memory location between the time the ldarx instruction is run and the time the stdcx instruction is completed.
If general-purpose register (GPR) RA equals 0, the effective address (EA) is the content of GPR RB. Otherwise, the EA is the sum of the content of GPR RA plus the content of GPR RB.
The ldarx instruction loads the word from the location in storage that is specified by the EA into the target GPR RT. In addition, a reservation on the memory location is created for use by a subsequent stwcx. instruction.
The ldarx instruction has one syntax form and does not affect the Fixed-Point Exception Register. If the EA is not a multiple of 8, either the system alignment handler is invoked or the results are called undefined.
Parameters
Item | Description |
---|---|
RT | Specifies the source GPR of the stored data. |
RA | Specifies the source GPR for the EA calculation. |
RB | Specifies the source GPR for the EA calculation. |
Examples
- The following code performs a fetch and store operation by atomically
loading and replacing a word in storage:
# Assume that GPR 4 contains the new value to be stored. # Assume that GPR 3 contains the address of the word # to be loaded and replaced. loop: lwarx r5,0,r3 # Load and reserve stwcx. r4,0,r3 # Store new value if still # reserved bne- loop # Loop if lost reservation # The new value is now in storage. # The old value is returned to GPR 4.
- The following code performs a compare and swap operation by atomically
comparing a value in a register with a word in storage:
If the value in the register equals the word in storage, the value from a second register is stored in the word in storage. If they are unequal, the word from storage is loaded into the first register and the EQ bit of the Condition Register field 0 is set to indicate the result of the comparison.# Assume that GPR 5 contains the new value to be stored after # a successful match. # Assume that GPR 3 contains the address of the word # to be tested. # Assume that GPR 4 contains the value to be compared against # the value in memory. loop: lwarx r6,0,r3 # Load and reserve cmpw r4,r6 # Are the first two operands # equal? bne- exit # Skip if not equal stwcx. r5,0,r3 # Store new value if still # reserved bne- loop # Loop if lost reservation exit: mr r4,r6 # Return value from storage # The old value is returned to GPR 4. # If a match was made, storage contains the new value.