Lalita
Lalita

Reputation: 31

MIPS assembly questions

I am working on my assignment and I have a few questions.

  1. Do I have to always push arguments/return value registers to the stack? If the answer is yes where do I do it? in caller or callee procedure?

  2. I am trying to optimize my instruction. I wonder if I can change from

    slt $t0, $a0, a1    #check if a<b
    beq $t0,$zero, ELSE     #if $a>b, $t0 = 0, go to else
    addi $v0, $zero,1       #a<b, return 1
    j    exit               #end if, jump to exit
    ELSE: addi $v1, $zero, 0 #a>b, return 0
    exit: jr  $ra           #return to address
    

    to

    slt  $v0, $a0, a1    #if a<b, $v0 =1 and if a>b, $v0 = 0
    jr   $ra             #return to address
    

Upvotes: 3

Views: 158

Answers (1)

Adam
Adam

Reputation: 846

MIPS divides registers into preserved and nonpreserved categories. The preserved registers include $s0 – $s7 (hence their name, saved). The nonpreserved registers include $t0 – $t9 (hence their name, temporary). A function must save and restore any of the preserved registers that it wishes to use, but it can change the nonpreserved registers freely.

The callee must save and restore any preserved registers that it wishes to use. The callee may change any of the nonpreserved registers. Hence, if the caller is holding active data in a nonpreserved register, the caller needs to save that nonpreserved register before making the function call and then needs to restore it afterward.

For these reasons, preserved registers are also called callee-save, and nonpreserved registers are called caller-save.

Upvotes: 1

Related Questions