The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.

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
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
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
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
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
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)
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
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
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/
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
OCC - CS/CIS CS116-Ch00-Orientation Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 1CS 116 Fall 2003 Not quite finished Creating.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Simple Procedure Calls jal: performs the control transfer (unconditional jump) to the starting address of procedure, while also saving the return.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Lecture 4: MIPS Instruction Set
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.
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.
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.
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 Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
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 structure: Procedure Calls
MIPS Assembly Language Programming
CSCI206 - Computer Organization & Programming
Lecture 4: MIPS Instruction Set
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
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Procedures and Calling Conventions
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
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer Organization and Design, 4 th Edition, by Patterson and Hennessey, and were used with permission from Morgan Kaufmann Publishers.

Material to be covered... Chapter 2: Sections 8 – 9

ECE Computer Organization3 Some Basic Definitions Stack Pointer  A value denoting the most recently allocated address in a stack that shows where registers should be spilled or where old register values can be found. Frame Pointer  A value denoting the location of the saved registers and local variables for a given procedure. Procedure Frame (aka. Activation Record)  The segment of the stack containing a procedure's saved registers and local variables. Caller:A program or function that calls a procedure Callee:The procedure that is called

ECE Computer Organization4 Frame Pointer The frame pointer points to the highest address in the procedure frame (or activation record). The frame pointer remains fixed at this memory location for the duration of the procedure.  Whereas, the stack pointer moves each time an element is added to or removed from the stack. The frame pointer must be preserved across procedure calls.

ECE Computer Organization5 Stack Allocation and the Frame Pointer Local data – stack space allocated by the called procedure stack allocation when a procedure is called before procedure call after procedure call $fp points to the highest address of the procedure frame $sp points to the “top” of the stack

ECE Computer Organization6 Example: Calling CalculateF

ECE Computer Organization7 CalculateF: f = g*(a+b+c+d+e*2) main {.. f = CalculateF( a, b, c, d, e, g );.. print( f ); } CalculateF( pa, pb, pc, pd, pe, pg ) { x = Sum2( a, b, c, d, e ); pf = Prod1( x, g ); return( pf ); } Sum2( pa, pb, pc, pd, pe ) { px = pa + pb + pc + pd + pe*2; return( px ); } Prod1( px, pg ) { y = pg * px; return( y ); }

ECE Computer Organization8 CalculateF: f = g*(a+b+c+d+e*2) The main program calls CalculateF to carry out the arithmetic operation. CalculateF calls Sum2 to carry out the addition. CalculateF then calls Prod1 to carry out the multiplication. Main CalculateF f = g*(a+b+c+d+e*2) Sum2 f = (a+b+c+d+e*2) Prod1 f = g*(sum) calls Before calling Sum2: 1. Save return address (of main) on the stack. 2. Save argument e on the stack. Before returning to Main: 1. Restore return address (of main) from the stack. 2. Restore stack (i.e. stack pointer).

ECE Computer Organization9 Main calls CalculateF Data local to Main function is pushed on the stack. Parameters to be passed to the called function are also pushed onto the stack. Stack Pointer points to the last element pushed onto the stack. low address high address Stack data $sp e g main Saved arguments Local Data $fp jal CalculateF procedure call in Main

ECE Computer Organization10 Main calls CalculateF Main program pushes arguments (e and g) onto the stack. Main function executes jal CalculateF  $ra = return address of Main  PC = CalculateF low address high address Stack data $sp e g main Saved arguments from calling program $fp

ECE Computer Organization11 Stack Usage by CalculateF low address high address Stack data e g $fp (from Main) addi $sp, $sp -4 sw $fp, 0($sp) add $fp, $sp, $zero Main $fp, $sp 1.Frame pointer from Main pushed onto stack. 2.Frame pointer for CalculateF set equal to stack pointer ($fp = $sp). Stack pointer and Frame pointer now point to the highest address in the procedure frame for CalculateF. All arguments are now relative to the frame pointer. CalculateF

ECE Computer Organization12 Stack Usage byCalculateF low address high address Stack data $sp e g $fp (from Main) Return address is pushed onto the stack. Stack pointer adjusted. It is now possible to call another function. $ra (from Main) $fp addi $sp, $sp, -4 sw $ra, 0($sp) Main CalculateF

ECE Computer Organization13 Stack Usage by CalculateF low address high address Stack data $sp e g lw $t0, 8($fp) sll $t0, $t0, 1 addi $sp, $sp, -4 sw $t0, 0($sp) Read argument e from stack. Multiply e by 2. Push value onto stack. Adjust stack pointer. $fp (from Main) $ra (from Main) e * 2 $fp Main CalculateF

ECE Computer Organization14 CalculateF calls Sum2

ECE Computer Organization15 CalculateF: f = g*(a+b+c+d+e*2) lw $t0, 0($sp) add $t0, $t0, $a0 add $t0, $t0, $a1 add $t0, $t0, $a2 add $t0, $t0, $a3 add $v0, $t0, $zero jr $ra Sum2: # f=(e*2) # f=(e*2)+a # f=(e*2)+a+b # f=(e*2)+a+b+c # f=(e*2)+a+b+c+d # return value # return control jal Sum2 procedure call in CalculateF return value from Sum2 CalculateF calls Sum2 Arguments passed to Sum2 a, b, c, d → $a0 - $a3 e*2 → last element on stack Return address ($ra) for Main pushed onto stack before call Value returned to CalculateF in register $v0

ECE Computer Organization16 CalculateF calls Prod1

ECE Computer Organization17 CalculateF: f = g*(a+b+c+d+e*2) add $a0, $v0, $zero lw $a1, 4($fp) jal Prod1 # sum from Sum2 # read g from stack # call Prod1 procedure call to Prod1 passing the sum to Prod1 passing g to Prod1 CalculateF calls Prod1 Arguments passed to Prod1 sum from Sum2 → $a0 g → $a1 Return address ($ra) for Main pushed onto stack before call Value returned to CalculateF in register $v0 CalculateF:...

ECE Computer Organization18 CalculateF returns to Main

ECE Computer Organization19 Restore $sp, $fp, $ra and Return low address high address Stack data $sp e g main lw $ra, -4($fp) add $sp, $fp, $zero lw $fp, 0($sp) addi $sp, $sp, 4 jr $ra Read $ra from stack. Restore $sp. Read $fp from stack. Return to main. $fp (from Main) $ra (from Main) e * 2 $fp CalculateF

ECE Computer Organization20 Memory Layout

ECE Computer Organization21 Memory Layout Text: program code Static data: global variables  e.g., static variables in C, constant arrays and strings  $gp initialized to address allowing ±offsets into this segment Dynamic data: heap  e.g., malloc in C, new in Java Stack: automatic storage Heap – memory allocated for dynamic data. The heap and stack grow towards each other in the same memory space.

Fall 2010ECE Computer Organization22 Questions?