CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.

Slides:



Advertisements
Similar presentations
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I If cars broadcast their speeds to.
Advertisements

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.
The University of Adelaide, School of Computer Science
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
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.
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.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
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.
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.
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.
Lecture 6: Procedure Call (Part I)
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.
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 */
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.
CS 61C: Great Ideas in Computer Architecture Lecture 6: More MIPS, MIPS Functions Instructor: Sagar Karandikar
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
Csci 136 Computer Architecture II – MIPS Procedure Call Handling Xiuzhen Cheng
CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions 1 Instructors: John Wawrzynek & Vladimir Stojanovic
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.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
1. CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions 2 Instructors: Vladimir Stojanovic & Nicholas Weaver
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.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
April 2006 Saeid Nooshabadi
CSCI206 - Computer Organization & Programming
Krste Asanović and Randy H. Katz
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
CS61C : Machine Structures Lecture 2. 2
Program and memory layout
Lecture 6: Assembly Programs
Program and memory layout
Procedures and addressing
Program and memory layout
Program and memory layout
Presentation transcript:

CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson & Hennessy, ©2005 Some slides and/or pictures in the following are adapted from: slides ©2008 UCB 5

Registers play a major role in keeping track of information for function calls Register conventions: – Return address $ra – Arguments $a0, $a1, $a2, $a3 – Return value $v0, $v1 – Local variables $s0, $s1, …, $s7 The stack is also used; more later Function Call Bookkeeping

... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address 1000 add $a0,$s0,$zero # x = a 1004 add $a1,$s1,$zero # y = b 1008 addi $ra,$zero,1016 # $ra= j sum # jump to sum sum: add $v0,$a0,$a jr $ra# new instruction Function Call Bookkeeping

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

Function Call Bookkeeping Single instruction to jump and save return address: jump and link ( jal ) Before: 1008 addi $ra,$zero,1016 #$ra= j sum #goto sum After: 1008 jal sum # $ra=1012,goto sum Why have a jal ? Make the common case fast: function calls are very common. Also, you don’t have to know where the code is loaded into memory with jal

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 (Why next instruction? Why not current one?) –Step 2 (jump): Jump to the given label Function Call Bookkeeping

Syntax for jr (jump register): jr register Instead of providing a label to jump to, the jr instruction provides a register which contains an address to jump to Very useful for function calls: – jal stores return address in register ($ra) – jr $ra jumps back to that address Function Call Bookkeeping

int sumSquare(int x, int y) { return mult(x,x)+ y; } Something called sumSquare, now sumSquare is calling mult So there’s a value in $ra that sumSquare wants to jump back to, but this will be overwritten by the call to mult Need to save sumSquare return address before call to mult Nested Procedures

In general, may need to save some other info in addition to $ra When a 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 Nested Procedures

0  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 Memory Allocation

The register scratchpad for a procedure seems volatile – it seems to disappear every time we switch procedures – a procedure’s values are therefore backed up in memory on a stack Proc A’s values Proc B’s values Proc C’s values … High address Low address Stack grows this way Proc A call Proc B … call Proc C … return 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 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

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 # restore stack jr $ra mult:... int sumSquare(int x, int y) { return mult(x,x)+ y; } “push” “pop” Using the Stack

Steps for Making a Procedure Call Save necessary values onto stack Assign argument(s), if any jal call 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. So what are they?

Register Conventions CalleR: the calling function CalleE: the function being called When callee returns from executing, the caller needs to know which registers are guaranteed to be unchanged (saved registers) and which registers may have changed (volatile registers) 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 - 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 - 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 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

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 Basic Structure of a Function

int leaf_example (int g, int h, int i, int j) { int f ; f = (g + h) – (i + j); return f; } Example 1 Compile into MIPS (done in class)