Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
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
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.
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.
1 Today  Remember to use your late days wisely —We can’t post solutions until all HWs have been turned in  One complete example —To put together the.
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)
Intro to Computer Architecture
Run time vs. Compile time
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
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.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Procedures 2a MIPS code examples Making Use of a Stack.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
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.
Activation Records (in Tiger) CS 471 October 24, 2007.
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,
February 3, 2003Functions in MIPS1 CS 232 It is important that students bring a certain ragamuffin, barefoot irreverence to their studies; they are not.
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:
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Functions in Assembly CS 210 Tutorial 7 Functions in Assembly Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/7/
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Chapter 9 :: Subroutines and Control Abstraction
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
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)
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:

Functions and Procedures

Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used for code, away from main and other functions (main is itself a function called by the system) u May return a value to caller u Function may use parameters provided by caller

Function call u Store parameters where function can find them u Store address where control should return after call u Go to function code u Store information in registers that need to be used by function, so they can be restored u Get parameters u Execute function u Put result where it can be found by caller

Function call continued u Restore information in registers u Return control to caller at appropriate address u Caller get results

Function call -- issues u How to save return address? u How to provide parameters for function? u How to save information stored in registers? u How to store return value of function so it is available to caller after function terminates

Solution 1 u Use fixed location in memory for each function u Nested calls of different functions can work u Recursive calls, direct or mutually recursive, cannot work u No longer used

Solution 2 u Use a portion of memory as a stack –allows recursive functions u Need to keep a pointer to top of stack –a register is usually used u Information which can be stored on the stack –parameters (stored by caller) –return address (stored by caller) –register values needed after call (stored by caller or function) –local variables for function (stored by function) –return value (stored by function)

Runtime stack SP main PC code memory stack memory parameters SP return address SP PC function 1 function 2

PC Runtime stack main function 1 code memory stack memory local variables return value return address parameters PC SP function 2

Runtime stack -- second call main code memory stack memory local variables return value return address parameters local variables return value return address parameters function 1 function 2 PC SP

Runtime stack -- red calls yellow main function 1 code memory stack memory local variables return value return address parameters function 2 local variables return value return address parameters PC SP parameters SP return address PC SP return value local variables PC

After red - yellow call returns main function 1 PC code memory stack memory local variables return value return address parameters SP function 2 local variables return value return address parameters still there no longer used

After yellow- red call completes main function 1 PC code memory stack memory local variables return value return address parameters SP function 2 still there no longer used still there no longer used

Solution 3 u Store all values which need to be saved in registers u Needs many registers u Faster than using memory u Eventually cannot handle recursive calls which go too deep u Deep stack in registers used in Sun Sparc architecture

MIPS solution u Mixed: keep as much as possible in registers u “Spill” register contents to memory stack when needed u Single function calls often use only registers u Deeper function calls always use stack –how much depends on register usage by functions

MIPS Register Usage u ra (31) used for function return address u sp (29) used as stack pointer u v0, v1 function return value(s) u a0, a1, a2, a3 function parameters u s registers guaranteed to hold same values across function calls (e.g. value after call is the same as it was before the call) u t registers values may be chnaged by function call without being restored after

MIPS function call sequence 1. Caller places parameter values in registers $a0 - $a3 if more than four, place extra on stack 2. Caller saves values of a and t registers on stack, if needed later adjust stack pointer accordingly 3. Jump to function start address and place return address in register $ra jal instruction does this -- return address is PC Allocate memory needed for stack frame by subtracting frame size from $sp (stack pointer register). 5. Save callee-saved registers any s registers which the function will use the $ra registers if this function calls another Execute function

MIPS function call sequence Caller uses return value(s) which is (are) in register(s) $v0, $v1 10. Caller restores values of a and t registers from stack, if needed readjust stack pointer accordingly 9. Return to return address which is in $ra jr $ra instruction does this 8. Deallocate memory needed for stack frame by adding frame size to $sp (stack pointer register). 7. Restore callee-saved registers any s registers which the function used the $ra registers if this function called another 6. Place return value(s) in registers $v0-$v1