Using MIPS Assembly's Registers
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.
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.
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.
Thanks for the response. In finishing, how can you convert a c++ pseudocode into mips such as this?
int age = 22; // local variable
int age = 22; // local variable
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
addi $t1, $zero, 22
move $t1, 22
Edit: think i fixed it, i forgot the syntax
Cool, thanks. Is the reason why you used a temporary register because it's local? And Saved Temps are global?
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;
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;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement