MIPS assembly language - temporary register vs sav

2020-02-16 07:10发布

问题:

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?

回答1:

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.



标签: assembly mips