Lecture 7 - ISA - J-Type Function Calls EECS 388
Lecture 7 - ISA - J-Type Function Calls EECS 388
Jacob Schoonover
Lecture notes based in part on slides created by Alex Fosdick,
Heechul Yun, Mohammad Alian, and Tamzidul Hoque
1
Agenda & Announcements
• Function calls with j-type
• Quiz
Monday, September 16: Last day to drop a full-term class and not
have it recorded on the transcript 'W'.
Monday, September 23: Last day to add a class with permission.
Last day for 50% refund
2
What is wrong with this code?
3
Lab 2 – Challenge Question
4
Context
Application Program
Operating
Compiler System
Digital Logic
Electronic Circuits
Transistors
5
Register Naming in MIPS
6
7
Function call or Procedures
• A function commonly used in high level programs like C or Java
• Makes code readable
• Allows code reuse Callee function
8
Memory Map of a Running
Program
A typical memory representation of a
C program consists of: Memory
1. Static Highest address
-Text/code segment
-Initialized data segment
-Uninitialized data segment Text segment of the memory
500
2. Stack
440
3. Heap Code of
GCD
400
Code of 200
Main
100
Lowest address 0
9
Memory Map of a Running
Program
Program Counter (PC): Memory
• A register that holds the address of Highest address
the next instruction to be executed
• Usually, the PC is incremented
after fetching an instruction Text segment of the memory
500
Example:
Code of …. 440
• Main() starts from address 100 GCD() Instruction 2
• PC=100 Instruction 1 400
• When instruction 1 is fetched, Code of …. 200
PC=PC+4=104, which is address of Main() Instruction 2
Instruction 1 100
instruction 2
Lowest address 0
10
Caller and Callee
13
How to return from the callee?
• We have the return address
void main(){
• Use jump register instruction: jr $ra
• Makes PC=$ra and starts executing m=1;
from where we left n=2;
o=sum(m,n); ---Address 500
p=m+1;
Question: }
• What is the value of PC after sum()
execution completes? Int sum (int a, int b){
• What instruction is in that address? return a+b;
}
14
Register Management during
function call
• Callee could overwrite registers that are currently in use by the caller program.
• Caller function main() stores variable m and n in register R1 and R3
• Assume that all other registers are occupied
void main(){
m=1;
Register Memory
n=2; Register
m=1
o=sum(m,n);
n=2
p=m+1;
}
o=sum(m,n);
p=m+1;
}
17
Who saves the registers?
• Who is responsible for saving important registers across function
calls?
• The caller knows which registers are important to it and should be saved.
• The callee knows exactly which registers it will use and potentially
overwrite.
• So how can two functions cooperate and share registers when they
don’t know anything about each other?
18
Approach 1: Caller saves all
registers
• One possibility is for the caller to
save any important registers main: li $a0, 3
that it needs before making a li $a1, 1 Note: The li is a
function call, and to restore li $s0, 4 pseudo instruction
them after. li $s1, 1 that loads an
immediate value into
# Save registers a register.
• But the caller does not know # $a0, $a1, $s0, $s1
what registers are actually
written by the function, so it may jal func
save more registers than
necessary. # Restore registers
# $a0, $a1, $s0, $s1
19
Approach 2: Callee saves all
registers
• Another possibility is if the callee
saves and restores any registers it func:
might overwrite. # Save registers
# $a0 $a2 $s0 $s2
• Slide taken and modified from CS232: Computer Architecture II. University of Illinois at Urbana-Champaign.
20
Best Approach: Caller and Calle
work together
• MIPS uses conventions again to split the register spilling chores.
• The caller is responsible for saving and restoring any of the following caller-saved registers
that it cares about.
$t0-$t9 $a0-$a3 $v0-$v1
In other words, the callee may freely modify these registers, under the assumption that the
caller already saved them if necessary.
• The callee is responsible for saving and restoring any of the following callee-saved
registers that it uses. (Remember that $ra is “used” by jal.)
$s0-$s7 $ra
Thus the caller may assume these registers are not changed by the callee.
• Be especially careful when writing nested functions, which act as both a caller and a
callee!
• •Slide$ra
takenis
andtricky; it isCS232:
modified from saved by aArchitecture
Computer callee II.who is also
University aatcaller.
of Illinois Urbana-Champaign.
21
Caller and Calle work together
• This convention ensures that the caller and callee together save all of the important
register
• main() only needs to save registers $a0 and $a1, while func() only has to save registers
$s0 and $s2.
• Slide taken and modified from CS232: Computer Architecture II. University of Illinois at Urbana-Champaign.
22