Tyler
Tyler

Reputation: 2639

MIPS: Saving the return address in nested procedures

Let's say I have:

main:

    // stuff

    jal function_a

function_a:

    // function_a stuff

    jal function_b

    jr $ra

function_b:

    // function_b stuff

    jr $ra

From what I understand, main saves the appropriate return address in $ra when it does jal function_a, but then function_a overwrites $ra when it does jal function_b, so obviously $ra needs to be saved at some point. But I can't find anything that actually says what the convention is for doing this. Do I store $ra on the stack while in function_a before calling jal function_b? Do I store $ra in an s-register before calling jal function_b, and then save that s-register at the beginning of function_b (which I think was implied here)? Something different? Does it matter?

Upvotes: 0

Views: 1796

Answers (1)

Jester
Jester

Reputation: 58762

The usual practice is to save $ra on the stack directly. Storing it in (say) $s0 doesn't make much sense, since according to normal calling convention function_a must also preserve $s0, so first you'd have to save $s0 on the stack to make room for $ra. If you don't follow normal calling convention then you can do whatever you want, of course.

Upvotes: 1

Related Questions