43

So far I have been using registers $s0-$s9 and same way as registers $t0-$t9. I have been told and read countless forum posts and paragraphs here and on Google explaining the difference, but with no coding examples. I'm told I should see the difference it pertain to using procedures, but I have created a countless number of scenarios involving procedures in an effort to find the difference between saved registers vs temporary register but I have failed.

I would like to see a relatively simple example where a $t0-9 register would not act the same as a $s0-9 register and, as a result, produce a different value?

Gingerbread
  • 53
  • 1
  • 4
  • 9
shawn a
  • 799
  • 3
  • 13
  • 21

1 Answers1

84

There is no difference between the temporary and saved variables in how they work. The difference is in how they are used, or rather, how they ought to be used.

The MIPS calling convention specifies how the various registers are to be used -- the $v registers are for function returns, the $a registers are for function arguments, the $t variables are temporary caller saved registers, while the $s registers are callee saved.

The difference between callee and caller saved is as follows: when calling a function, the convention guarantees that the $s registers are the same after return whereas the convention does not guarantee this for the $t registers. Of course this means that if you wish to use the $s registers in a routine, you must save and restore their values. For instance, if function A uses registers $t0 and $s0 and then calls a function B, it must save the register $t0 if it wants to use it after function B returns. Function B must save $s0 before it can begin using it.

An example:

main:

    li $s0 7
    li $t0 7

    jal myFunction

    #$s0 guaranteed to equal 7
    #$t0 value not guaranteed

This link looks like some decent more in-depth information.

Of course, all of this is only a convention, and therefore it only works if you and the other programs respect the convention by saving and restoring $s registers so that they are not overwritten by a function call.

Community
  • 1
  • 1
Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
  • 2
    To reply to the poster's plea for a "relatively simple example" where $t behaves differently from $s, you could add the definition of myFunction: `addi $t0, $0, 255` `jr $ra`. – RobertB Nov 21 '13 at 14:39
  • 1
    @RobertB I would think that would be overly confusing. The user is free to change the value of `$s0` in `myFunction` as well, it's just that they are _supposed_ to put it back afterwards. – Konrad Lindenbach Nov 27 '13 at 21:45
  • 1
    What exactly did you mean though by "Function B must save $s0 before it can begin using it."... What do you mean by save? $s0 is already a saved value. And the main function call already 'saved' $s0. –  Dec 19 '16 at 17:59
  • 2
    I like the terminology *call-preserved* vs. *call-clobbered*. That avoids implying that anyone has to do any saving/restoring of registers if they don't care about the value, or aren't going to modify them anyway. The normal case is that you use call-clobbered registers for values you won't need after the next function call, so it's ok to let those values die, and consider the registers / values dead after the function call. Also, the terms differ by more than one letter, and talk about both kinds of registers from the same perspective (of the current function using them). – Peter Cordes Mar 24 '18 at 04:26
  • Why not only caller saved registers? I don't see a need of callee saved registers. Can anyone please share a case where callee saved registers are required? – mejariamol Jun 15 '20 at 18:40
  • Does static variable in c use registers like `t` in MIPS? – NAND Jan 23 '21 at 21:48
  • 1
    @mejariamol to make sure everything works fine, regardless of respecting or not the convention, the callee can start by saving in the stack all the registers it will modify and then restore them before returning. This way the caller does not have to save any register, the ones affected are preserved by the callee. – Helder Daniel Feb 19 '22 at 16:52