10

What is the use of a $zero register in MIPS?

What does it mean?

lw $t0, myInteger($zero)
user366312
  • 16,949
  • 65
  • 235
  • 452
  • Possible duplicate of [Can the MIPS register $0 be used to store and retrieve values?](https://stackoverflow.com/questions/46699358/can-the-mips-register-0-be-used-to-store-and-retrieve-values) – phuclv Jul 24 '18 at 15:04
  • this was mentioned at the beginning of every MIPS book and in a lot of questions here [$zero on MIPS really hardware zero?](https://stackoverflow.com/q/12541443/995714), [How does a zero register improve performance?](https://stackoverflow.com/q/24646101/995714), [How to set a floating point register to 0 in MIPS (or clear its value).](https://stackoverflow.com/q/22770778/995714) – phuclv Jul 24 '18 at 15:05
  • @phuclv, you are closing the older question in favor of a newer one? – user366312 Jul 24 '18 at 15:05
  • no one cares about time. [*The general rule is to keep the question with the best collection of answers*](https://meta.stackexchange.com/q/10841/230282) – phuclv Jul 24 '18 at 15:07
  • @phuclv, Donald Trump cares about time. – user366312 Jul 24 '18 at 15:10

1 Answers1

15

The zero register always holds the constant 0. There's not really anything special about it except for the fact that 0 happens to be a very useful constant. So useful that the MIPS designers dedicated a register to holding its value. (This way you don't have to waste another register, or any memory, holding the value.)


EDIT:

As for the question of what that line of code means, it loads the word from MEMORY[myInteger + 0] into the $t0 register. The lw command takes both a constant (myInteger) and a register ($zero). Not sure why that is, but that's just how the instructions work. Since myInteger was used as the constant, a register had to be provided, so $zero was used.

David Cary
  • 5,250
  • 6
  • 53
  • 66
GJK
  • 37,023
  • 8
  • 55
  • 74
  • lw $t0, myInteger($zero) – user366312 Aug 26 '15 at 18:23
  • `lw` is just the load word instruction. It load the constant 0 into the `$t0` register. I have no idea what `myInteger()` is, I've never seen that. I don't think that's part of normal MIPS assembly. – GJK Aug 26 '15 at 18:25
  • https://www.youtube.com/watch?v=zhO8CLaQrr8&index=8&list=PL5b07qlmA3P6zUdDf-o97ddfpvPFuNa5A – user366312 Aug 26 '15 at 18:27
  • I edited my answer. Here they're using `$zero` to represent a 0 offset from a memory address. The point stands about the zero register though. Look through the source and you'll probably see `myInteger` defined as a constant somewhere. – GJK Aug 26 '15 at 18:28
  • Why would they do that? I mean, why not use a plain command like `lw $t0, myInteger`? Why `lw $t0, myInteger($zero)`? – user366312 Aug 26 '15 at 18:34
  • I think that's just the way MIPS works. It requires a constant memory address and a register for the `lw` instruction. I guess they could have made a pseudo-instruction to shorten things a bit, but this is just how it is. – GJK Aug 26 '15 at 18:36
  • 1
    the only form of `lw` is `lw $dest, offset($source)`, so you can't have `lw $t0, myInteger`. When source is $zero then it's a special case of absolute addressing for a small address range – phuclv Jul 24 '18 at 15:04