[ Previous | Next | Contents | Home | Search ]
AIX Version 4.3 Assembler Language Reference

lwarx (Load Word and Reserve Indexed) Instruction

Purpose

Used in conjunction with a subsequent stwcx. instruction to emulate a read-modify-write operation on a specified memory location.

Note: The lwarx instruction is supported only in the PowerPC architecture.

Syntax

PowerPC
lwarx RT,RA,RB

Description

The lwarx and stwcx. instructions are primitive, or simple, instructions used to perform a read-modify-write operation to storage. If the store is performed, the use of the lwarx and stwcx. instructions ensures that no other processor or mechanism has modified the target memory location between the time the lwarx instruction is executed and the time the stwcx. instruction completes.

If general-purpose register (GPR) RA = 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 lwarx instruction loads the word from the location in storage 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 lwarx instruction has one syntax form and does not affect the Fixed-Point Exception Register. If the EA is not a multiple of 4, the results are boundedly undefined.

Parameters

RT Specifies target general-purpose register where result of operation is stored.
RA Specifies source general-purpose register for EA calculation.
RB Specifies source general-purpose register for EA calculation.

Examples

  1. The following code performs a "Fetch and Store" 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. 
  2. The following code performs a "Compare and Swap" by atomically comparing a value in a register with a word in storage:
    # 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.
    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.

Related Information

The stwcx. (Store Word Conditional Indexed) instruction.

Processing and Storage


[ Previous | Next | Contents | Home | Search ]