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.

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
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
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.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
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.
CS61C L8 Introduction to MIPS : Decisions II & Procedures I (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c UCB CS61C.
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L11 Introduction to MIPS: Procedures I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
CS61C L8 MIPS Procedures (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #8 – MIPS.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition,
Intro to Computer Architecture
Lecture 6: Procedure Call (Part I)
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
CS61C L11 Introduction to MIPS : Procedures I (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Machine Structures Lecture 4 – Introduction to MIPS Procedures 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);...
CS61C L8 Decisions(2), Procedures(1) (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture.
CS61C L11 Introduction to MIPS : Procedures I (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
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures I Internet2, a non-profit advanced.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
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.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
Csci 136 Computer Architecture II – MIPS Procedure Call Handling Xiuzhen Cheng
CS61C L8 MIPS Procedures (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
IT 251 Computer Organization and Architecture Introduction to MIPS Procedures Chia-Chi Teng.
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I Apple took a bold step recently.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Decisions (cont), Procedures. Review Memory transfer: lw, sw, lb, sb A Decision allows us to decide what to execute at run-time rather than compile-time.
MIPS Procedures II, Logical & Shift Ops. Review Functions called with jal, return with jr $ra. The stack is your friend: Use it to save anything you need.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Function Calls in MIPS To call a function: jal func
Procedures (Functions)
Procedures (Functions)
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
Computer Architecture Procedure Calls
Systems Architecture I
Program and memory layout
Procedures and addressing
Computer Architecture
Program and memory layout
Program and memory layout
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

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 Return value is always in $v0 (and if necessary in $v1 ) Must follow register conventions (even in functions that only you will call)!

Nested Procedures (1/2) int sumSquare(int x, int y) { return mult (x,x)+ y; } A procedure called sumSquare, now sumSquare is calling mult. Problem: there’s a return address in $ra that sumSquare wants to jump but it will be overwritten by the call to mult. Need to save sumSquare return address before call to mult.

Nested Procedures (2/2) In general, may need to save some other info in addition to $ra. When a C program is run, there are 3 important memory areas allocated: Static: Variables declared once per program, cease to exist only after execution completes. e.g., C globals Heap: Variables declared dynamically Stack: Space to be used by procedure during execution; this is where we can save register values

C Memory Allocation 0  fff fff Address Code Program Static Variables declared once per program Heap Explicitly created space, e.g., malloc(); C pointers Stack Space for saved procedure information $sp stack pointer

Using the Stack (1/2) register $sp always points to the last used space in the stack. To use stack, we decrement this pointer by the amount of space we need and then fill it with info. So, how do we compile this? int sumSquare(int x, int y) { return mult (x,x)+ y; }

Using the Stack (2/2) Hand-compile sumSquare: addi $sp,$sp,-8 # space on stack sw $ra, 4($sp) # save ret addr sw $a1, 0($sp) # save y add $a1,$a0,$zero # mult(x,x) jal mult # call mult lw $a1, 0($sp) # restore y add $v0,$v0,$a1 # mult()+y lw $ra, 4($sp) # get ret addr addi $sp,$sp,8 # update sp jr $ra mult:... int sumSquare(int x, int y) { return mult(x,x)+ y; } “push” “pop”

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 registers 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 registers may be changed.

Register Conventions (2/4) - saved $0 : No Change. Always 0. $s0 - $s7 : Save and 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 : Save and 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.

Register Conventions (4/4) What do these conventions mean? If function R calls function E, then function R/caller must save any t (temporary) registers that it may be using onto the stack before making a jal call. Function E/callee 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.

Beta: Save Saved Registers Restore Saved Registers Return Results … Send Parameters Receive Results Call Procedure … Save Unsaved Registers $t’s, $v’s, $a’s, $ra Restore Unsaved Regs. $t’s, $v’s, $a’s, $ra Return … Retrieve Parameters Alpha : Save Saved Registers Restore Saved Registers Return Results … Send Parameters Receive Results Call Procedure … Retrieve Parameters Save Unsaved Registers $t’s, $v’s, $a’s, $ra Restore Unsaved Regs. $t’s, $v’s, $a’s, $ra Return Structure of a Procedure

MIPS Registers The constant 0$0$zero Reserved for Assembler$1$at Return Values$2-$3$v0-$v1 Arguments$4-$7$a0-$a3 Temporary$8-$15$t0-$t7 Saved$16-$23$s0-$s7 More Temporary$24-$25$t8-$t9 Used by Kernel$26-27$k0-$k1 Global Pointer$28$gp Stack Pointer$29$sp Frame Pointer$30$fp Return Address$31$ra Use names for registers -- code is clearer!

Other Registers $at : may be used by the assembler at any time; unsafe to use $k0-$k1 : may be used by the OS at any time; unsafe to use $gp, $fp : don’t worry about them Note: Feel free to read up on $gp and $fp in Appendix A, but you can write perfectly good MIPS code without them.

Quickie Quiz (True or False?) When translating this to MIPS… A. We COULD copy $a0 to $a1 (not store $a0 or $a1 on the stack) to preserve n across recursive calls. B. We MUST save $a0 on the stack since it gets changed. C. We MUST save $ra on the stack since we need to know where to return to… int fact(int n){ if(n == 0) return 1; else return(n*fact(n-1));}  Tail Recursion FFTFFT

What does r have to push on the stack before “jal e”? 1: Nothing 2: 1 of ($s0,$sp,$v0,$t0,$a0,$ra) 3: 2 of ($s0,$sp,$v0,$t0,$a0,$ra) 4: 3 of ($s0,$sp,$v0,$t0,$a0,$ra) 5: 4 of ($s0,$sp,$v0,$t0,$a0,$ra) 6: 5 of ($s0,$sp,$v0,$t0,$a0,$ra) r:... # R/W $s0,$v0,$t0,$a0,$sp,$ra,mem... ### PUSH REGISTER(S) TO STACK? jal e # Call e... # R/W $s0,$v0,$t0,$a0,$sp,$ra,mem jr $ra # Return to caller of r e:... # R/W $s0,$v0,$t0,$a0,$sp,$ra,mem jr $ra # Return to r Volatile! -- need to pushSaved e can’t return changed, no need to push e can return changed Quickie Quiz

# RECURSIVE PROCEDURE TO COMPUTE FACTORIALS FACT: #save the registers that FACT uses sub $sp,$sp,8 #modify stack top sw $s0,0($sp) #save registers sw $ra,4($sp) move $s0,$a0 #get parameter out of a0 #For n=1, return 1 bne $s0,1,MORE li $v0,1 #return a 1 j RET

#For n>1, return n*FACT(n-1) MORE: ## move arguments in for call sub $a0,$s0,1 #compute n-1 ## call FACT on n-1 jal FACT #recursive call ## use returned value and set up return value for this call mul $v0,$v0,$s0 RET: lw $s0,0($sp) #restore registers lw $ra,4($sp) add $sp,$sp,8 #reposition stack top jr $ra #return

# Function fibo ## int fibo(int n) { ## if (n <= 2) ## return 1; ## else return(fib(n-2) + fib(n-1)); ## } ## fibo is a recursive procedure to find a given Fibbonacci number fibo: sub $sp, $sp, 12 # push stack frame sw $ra, 0($sp) # save return address sw $s0, 4($sp) # and s registers used here sw $s1, 8($sp) bgt $a0, 2, more # check for input greater than 1 li $v0, 1# if not, just return a 1 j fret

# input OK, set up recursive calls more: move $s0, $a0 # copy a0 to s0 so it's saved across calls sub $a0, $s0, 2 # set param to n-2 jal fibo # and make recursive call move $s1, $v0 # save fib(n-2) sub $a0, $s0, 1 # param = n-1 jal fibo # compute fib(n-1) add $v0, $v0, $s1 # add fib(n-2) # return value from this call is already in $v0 as needed fret: lw $ra, 0($sp) # restore return address, lw $s0, 4($sp) # s registers, lw $s1, 8($sp) add $sp, $sp, 12 # pop stack frame, jr $ra # and return....

“And in Conclusion…” Use the stack to save anything you need. Just be sure to leave it the way you found it. If your function is going to change an S register, you must: Store the register’s current value on the stack. Use the register as you’d like. Restore the register’s original value (with respect to your function) before the function returns. This ensures that values in saved registers are the same before and after a function call, regardless of recursive depth!