Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIPS assembly language - temporary register vs saved registers

Tags:

assembly

mips

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?

like image 887
shawn a Avatar asked Nov 21 '13 02:11

shawn a


People also ask

What are saved registers in MIPS?

MIPS calls these saved registers, and designates $s0-s8 for this useage. the called procedure saves register values in its AR, uses the registers for local variables, restores register values before it returns.

What are the different types of registers MIPS?

MIPS has two primary types of registers, integer registers and floating point registers. In addition, MIPS has a small number of special purpose control registers. There are register usage conventions that specify how main programs and subprograms should coordinate their use of registers.

What are temporary registers?

A temporary register is the only register that can be read and written more than once in a single instruction. Each temporary register has single-write and triple-read access. Therefore, an instruction can have as many as three temporary registers in its set of input source operands.


1 Answers

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.

like image 109
Konrad Lindenbach Avatar answered Oct 21 '22 22:10

Konrad Lindenbach