0% found this document useful (0 votes)
24 views18 pages

CSE 230 - MIPS Procedure Calls

The document discusses MIPS procedure calls, detailing the execution steps for caller and callee functions, including argument passing and return value handling. It explains the roles of caller and callee saved registers, the importance of stack usage for register spilling, and provides examples of nested and recursive calls. Additionally, it includes MIPS code demonstrations for procedure execution and register management.

Uploaded by

ashyam3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views18 pages

CSE 230 - MIPS Procedure Calls

The document discusses MIPS procedure calls, detailing the execution steps for caller and callee functions, including argument passing and return value handling. It explains the roles of caller and callee saved registers, the importance of stack usage for register spilling, and provides examples of nested and recursive calls. Additionally, it includes MIPS code demonstrations for procedure execution and register management.

Uploaded by

ashyam3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

MIPS Procedure Calls

Dr. Soumya Indela


Topics
• Procedure Execution
• Register Spilling
Procedure Execution
• Procedure – Function for better structure
• Procedure Execution – Steps in MIPS to execute functions
• Types of Procedure/Function Calls:
• Single Procedure Call – caller calls callee
• Nested Procedure Call – caller calls nested function which calls callee
• Recursive Procedure Call – caller calls recursive function which calls itself
Procedure Execution
• Caller Function
• Assign Arguments ($a Registers)
• Transfer control to the procedure (jal)
• Callee Function
• Perform the desired task.
• Assign Return Values ($v Registers)
• Return control to the point of origin (jr $ra)
Procedure Execution
• Caller Function
• Assign Arguments ($a Registers)
• Transfer control to the procedure (jal)
• Callee Function
• Perform the desired task.
• Assign Return Values ($v Registers)
• Return control to the point of origin (jr $ra)
• Caller + Callee Function
• Task of Caller
• Mandatory use of Stack to store arguments and return address
• Task of Callee
Registers for Procedure Execution
• $a0 – $a3: four argument registers to pass parameters (passed from
caller to callee)
• $v0 – $v1: two value registers to return values (passed from callee
to caller)
• $ra: one return address register to return to the point of origin
Caller Saved Registers
• Volatile Registers
• Modified during Procedure call
• Caller’s responsibility to save these registers to stack before “jal”
• Registers include:
• $ra
• $v0 – $v1
• $a0 - $a3
• $t0 - $t9
Callee Saved Registers
• Saved Registers
• Not modified during Procedure call
• Callee’s responsibility to restore original values, if changed, before “jr”
• Registers include:
• $zero – No change (always 0)
• $s0 – $s7
• $sp – Stack pointer should point to same location
before and after function call (jal)
Otherwise, caller can’t restore from stack correctly
Procedure Execution - Example
• C Code:
int main () {
int x, y, z;
z = avg (x, y);
}

int avg (int x, int y) {


int t = sum(x, y);
return(t/2);
}

int sum (int x, int y)


return(x+y);
Nested or Recursive Procedure
• Function call inside a function - Both Caller and Callee
• “jal” inside function before “jr”
• “jal” modifies “$ra”
• Before “jal”, “$a” registers are modified
• Required to store ”$a” , “$ra” before modifications (Spill $a, $ra registers)
MIPS Memory Layout

high address
Stack Space for saved procedure information
$sp (Note: the stack is up side down.)
stack
pointer Heap Explicitly created space, (e.g., malloc() in C)
Static Variables declared once per program
Code Programs
reserved
address 0
Register Spilling using Stack
• Stack – Last In First Out Structure
• Stack Pointer ($sp) – Always points to last used space in Stack
• Register Spilling – Write data from Registers to Memory
• To Push to stack
• Decrement $sp
• Write to Memory (using $sp as reference)
• To Pop or retrieve from Stack
• Read from Memory (using $sp as reference)
• Increment $sp
Register Spilling using Stack

addi $sp, $sp, -12


sw $t1, 8($sp)
sw $t0, 4($sp)
sw $s0, 0($sp)
Register Spilling using Stack

lw $s0, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
addi $sp, $sp, 12
Zylab Demo
• MIPS Code:

Main: addi $a0, $s0, 0


jal Func
addi $s1, $v0, 0
j Exit

Func: add $v0, $a0, $a0


jr $ra

Exit:
Zylab Demo
• MIPS Code:

Store: addi $sp, $sp, -8


sw $t1, 4($sp)
sw $t0, 0($sp)

Retrieve: lw $t0, 0($sp)


lw $t1, 4($sp)
addi $sp, $sp, 8
Questions
Thank you!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy