Hi I am studying computer architecture. We learned that we usually use temporary registser($t) to temporarily store values ββin registers. However, I saw a data that uses the stack pointer ($sp) to temporarily store the value of the saved register ($s) in the function. Can't we just use temporary register($t) instead of this? I think the stack pointer and the temporary register do the same thing, but I'm curious what exactly is the difference between the two.
Asked
Active
Viewed 70 times
0
-
1Are you talking about stack *memory* pointed-to by the stack pointer? Consider a non-leaf function that needs some values after it calls another function. Of course in leaf functions you're right, you should just use `$t` registers unless you run out and need more than 10 or so values live at once, or I guess 16 with $t0..9, $v0..1, $a0..3 in the standard MIPS calling convention. See also [call-clobbered vs. call-preserved registers](https://stackoverflow.com/questions/9268586/what-are-callee-and-caller-saved-registers/56178078#56178078) for how they're used in non-leaf functions. β Peter Cordes Sep 27 '21 at 12:02
-
1Yes, the registers are all the same as far as the hardware is concerned. But no, you almost certainly misunderstood the code you're referring to (but aren't showing it so we cannot say for sure). By software convention/agreement: `$sp` has special usage as the stack pointer register; Registers in the temporary category don't survive a function call, so if there is a call involved and you want a variable to survive that call, then stack memory is used, either directly for the variable or to preserve a `$s` register's original value before using it for the variable. β Erik Eidt Sep 27 '21 at 15:13