Procedure Conventions & The Stack Jen-Chang Liu, Spring 2006 Adapted from

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
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
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
 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.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 12 – Introduction to MIPS Procedures II & Logical Ops IBM has created.
CS61C L8 Introduction to MIPS : Decisions II & Procedures I (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c UCB CS61C.
1 CS61C L6 Machine Rep Clap if you like pizza! Pointless Poll.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
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.
1 CS 430 Computer Architecture Clap if you like pizza! Pointless Poll.
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,
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,
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)
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures II & Logical Ops “Cycle Computing”
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 L12 Introduction to MIPS : Procedures II & Logical Ops (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Procedures I Fall 2005 C functions main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */ int mult (int mcand,
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
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures I Internet2, a non-profit advanced.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering 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.
1 CS61C L6 Machine Rep CENG 311 Procedure Conventions & The Stack.
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.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures II & Logical Ops Prof Paul Debevec.
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
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
10/4: Lecture Topics Overflow and underflow Logical operations
CS61C : Machine Structures Lecture 2. 2
Program and memory layout
Program and memory layout
Procedures and addressing
Program and memory layout
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Procedure Conventions & The Stack Jen-Chang Liu, Spring 2006 Adapted from

Overview °C Functions °MIPS Instructions for Functions °Nested Functions - The Stack °Procedure Call Conventions

Review: Layout of Memory PC(program counter)

Review °Big Idea: Stored Program Concept Assembly language instructions encoded as numbers (machine language) Everything has an address, even instructions What about functions (procedures)?

C functions main() { int i,j,k,m; i = mult(j,k); m = mult(i,i); } int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier -1;} return product; } What information must compiler/programmer keep track of?

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.

General Purpose Registers in MIPS $0: zero $v0-$v1: return values $a0-$a3: arguments $k0-$k1: OS reserved $gp: global pointer $sp: stack pointer $ra = return address $t0-$t9: temporary registers (not preserved across call) $s0-$s7: saved temporary registers (preserved across call)

Instruction Support for Functions (1/4)... sum(a,b); /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address C MIPSMIPS 1004 add $a1,$s1,$0 # argument= b 1000 add $a0,$s0,$0 # argument=a 2000 sum: add $v0,$a0,$a1 # return =$v addi $ra,$0,1016 # $ra= j sum # jump to sum jr $ra # new instruction

Instruction Support for Functions (2/4) °Single instruction to jump and save return address: jump and link ( jal ) °Before: 1008 addi $ra,$0,1016 #$ra= j sum #go to sum °After: 1012 jal sum # $ra=1016,go to sum °Why have a jal ? Make the common case fast: functions are very common.

Instruction Support for Functions (3/4) °Syntax for jal (jump and link) is same as for j (jump): jallabel °jal should really be called laj for “link and jump”: Step 1 (link): Save address of next instruction into $ra Step 2 (jump): Jump to the given label

Instruction Support for Functions (4/4) °Syntax for jr (jump register): jrregister °Instead of providing a label to jump to, the jr instruction provides a register that contains an address to jump to. °Usually used in conjunction with jal, to jump back to the address that jal stored in $ra before function call.

Instruction Support for Functions... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } 2000 sum: add $v0,$a0,$a jr $ra# new instruction C MIPSMIPS °Question: Why use jr here? Why not simply use j? °Answer: sum might be called by many functions, so we can’t return to a fixed place. The calling proc to sum must be able to say “return here” somehow.

Overview °C Functions °MIPS Instructions for Functions °Nested Functions - The Stack °Procedure Call Conventions

Nested Procedures (1/2) int sumSquare(int x, int y) { return mult (x,x)+ y; } main: … la $ra, ra1 # $ra = ra1 j sumSquare ra1: … sumSquare: la $ra, ra2 # $ra = ra2 j mult ra2: … jr $ra # $ra = ra2 (???) mult: … jr $ra # $ra = ra2

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

C memory Allocation 0  Address Text 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 reserved

Using the Stack °So we have a register $sp which always points to the last used space in the stack. °To use stack we decrement this pointer by the amount of space we need then fill it with info Stack 堆疊 : Last In First Out (LIFO) a push a a b push b a pop

