Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.

Slides:



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

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.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Procedure Calls Prof. Sirer CS 316 Cornell University.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
Computer Architecture CSCE 350
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 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.
Procedure call frame: Hold values passed to a procedure as arguments
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
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:
Intro to Computer Architecture
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
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.
Runtime Environments Compiler Construction Chapter 7.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
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:
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/
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
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.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Lecture 7 Macro Review Stack Frames
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.
CS 140 Lecture Notes: Virtual Memory
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Help! How do procedure calls work?
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
In this lecture Global variables Local variables System stack
CS 140 Lecture Notes: Virtual Memory
Stack Frame Linkage.
MIPS Instructions.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
CS 140 Lecture Notes: Virtual Memory
MIPS function continued
Program and memory layout
Procedures and Calling Conventions
Systems Architecture I
Program and memory layout
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
CS 140 Lecture Notes: Virtual Memory
© Craig Zilles (adapted from slides by Howard Huang)
Implementing Functions: Overview
Presentation transcript:

Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text

Review Continued review of Power example and using the stack

Stack Pointer Notice that the stack is important for use in nested function calls. Since recursive functions include many nested function calls, the stack is essential for recursion. We need the stack so we can keep track of the values within argument registers, and saved registers each time we make a function call. Let's see what happens when we call power(4, 3). Assume that this function takes 4 to the third power and uses recursion rather than a loop to compute the result. The result should be 64. The first call to this function is power(4,3) and we will assume that this call comes from the main function. We need to store the return address and the power (the second argument) on the stack. We don't necessarily need to store the value four anywhere other than an argument register because it will stay the same on every call we make. If we look at the code on the examples page, we see that on our first call, we store the return address in the current function's frame, and we store the argument register in the calling function's frame.

Stack Pointer <---- $sp (frame for first call) | | empty | | empty <---- $sp | | empty <---- $sp | | empty <---- $sp | | empty <---- $sp | ret. addr. for main | ra <---- $sp <---- main function frame | $a1 = 3 | empty <---- $sp | | empty | | empty | | empty | | empty | | empty <---- End of memory Notice that the return address is stored in the frame for power(4,3) and the argument value for the power (3) is stored in the calling function's frame. That is we store 3 in the frame for the main function.

Stack Pointer <---- $sp (frame for second call) | | empty | | empty <---- $sp | | empty <---- $sp | | empty <---- $sp | | empty <---- $sp | ret. To pow(4,3) | ra <---- $sp <---- (frame for first call) | $a1 = 2 | empty <---- $sp | | empty <---- $sp | | empty <---- $sp | | empty <---- $sp | | empty <---- $sp | ret. addr. for main | ra <---- $sp <---- main function frame | $a1 = 3 | empty <---- $sp | | empty | | empty <---- End of memory On the second call power(4,2), notice that the stack grows and the return address to the call from power(4,3) is stored in the current stack frame, and the current value of the argument register is stored in the previous frame.

Stack Pointer On the third call power(4,1), notice that the stack grows and the return address to the call from power(4,2) is stored in the current stack frame, and the current value of the argument register is stored in the previous frame.

Stack Pointer Once we reach the last call notice that all the data needed to return from each function is provided. In the last call we simply return the value of the first argument register ($a0). That is, we return four. This value is stored in the return register ($v0). Additionally we pop the stack. Now $v0 is 4 and the stack looks as follows:

Stack Pointer As we return from the second call of power(4,2), we multiply $v0 by $a0 or four and store the result in $v0. So now $v0 is 16, and we can pop the stack again. Remember that we have to update the $ra register to return to the proper location.

Stack Pointer Finally, we will return from the call power(4,3). Again we must multiply $v0 by $a0. Recall that $v0 is 16 and $a0 is 4. So the result stored in $v0 is 64. We also need to load the return address for the main function from the stack into $ra so we can return to main <---- main function frame | $a1 = 3 | empty <---- $sp | | empty | | empty | | empty | | empty | | empty <---- End of memory

Stack Pointer Once we return to the main method, the result of power(4,3) will be stored in $v0, and after returning, we may pop the stack so that all the memory utilized will be available for future use. In the event we need more space for arguments, we can simply allocate more space for each activation record and utilize memory to pass arguments to functions