0

Do MIPS registers act differently? for example, there are saved registers and temp registers, can they be used interchangeably? Will temp registers possibly be over written by the OS, while saved registers wont (hence their name) or is it purely convention?

toastedDeli
  • 570
  • 5
  • 28
  • 1
    purely Convention. Temp registers will not be over written by the OS. – Omid CompSCI Nov 02 '16 at 17:13
  • Take a look at this post: http://stackoverflow.com/questions/20111326/mips-assembly-language-temporary-register-vs-saved-registers – Omid CompSCI Nov 02 '16 at 17:14
  • @OmidCompSCI I just had a look and seen this: `main: li $s0 7 li $t0 7 jal myFunction #$s0 guaranteed to equal 7 #$t0 value not guaranteed ` Why is $t0 not guaranteed to hold the value of 7? – toastedDeli Nov 02 '16 at 17:18
  • 1
    "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." He is saying if it is after a function call. Otherwise those lines itself without jal myFunction would work the same. – Omid CompSCI Nov 02 '16 at 17:21
  • @OmidCompSCI how do you "save" a register? – toastedDeli Nov 02 '16 at 17:22
  • "Save register $t0" can be as simple as add $t0 $t0 $0, I don't remember exact syntax for add, but I believe if that is the order As long as this is right before the function returning – Omid CompSCI Nov 02 '16 at 17:23
  • @OmidCompSCI So when using a jump function it's possible for a register to be overwritten? and to avoid it you should save registers after each jump? – toastedDeli Nov 02 '16 at 17:27

1 Answers1

2

MIPS has 32 general-purpose registers and another 32 floating-point registers. Temporary registers are general-purpose registers that can be used for arithmetic and other instructions freely, while saved registers must keep their value across function calls.

The system service calls follow the standard calling convention in that the temporary registers ($t0 - $t9) may be altered and the saved registers ($s0 - $s7, $fp, $ra) will be preserved.

Integer registers $t0 - $t9 and floating-point registers $f4 - $f10 and $f16 - $f18 (single or double precision) are used to hold temporary quantities that do not need to be preserved across procedure calls.

As such, if a series of values is being printed in a loop, a saved register would be required for the loop counter and the current array address/index.

Example: 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.

.data
newline: .asciiz "\n"
.text
.globl main

main:
addi $s0, $zero, 5
# li $t0, 5

jal increment 


li $v0, 4
la $a0, newline    #print newline string
syscall

move $a0, $s0       #print saved register after calling function --> output = 5
li $v0, 1        
syscall

end:
li $v0, 10
syscall

increment:
addi, $sp, $sp, -4      # to store old value of--> -4 because store in 4 bytes (allocated in stack)
sw $s0, 0($sp)          # save the value of $s0 to the first location in stack pointer  

addi $s0, $s0, 1        # add 1 

move $a0, $s0
li $v0, 1               #print saved register after calling function  --> output = 6
syscall

lw $s0, 0($sp)          # will load previous value 
addi $sp, $sp, 4

jr $ra

Even though saved register value was incremented in function, after function ended we could still access the original value.

Areesha
  • 21
  • 3