Search code examples
mipsmips32

Is it possible to load-linked a register to itself?


Is it possible to load-linked a register to itself?

There is a line of MIPS(R2000) assembly code in my textbook says:

ll    $r2, 0($r2)

Is this correct?

If it's correct: Is this instruction used when the original content in $r2 is not important but one only want to store a value into $r2 and he doesn't want others to store at the same time?


Solution

  • LL does not prevent anyone from storing to the address that is loaded, it only enables checking if someone did.

    The LL instruction you posted looks legal to me. Whether it would make sense at that point depends on what else the program does.

    It is possible that the address is also stored somewhere else and later read from someplace else for a call to SC.

    Modified example from the reference manual:

    L1:
      ADD T1, ZERO, T0 # copy T0 -> T1
      LL T1, (T1)      # 
      ADDI T2, T1, 1   # increment
      SC T2, (T0)      # try to store, checking for atomicity
      BEQ T2, 0, L1    # if not atomic (0), try again