Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this.

Similar presentations


Presentation on theme: "Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this."— Presentation transcript:

1 Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this address does not have to remain in $ra all the time the subroutine is running. It works fine to save the value in $ra somewhere. The value can be restored, later on, when it is time to return to the caller. In the picture, the operating system calls main. The return address to the operating system is in $ra. As soon as it gets control, main pushes the return address on the stack (step 1). The return address that main should use when it returns to the operating system is now on the top of the stack. In the picture, the operating system calls main. The return address to the operating system is in $ra. As soon as it gets control, main pushes the return address on the stack (step 1). The return address that main should use when it returns to the operating system is now on the top of the stack. One return address register $ra

2 Pushing the Return Address After pushing the return address, main computes for a bit, and then calls subC using a jal instruction (step 2). This puts the return address to main in $ra. Luckily, it does not matter that $ra has been changed because the return address that main will use to return to the operating system is safely on the stack. After pushing the return address, main computes for a bit, and then calls subC using a jal instruction (step 2). This puts the return address to main in $ra. Luckily, it does not matter that $ra has been changed because the return address that main will use to return to the operating system is safely on the stack. One return address register $ra

3 Pushing the Return Address subC receives control and computes for a while. Because it does not call another subroutine, subC does not alter $ra and does not need to save it on the stack. When subC returns to main it uses a jr $ra instruction (step3). subC receives control and computes for a while. Because it does not call another subroutine, subC does not alter $ra and does not need to save it on the stack. When subC returns to main it uses a jr $ra instruction (step3). One return address register $ra

4 Pushing the Return Address Control returns to main, which computes for a while longer. It returns to the OS by popping the stack into $ra and executing a jr $ra instruction (step 4). Control returns to main, which computes for a while longer. It returns to the OS by popping the stack into $ra and executing a jr $ra instruction (step 4).

5 Chain of Subroutine Calls One return address register $ra A subroutine that might call another subroutine must keep the current return address from $31-$ra somewhere It pushes the return address onto the stack. When it returns to its caller, it pops the stack to get the return address into $31-$ra. How do deal with the registers recursive usage ? First lets resolve the $ra problem. How about the other registers saving ?

6 Stack-based Linkage Convention Calling a Subroutine (done by the caller ): 1. Push onto the stack any registers $t0-$t9 that contain values that must be saved. The subroutine might change these registers. 2. Put argument values into $a0-$a3. 3. Call the subroutine using jal.

7 Stack-based Linkage Convention Subroutine Prolog (done by the subroutine at its beginning): 4. If this subroutine might call other subroutines, push $ra onto the stack. 5. Push onto the stack any registers $s0- $s7 that this subroutine might alter. Subroutine Body: 6. The subroutine may alter any "T" or "A" register, or any "S" register that it saved in the prolog (step 5). 7. If the subroutine calls another subroutine, then it does so by following these rules.

8 Stack-based Linkage Convention Subroutine Epilog (done by the subroutine just before it returns to the caller): 8. Put returned values in $v0-$v1 9. Pop from the stack (in reverse order) any registers $s0-$s7 that were pushed in the prolog (step 5). 10. If it was pushed in the prolog (step 4), pop $ra from the stack. 11. Return to the caller using jr $ra.

9 Stack-based Linkage Convention Return from a Subroutine (done by the caller): 12. Pop from the stack (in reverse order) any registers $t0-$t9 that were previously pushed (step 1).

10 How to “push” and “pop” registers ? sub $sp,$sp,4 # push the return address sw $ra,($sp) sub $sp,$sp,4 # push $s0 sw $s0,($sp)... jal conChar # call other subroutine... lw $s0,($sp) # pop $s0 add $sp,$sp,4 lw $ra,($sp) # pop return address add $sp,$sp,4 jr $ra # return to caller


Download ppt "Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this."

Similar presentations


Ads by Google