Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
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
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
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
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Assembly Language
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 12 – Introduction to MIPS Procedures II & Logical Ops IBM has created.
Cs 61C L8 Proc II., Arrays and Pointers in C 1 Patterson Fall 00 ©UCB CS61C - Machine Structures Lecture 8 - Procedure Conventions part II, plus, Arrays.
Lecturer PSOE Dan Garcia
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 12 – Introduction to MIPS Procedures II & Logical Ops As of mid-2007,
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
Lecture 6: Procedure Call (Part I)
Procedures I Fall 2010 Lecture 6: Procedures. Procedures I Fall 2010 Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);...
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
CS61C L12 Introduction to MIPS : Procedures II & Logical Ops (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L08 MIPS Procedures (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #8: MIPS Procedures
Procedure Conventions & The Stack Jen-Chang Liu, Spring 2006 Adapted from
CHAPTER 2 ISA Instructions (logical + procedure call)
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
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,
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Csci 136 Computer Architecture II – MIPS Procedure Call Handling Xiuzhen Cheng
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Function Calls in MIPS To call a function: jal func
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
MIPS Procedures.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
10/4: Lecture Topics Overflow and underflow Logical operations
CS61C : Machine Structures Lecture 2. 2
Program and memory layout
Systems Architecture I
Program and memory layout
Procedures and addressing
Computer Architecture
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)
MIPS function continued
Presentation transcript:

Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged. °Register Conventions: A set of generally accepted rules as to which registers will be unchanged after a procedure call ( jal ) and which may be changed.

Register Conventions (4/4) °What do these conventions mean? If function R calls function E, then function R must save any temporary registers that it may be using onto the stack before making a jal call. Function E must save any S (saved) registers it intends to use before garbling up their values Remember: Caller/callee need to save only temporary/saved registers they are using, not all registers.

Register Conventions (2/4) - saved °$0 : No Change. Always 0. °$s0 - $s7 : Restore if you change. Very important, that’s why they’re called saved registers. If the callee changes these in any way, it must restore the original values before returning. °$sp : Restore if you change. The stack pointer must point to the same place before and after the jal call, or else the caller won’t be able to restore values from the stack. °HINT -- All saved registers start with s!

Register Conventions (3/4) - volatile °$ra : Can Change. The jal call itself will change this register. Caller needs to save on stack if nested call. °$v0 - $v1 : Can Change. These will contain the new returned values. °$a0 - $a3 : Can change. These are volatile argument registers. Caller needs to save if they’ll need them after the call. °$t0 - $t9 : Can change. That’s why they’re called temporary: any procedure may change them at any time. Caller needs to save if they’ll need them afterwards.

Steps of calling a function 1.Place parameters in a place where the procedure can access them. 2.Transfer control to the procedure. 3.Acquire the storage resources needed for the procedure. 4.Perform the desired task. 5.Place the result value in a place where the calling program can access it. 6.Return control to the point of origin.

Basic Structure of a Function entry_label: addi $sp,$sp, -framesize sw $ra, framesize-4($sp) # save $ra save other regs if need be... restore other regs if need be lw $ra, framesize-4($sp) # restore $ra addi $sp,$sp, framesize jr $ra Epilogue Prologue Body (may call other functions…) ra memory

Example: Fibonacci Numbers 1/7 °Generate Fibonacci number in C: int fib(int n) { if(n == 0) { return 1; } if(n == 1) { return 1; } return (fib(n - 1) + fib(n - 2)); }

___________________ fib: ___________________ # Space for three words # Save the return address # Save $s0 ? Used later addi $sp, $sp, -12 sw $ra, 8($sp) sw $s0, 4($sp) °Now, let’s translate this to MIPS! °You will need space for ? words on the stack °The function will use $s0 (will be used later) °Write the Prologue: Example: Fibonacci Numbers 2/7

fin: ___________________ jr $ra_____________ lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 # Restore $s0 # Restore return address # Pop the stack frame # Return to caller ___________________ °Now write the Epilogue: Example: Fibonacci Numbers 3/7

beq $a0 $zero beq $a0 $t0 addi $v0, $zero, 1_ _______, _____,_fin addi $t0, $zero, 1_ _______,______,_fin Continued on next slide... # $v0 = 1, return value # # $t0 = 1 # °Finally, write the body. The C code is below. Start by translating the lines indicated in the comments int fib(int n) { if(n == 0) { return 1; } /*Translate Me!*/ if(n == 1) { return 1; } /*Translate Me!*/ return (fib(n - 1) + fib(n - 2)); } ___________________ __if (n == 0)... ______ ____________ __if (n == 1)... Example: Fibonacci Numbers 4/7

addi $a0, $a0, -1 # $a0 = n – 1 addi $a0, $a0, -1__ °Almost there, but be careful, this part is tricky! int fib(int n) {... return (fib(n - 1) + fib(n - 2)); } Example: Fibonacci Numbers 5/7 sw $a0 0($sp) # Need $a0 after jal jal fib # fib(n – 1) lw $a0 0($sp) # Restore $a0______ # $a0 = n – 2 next slide … jal fib #???

add $s0, $v0, $zero °Remember that $v0 is caller saved! int fib(int n) {... return (fib(n - 1) + fib(n - 2)); } Example: Fibonacci Numbers 6/7 jal fib # Place fib(n – 1) # somewhere it won’t get # clobbered # fib(n-2) add $v0, $v0, $s0 # fib(n-1)+fib(n-2)

°Here’s the complete code for reference: Example: Fibonacci Numbers 7/7 fib: addi $sp, $sp, -12 sw $ra, 8($sp) sw $s0, 4($sp) addi $v0, $zero, 1 beq $a0, $zero, fin addi $t0, $zero, 1 beq $a0, $t0, fin addi $a0, $a0, -1 sw $a0, 0($sp) jal fib lw $a0, 0($sp) addi $a0, $a0, -1 add $s0, $v0, $zero jal fib add $v0, $v0, $s0 fin: lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra