28/06/2015CMPUT 229 1 Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

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.
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
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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)
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
MIPS Assembly Language
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
CS 536 Spring Code generation I Lecture 20.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
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
Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.
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.
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.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
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,
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:
Lecture 19: Control Abstraction (Section )
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
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.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
Storage Allocation Mechanisms
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
MIPS Assembly Language Programming
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
143A: Principles of Operating Systems Lecture 4: Calling conventions
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
Activation Records and Function Calls
Stack Frame Linkage.
MIPS Instructions.
MIPS Procedure Calls CSE 378 – Section 3.
The University of Adelaide, School of Computer Science
Topic 3-a Calling Convention 1/10/2019.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Systems Architecture I
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
MIPS function continued
Topic 2b ISA Support for High-Level Languages
Runtime Stack Activation record for hanoi;
Presentation transcript:

28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions

28/06/2015CMPUT Recursive functions  A function that calls itself  Terminating condition  Stack frame implementation

28/06/2015CMPUT Function calling convention  In summary, caller: 1.saves temporary registers by pushing their values on stack 2.pushes arguments (both “value” and “variable” on stack 3.calls the function with jal instruction  (function runs until it returns, then:) 4.clears function arguments by popping allocated space 5.restores saved temporary registers by popping their values off the stack 6.uses the return value found in both $v0 and popped “variable” arguments

28/06/2015CMPUT Function calling convention  In summary, callee: 1.saves $ra by pushing its value on stack 2.saves $fp by pushing its value on stack 3.copies $sp to $fp 4.allocates local variables  (body of function goes here, then:) 5.chooses return value by setting register $v0 6.deallocates local variables by popping allocated space 7.restores $fp by popping its saved value 8.restores $ra by popping its saved value 9.returns with jr $ra

28/06/2015CMPUT Recursion  Function calling convention works exactly the same for recursive functions  don’t need to do anything special  Each invocation of the function has its own stack frame  local variables and parameters with their current valueswith their current values  return address where to return towhere to return to

28/06/2015CMPUT Example  Fibonacci’s numbers fib(0) = 0 fib(1) = 1 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) for n > 1 fib(n) = fib(n-1) + fib(n-2) for n > 1

28/06/2015CMPUT Fibonacci Numbers

28/06/2015CMPUT Implementation of Recursive Functions  Different conventions can be used for writing recursive functions  MIPS  $a0 for taking input parameters  $v0 for returning results  Callers save $t0, $t1, …,  Callees save $s0, $s1, …,

28/06/2015CMPUT Implementation of Recursive Functions  Other conventions  Callees save all registers

28/06/2015CMPUT  More restricted conventions  All arguments are passed with the stack frame, including the value and variable parameters  Useful and important

28/06/2015CMPUT  More restricted conventions  All arguments are passed with the stack frame, including the value and variable parameters  Frame pointers  Used by compilers but difficult to understand

28/06/2015CMPUT  More restricted conventions  All arguments are passed with the stack frame, including the value and variable parameters  Frame pointers  Used by compilers but difficult to understand

28/06/2015CMPUT  Two different implementations of fibonacci numbers  Simple one (fib.a) Using $a0 and $v0 to pass argumentsUsing $a0 and $v0 to pass arguments Callees save all the registersCallees save all the registers  Stack frame (fib_stack.a) Using the stack frame for passing the arguments to and from the functionUsing the stack frame for passing the arguments to and from the function