2

So I'm learning MIPS right now and I saw on the MIPS green sheet that there are a total of 12 registers (including the s registers) that are preserved across a call. In my understanding of this, one must stack all such registers and retrieve them later when we want access to them again.

However my question is if there was the trouble of going through this, why bother even using these registers? Is there a point of using these registers other than possibly running out of registers to use?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Edasaur
  • 397
  • 1
  • 8
  • 19
  • Related: [What are callee and caller saved registers?](https://stackoverflow.com/a/56178078) explains the concept, including that the easiest way to leave a call-preserved register unmodified is not to touch it at all. Usually you only use them in non-leave functions. – Peter Cordes Oct 02 '21 at 00:22

1 Answers1

9

MIPS assembly has a calling convention which specifies that the t registers are caller saved, and the s registers are callee saved.

This means that if you call somebody else's function, say mine, you can gaurantee that the s registers will be the same in your function after my function exits. You cannot gaurantee this for the t registers, which my function is free to overwrite.

When writing MIPS assembly you should always follow this convention.

Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
  • 1
    So a good analogy for this would be that t registers are like local variables in a function and s registers are global variables that cannot be changed within a function? – Edasaur Oct 10 '13 at 03:33
  • 2
    Close. Except that you can edit `s` registers in a function, you just have to save and restore there values so they appear to have not changed. – Konrad Lindenbach Oct 10 '13 at 03:45
  • Your [other answer](https://stackoverflow.com/a/20112240/6243352) says "the `$t` variables are temporary caller saved registers, while the `$s` registers are callee saved." while this post says "the `t` registers are callee saved, and the `s` registers are caller saved.". I think this one is wrong. – ggorlen Dec 03 '20 at 02:55