Use LL/SC instructions to construct an atomic compare and swap instruction “CAS R1, R2, L”

Use LL/SC instructions to construct an atomic compare and swap instruction “CAS R1, R2, L” which tests whether data in memory location L is equal to that in R1. If they are equal, write the value in R2 to L, and copy R1 to R2. Otherwise, nothing is done and CAS returns. For example, if initially R1=5, R2=10, L=5, after CAS we have R1=5, R2=5, and L=10. If initially R1=7, R2=10, L=5, nothing changes after CAS. Show your answer in assembly code, and annotate what each instruction does. Keep as few instructions between LL and SC as possible.

The Correct Answer and Explanation is :

To construct the atomic compare and swap (CAS) instruction using LL/SC (Load-Linked/Store-Conditional) instructions, we can use the following assembly code. LL/SC is typically used to implement atomic operations like CAS, ensuring that the memory operation is performed atomically and is only successful if the memory location has not been modified by another process or thread.

CAS Assembly Code (using LL/SC):

CAS:
    LL    R3, L         ; Load linked value from memory location L into R3
    CMP   R1, R3        ; Compare R1 (the expected value) with R3 (value in memory)
    BNE   CAS_DONE      ; If R1 ≠ R3, branch to CAS_DONE (nothing to do)

    SC    R4, R2, L     ; Store conditional: attempt to store R2 to L
    BEQ   CAS_SUCCESS   ; If store conditional succeeded (R4 = 0), branch to CAS_SUCCESS

CAS_DONE:
    ; If CAS is not successful, do nothing, return.

CAS_SUCCESS:
    MOV   R1, R3        ; Move the value from R3 (the old value of L) to R1
    MOV   R2, R3        ; Move the value from R3 to R2
    RET                 ; Return from the CAS function

Explanation of Each Instruction:

  1. LL R3, L: The LL instruction loads the value from memory location L into register R3. This instruction is part of the atomic pair used in LL/SC operations. It effectively marks the start of the atomic operation.
  2. CMP R1, R3: The CMP instruction compares the value in R1 (which is the expected value) with the value in R3 (the current value of L). If these values are different, the CAS operation cannot proceed.
  3. BNE CAS_DONE: If the comparison between R1 and R3 shows they are not equal (BNE stands for “Branch if Not Equal”), the function branches to CAS_DONE without making any changes, since the value in memory doesn’t match the expected value.
  4. SC R4, R2, L: If the comparison succeeds (i.e., R1 equals R3), the SC (Store-Conditional) instruction attempts to store the value in R2 to memory location L. If no other processor or thread has modified L since the LL instruction, the store is successful, and R4 will be set to 0.
  5. BEQ CAS_SUCCESS: If the SC instruction succeeded (R4 == 0), the function branches to CAS_SUCCESS, where the actual swap and return will take place.
  6. MOV R1, R3: Once the store succeeds, the old value of L (which is in R3) is moved to R1, so that R1 holds the original value of L.
  7. MOV R2, R3: Similarly, the old value of L is moved to R2, ensuring that R2 gets the old value from memory.
  8. RET: The function returns after successfully completing the CAS operation.

CAS Behavior:

  • If the memory value at L is equal to R1, the CAS operation stores R2 into L, updates R1 and R2 with the old value of L, and the operation completes successfully.
  • If the memory value at L is different from R1, no changes are made, and the function returns without doing anything.

Atomicity:

  • The LL and SC instructions ensure that the entire compare-and-swap operation is atomic. If any other process modifies the memory location between the LL and SC, the store fails, and the operation does not proceed.
Scroll to Top