1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.

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
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 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
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See P&H 2.8 and 2.12.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
MIPS Programming Arrays Writing Procedures: Calling Convention.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12.
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.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
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.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
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
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Program and memory layout
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

Procedures int len(char *s) { int l; for (l=0; *s != ‘\0’; s++) l++; return l; } void reverse(char *s, char *r) { char *p, *t; int l = len(s); *(r+l) = ‘\0’; l--; for (p=s+l t=r; l>=0; l--) { *t++ = *p--; } } void main(int) { char *s = “Hello World!”; char r[100]; reverse(s,r); } How can we do this with assembly? * Need a way to call / return procedures * Need a way to pass arguments * Need a way to return a value Main calls reverse, which calls len

Procedure Call and Return Procedure call Need to jump to the procedure The return goes back to the point immediately after the call Need to pass “return address” (instruction after call) jal Label $ra = PC+4 # set return address to next PC PC = PC[31:28], address, 2’b0 # jump to procedure Procedure return Needs to know the return address Need to jump back to the return point (after the call) jr $ra PC = $ra # jump back to return address

Arguments and Return Value Register conventions specified in PRM $a0-$a3: four arguments to pass to called procedure $v0-$v1: two values returned from procedure $ra: return address register (set by call, used by return) Call chains One procedure calls another, which calls another one E.g., main  reverse  len What happens to $ra??? (e.g., when reverse calls len) You must save $ra someplace! Simple approach: A “free” register (can’t be used by caller) Leaf procedure: Doesn’t make any calls. Doesn’t ever save $ra.

# procedure reverse($a0,$a1) reverse: move $t7,$ra # save return address jal len # get length of source string blt $v0,$0,rev_exit # exit if empty string add $t0,$a1,$v0 # null terminate target string sb $0,0($t0) # put null into end of string addi $v0,$v0,-1 # decrement length (written /0) add $t0,$a0,$v0 # $t0 holds p (source string) add $t1,$a1,$0 # $t1 holds t (target string) rev_loop: lbu $t2,0($t0) # get char from source string sb $t2,0($t1) # save char to target string addi $t0,$t0,-1 # decrement source string ptr addi $t1,$t1,1 # increment target string ptr addi $v0,$v0,-1 # decrement length slt $t2,$v0,$0 # is l < 0? beq $t2,$0,rev_loop rev_exit: move $ra,$t7 jr $ra

# procedure len($a0); returns string length in $v0 len: move $t0,$a0 # copy start ptr len_loop: lbu $t1,0($t0) # get char beq $t1,$0,len_exit # check for null addi $t0,$t0,1 # go to next character j len_loop # continue loop len_exit: sub $v0,$t0,$a0 # diff of ptrs is length jr $ra Full program is in reverse.asm

More on Procedure Call/Return Caller: The procedure that calls another one Callee: The procedure that is called by the caller What if callee wants to use registers? Caller is also using registers! If callee wants to use same registers, we must save them Consider what happened with $ra in a call chain Register usage conventions specified by PRM $t0-$t9: Temp. registers; if caller wants them, must save before call $s0-$s7: Saved registers; saved by callee prior to using them

9 Register Usage Convention NAMEREG. NUMBERUSAGE $zero0zero $at1reserved for assembler usage $v0~$v12~3values for results and expression eval. $a0~$a34~7arguments $t0~$t78~15temporaries $s0~$s716~23temporaries, saved $t8~$t924~25more temporaries $k0~$k126~27reserved for OS kernel $gp28global pointer $sp29stack pointer $fp30frame pointer $ra31return address

Where to Save? Need a memory space to hold saved registers Caller spills $t0-$t9 that be must saved to memory Callee spills $s0-$s7 to memory, when these regs are used Non-leaf caller spills $ra when making another call Each procedure needs locations to save registers In general, call-chain depth (number of called procs) is unknown, so we need to support undetermined length Suggestion: Use a stack. Add “stack element” onto stack for each call. The “stack element” has the locations.

Program Stack Program stack: Memory locations used by running program Has space for temporary and saved registers Has space for local variables, when can’t all fit in registers E.g., local arrays are allocated on the stack Has space for return address Each procedure allocates space for these items So-called “activation frame” (a.k.a., “activation record”) Purpose of locations in activation frame are known Prologue (entry point into the procedure): Allocates an activation frame on the stack Epilogue (exit point from procedure): De-allocates the activation frame, does actual return

13 Stack and Frame Pointers Stack pointer ($sp) – Keeps the address to the top of the stack – $29 is reserved for this purpose – Stack grows from high address to low – Typical stack operations are push/pop Procedure frame – Contains saved registers and local variables – “Activation record” Frame pointer ($fp) – Points to the first word of a frame – Offers a stable reference pointer – $30 is reserved for this – Some compilers don’t use $fp

Stack and Frame Pointer Caller saves needed registers, sets up args, makes call When not enough arg regs: Put arguments onto the stack Called procedure prologue Adjust stack pointer for activation frame size to hold enough space to hold saved registers, locals, return address (non-leaf) Save any saved registers to the stack Save return address to the stack Called procedure epilogue Restore return address from the stack (non-leaf) Restore any saved registers from the stack Return to caller

Example: Factorial / * factorial */ int fac(int f) { if (f == 1) // end of recursion return 1; else // go to bottom return (fac(f-1) * f); } int main(void) { a = fac(i); print(a); } See factorial.asm