Sign in to follow this  
The Plan 9 Hacker

Using MIPS Assembly's Registers

Recommended Posts

Hi, I just started the class here at my college, and I'm beginning to wonder when it's appropriate to use $t1-9, $s1-9, etc. I know its for using functions such as $a being passed to one. But how can I just use a assm local variable that would be used in main() like in c++? just $s1 = 22 # age? I'm lost.

Share this post


Link to post
Share on other sites
registers $t1-t9 are for temporary values.
Edit: I mean if you are in a function, you can use the $t registers without worrying, b/c the "previous" function would have saved them on the stack if it needed them.( you will learn about the stack later).


The $s registers are for "save" values. They are kind of like global variables in c++.




Of course, in the "mips computer" there is no physical difference between most of the registers( a1, s1, t1). They are just a way for programs to be easily written and understood by others.

Share this post


Link to post
Share on other sites
there are many ways to do this, most common are:


addi $t1, $zero, 22

move $t1, 22

Edit: think i fixed it, i forgot the syntax

Share this post


Link to post
Share on other sites
It's not "saved temps", it's saved registers. It is up to you if you want to use $t0, or $s0. Both will work the same. But in "practice", $t's are used for values you don't mind losing due to a function call. $s's are values you don't want other functions touching.


pseudocode:

main:
t1 = x + 1;
dosomething();
y = t1 + 2;


^ This would be dangerous b/c dosomething() could have used t1 and changed it. So you can "solve" this by using $s's , b/c it is understood that dosomething() will not change $s registers (this of course may not be true)

s1= x + 1;
dosomething();
y = s1 + 2;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this