0

have a really basic question here. Can a register have both a value and an address. As in assuming i want to swap between values: 5 stored in t0 and 7 stored in t1 does this code work:

sw $t0, 0($t0)
sw $t1, 0($t1)
lw $t1, 0 ($t0)
lw $t0, 0 ($t1)

Sorry this might sound stupid

Rani Balaa
  • 107
  • 3
  • 3
  • 7

1 Answers1

1

Not really for all values, as sw and lw need proper alignment (valid addresses should be multiple of 4).

That is, your code would only work for values multiple of 4, and anyways it would be a bad idea to do so, because you would be basically write garbage on whichever address you are pointing at.

To swap registers without overwriting a third register you can use the following trick:

 xor $t0, $t0, $t1
 xor $t1, $t0, $t1
 xor $t0, $t0, $t1
gusbro
  • 22,357
  • 35
  • 46