Compiling nested C func into MIPS int sumSquare(int x, int y) { return mult(x,x)+ y; } sumSquare: addi $sp,$sp,-12# space on stack sw $ra,$ 8($sp) # save ret addr sw $a0,$ 0($sp) # save x sw $a1,$ 4($sp) # save y C Pop Push Body addi $a1,$a0,$0 # mult(x, x) jal mult # call mult lw $ra,$ 8($sp) # get ret addr lw $a0,$ 0($sp) # restore x lw $a1,$ 4($sp) # restore y addi $sp,$sp,12 # => stack space add $v0, $v0, $a1 # mult(x,x)+y jr $ra

Stack Contents word size stack High memory Low memory … old $sp new $sp $ra $a1 $a0

Overview °C Functions °MIPS Instructions for Functions °Nested Functions - The Stack °Procedure Call Conventions

Steps for Making a Procedure Call 1) Save necessary values onto stack. 2) Assign argument(s), if any. 3) jal call 4) Restore values from stack.

Rules for Procedures °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)! So what are they?

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 (call other functions…) ra memory frame

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 (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.

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.

Parents leaving for weekend analogy (1/5) °Parents (main) leaving for weekend °They (caller) give keys to the house to kid (callee) with the rules (calling conventions): You can trash the temporary room(s), like the den and basement (registers) if you want, we don’t care about it BUT you’d better leave the rooms (registers) that we want to save for the guests untouched. “these rooms better look the same when we return!” °Who hasn’t heard this in their life?

Parents leaving for weekend analogy (2/5) °Kid now “owns” rooms (registers) °Kid wants to use the saved rooms for a wild, wild party (computation) °What does kid (callee) do? Kid takes what was in these rooms and puts them in the garage (memory) Kid throws the party, trashes everything (except garage, who goes there?) Kid restores the rooms the parents wanted saved after the party by replacing the items from the garage (memory) back into those saved rooms

Parents leaving for weekend analogy (3/5) °Same scenario, except before parents return and kid replaces saved rooms… °Kid (callee) has left valuable stuff (data) all over. Kid’s friend (another callee) wants the house for a party when the kid is away Kid knows that friend might trash the place destroying valuable stuff! Kid remembers rule parents taught and now becomes the “heavy” (caller), instructing friend (callee) on good rules (conventions) of house.

Parents leaving for weekend analogy (4/5) °If kid had data in temporary rooms (which were going to be trashed), there are three options: Move items directly to garage (memory) Move items to saved rooms whose contents have already been moved to the garage (memory) Optimize lifestyle (code) so that the amount you’ve got to shlep stuff back and forth from garage (memory) is minimized °Otherwise: “Dude, where’s my data?!”

Parents leaving for weekend analogy (5/5) °Friend now “owns” rooms (registers) °Friend wants to use the saved rooms for a wild, wild party (computation) °What does friend (callee) do? Friend takes what was in these rooms and puts them in the garage (memory) Friend throws the party, trashes everything (except garage) Friend restores the rooms the kid wanted saved after the party by replacing the items from the garage (memory) back into those saved rooms

What does r have to push on the stack before “jal e”? 1: 1 of ($s0,$sp,$v0,$t0,$a0,$ra) 2: 2 of ($s0,$sp,$v0,$t0,$a0,$ra) 3: 3 of ($s0,$sp,$v0,$t0,$a0,$ra) 4: 4 of ($s0,$sp,$v0,$t0,$a0,$ra) 5: 5 of ($s0,$sp,$v0,$t0,$a0,$ra) 6: 6 of ($s0,$sp,$v0,$t0,$a0,$ra) 7: 0 of ($s0,$sp,$v0,$t0,$a0,$ra) Peer Instruction 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

What does r have to push on the stack before “jal e”? 1: 1 of ($s0,$sp,$v0,$t0,$a0,$ra) 2: 2 of ($s0,$sp,$v0,$t0,$a0,$ra) 3: 3 of ($s0,$sp,$v0,$t0,$a0,$ra) 4: 4 of ($s0,$sp,$v0,$t0,$a0,$ra) 5: 5 of ($s0,$sp,$v0,$t0,$a0,$ra) 6: 6 of ($s0,$sp,$v0,$t0,$a0,$ra) 7: 0 of ($s0,$sp,$v0,$t0,$a0,$ra) Peer Instruction Answer 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

Do it yourself °Check out the examples in the textbook °Conclusion: Now you know that a procedure call takes a lot of steps!