Caller%20Must%20Do - PowerPoint PPT Presentation

About This Presentation
Title:

Caller%20Must%20Do

Description:

Return by jumping to the address in register $ra using jr. week04-3.ppt. 7 ... additional parameters using the stack. week04-3.ppt. 8. Example: Five Parameters ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 17
Provided by: Zhen67
Learn more at: http://www.cs.fsu.edu
Category:
Tags: 20do | 20must | caller | register

less

Transcript and Presenter's Notes

Title: Caller%20Must%20Do


1
(No Transcript)
2
Caller Must Do
  • Before it calls a procedure/function, it must
  • Pass parameters
  • Up to four parameters are passed by a0 - a3
  • Save caller-saved registers on the stack
  • It includes a0 - a3 (since the callee may
    change these values), s0 - s9, and ra
  • Why ra?
  • Execute a jal instruction, which jumps to the
    callees first instruction and save the next
    instruction in ra

3
Caller Must Do cont.
  • After the procedure/function call, it needs to
  • Read the returned values from v0 and v1
  • Restore caller-saved registers

4
Callee Must Do
  • Before it does its calculations, the callee must
    do the following
  • Allocate memory for its frame by subtracting its
    frame size from the stack pointer
  • Save callee-saved registers in the frame
  • It must save s0 - s7, fp, ra before changing
    them
  • ra needs to be saved if the callee itself makes
    a call
  • When needed, establish the frame pointer fp by
    loading sp to it
  • In this case, fp must be saved

5
Allocating Space for Local Data on Stack
  • In MIPS, local variables are also stored on the
    stack
  • The segment of the stack containing a procedures
    saved registers and local variables is called a
    procedure frame (or activation record)

6
Callee Must Do
  • After its calculations and before it returns to
    the caller, the callee must
  • Place the return values in v0 and v1 (if
    needed)
  • Restore all the callee-saved registers that were
    saved at the procedure entry
  • Pop the stack frame by adding the frame size to
    sp
  • Return by jumping to the address in register ra
    using jr

7
If There are Five or More Parameters
  • We know what to do if we have up to four
    parameters
  • What if a procedure needs five or more
    parameters?
  • We pass the additional parameters using the stack

8
Example Five Parameters
9
Example Five Parameters cont.
10
Nested Procedures
  • When calling non-leaf procedures (procedures that
    can call other procedures), more care needs to be
    taken
  • A best example in this case is a recursive call

11
The Recursive Function
  • The function is a caller as well as a callee
  • The base case of the function is not difficult to
    write

12
The Recursive Function
  • The recursive part is not difficult
  • What will happen?

13
The Recursive Function
  • The recursive part is not difficult
  • What is wrong?
  • We need to save a0 and ra registers since they
    are changed

14
The Recursive Function
15
The Stack During Recursion
16
.data array .word 12, 34, 67, 1, 45, 90, 11,
33, 67, 19 msg_done .asciiz "done!\n" .text
.globl main main li a0, 4 jal
fact done li v0,4 la a0,msg_done syscall
jr ra fact addi sp, sp, -8 sw ra,
4(sp) sw a0, 0(sp) slti t0, a0, 1 beq
t0, zero, L1 addi v0, 0, 1 addi sp, sp,
8 jr ra L1 addi a0, a0, -1 jal fact lw
ra, 4(sp) lw a0, 0(sp) mul v0, v0,
a0 addi sp, sp, 8 jr ra
Write a Comment
User Comments (0)
About PowerShow.com