Function Calls in MIPS To call a function: jal func

Slides:



Advertisements
Similar presentations
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
Advertisements

The University of Adelaide, School of Computer Science
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Procedures 2a MIPS code examples Making Use of a Stack.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
February 3, 2003Functions in MIPS1 CS 232 It is important that students bring a certain ragamuffin, barefoot irreverence to their studies; they are not.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
MIPS Functions and the Runtime Stack
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
Function Calls in MIPS To call a function: jal func
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Functions and the Stack
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
MIPS Procedures.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS functions.
MIPS Procedures.
Review.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Procedures and Calling Conventions
Computer Architecture Procedure Calls
Systems Architecture I
Program and memory layout
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
MIPS Functions.
MIPS function continued
Presentation transcript:

Function Calls in MIPS To call a function: jal func jal has a side-effect: it sets $ra to address of instruction after jal To return from a function: jr $ra Calling conventions: arguments “passed” in registers $a0, …, $a3 return values in registers $v0, $v1 callee can destroy: $t0-$t9 $a0-$a3 $v0-$v1 callee assumes caller will need: $s0-$s7 $ra For MP 2: You will be writing a function (called from our main program), which also calls one of our functions you must assume the worst!

Example C++ code fragment: if(func(7) == 7) # free to modify $a, $t and $v registers jr $ra main: li $a0, 7 # set argument for call to func jal func # call func(7) bne $a0, $v0, else # test if func(7) == 7 Since we want to preserve $a0 across the function call, we must save it before the call, and then restore it after the call. MIPS stack grows downwards, $sp points to “top” 0x7FFFFFFF 0x00000000 $sp stack

Pushing elements To push elements onto the stack: “Grow stack” by subtracting from $sp Store the elements into the stack (array) Example: Push $t1 and $t2 onto stack addi $sp, $sp, -8 sw $t1, 0($sp) sw $t2, 4($sp) Returning to our previous example: li $a0, 7 addi $sp, $sp, -4 sw $a0, 0($sp) jal func # restore $a0 bne $a0, $v0, else Before word 2 word 1 $sp word 2 word 1 $sp After $t2 $t1

Accessing and popping elements You can access any element in the stack (not just the top one) if you know where it is relative to $sp For example, to retrieve the value of $t2: lw $t2, 4($sp) You can pop, or “erase,” elements simply by adjusting the stack pointer upwards Example: addi $sp, $sp, 4 Note: The “popped” data is still present in memory, but data past the stack pointer is considered invalid word 2 word 1 $sp $t2 $t1 word 2 word 1 $sp $t2 $t1

Finishing the main example func: # free to modify $a, $t and $v registers jr $ra main: li $a0, 7 # set argument for call to func addi $sp, $sp, -4 # grow stack sw $a0, 0($sp) # save $a0 on stack jal func # call func(7) lw $a0, 0($sp) # restore $a0 from stack addi $sp, $sp, 4 # shrink stack bne $a0, $v0, else ... jr $ra Unfortunately, main won’t return correctly! Any time you do a jal, you must save and restore $ra

Translate into MIPS int plusOne(int x) { return x + 1; } void main() { x += plusOne(x); plusOne: # a0 = x addi $v0, $a0, 1 jr $ra main: li $a0, 5 addi $sp, $sp, -8 sw $ra, 0($sp) sw $a0, 4($sp) jal plusOne lw $ra, 0($sp) lw $a0, 4($sp) addi $sp, $sp, 8 add $a0, $a0, $v0

Recursive Functions Recall that recursive functions have one or more base-cases, and one or more recursive calls. Example: int rec_max(int *array, int n) { if(n == 1) return array[0]; return max(array[n-1], rec_max(array, n-1)); } Useful tip: Translate the base case first (rarely needs the stack) rec_max: # $a0 = array = &array[0], $a1 = n bne $a1, 1, rec_case lw $v0, 0($a0) # return-value = array[0] jr $ra rec_case: ...

The Recursive Case Let’s examine the recursive step more carefully: return max(array[n-1], rec_max(array, n-1)); Useful tip: Figure out what we need to remember across the recursive function call: array[n-1] ($a0 = array, $a1 = n) rec_case: addi $sp, $sp, -12 # save space for 3 regs addi $a1, $a1, -1 # compute n-1 sw $a0, 0($sp) # save &array[0] sw $a1, 4($sp) # save n-1 sw $ra, 8($sp) # save $ra, since I’m doing jal! jal rec_max # recursive call w/new args # restore $a0, $a1 and $ra # compare array[n-1] and $v0, and put larger into $v0