Stack Memory 2 (also called Call Stack)

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Run-Time Storage Organization
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
Run-time Environment and Program Organization
Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Methods & Activation Record. Recap: what’s a method?
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
ESP int f(int x) {.... } int g(int y) { …. f(2); …. } int main() { …. g(1); …. } EIP 100: 200: 250: 300: 350:
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
Runtime Environments Compiler Construction Chapter 7.
Cosc236/recursion1 Recursion Recursive method calls itself Recursion frequently occurs in mathematics Recursive definition –2 parts base part (basis) recursive.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
CSCI 171 Presentation 6 Functions and Variable Scope.
Recursion Using Stacks. What’s Going On? int main() { // Get Input int x, y; cout > x; // Call f y = f(x); // Print results.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
C Programming Chapters 11, . . .
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0.
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.
Chapter 2 Clarifications
Arrays 2/4 By Pius Nyaanga.
Understand argc and argv
Lecture 4: MIPS Instruction Set
Run-time organization
Yung-Hsiang Lu Purdue University
Depth First Search—Backtracking
Stack Memory 1 (also called Call Stack)
Multiple Files Revisited
understanding memory usage by a c++ program
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
Scope of Variables.
An Introduction to Java – Part I, language basics
Dynamic Memory A whole heap of fun….
The University of Adelaide, School of Computer Science
Understanding Program Address Space
EECE.3170 Microprocessor Systems Design I
Introduction to Programming
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Introduction to Programming
CS2011 Introduction to Programming I Arrays (II)
Writing Functions.
EECE.3170 Microprocessor Systems Design I
CS302 - Data Structures using C++
Observing how the machine acts
When a function is called...
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Method exercises Without IF
The Stack.
Unit-1 Introduction to Java
Corresponds with Chapter 5
Presentation transcript:

Stack Memory 2 (also called Call Stack) Yung-Hsiang Lu Purdue University

When a function is called, the line number after the call is stored in the stack memory.

Arguments in Function Call When a function is called with one (or several) argument, the argument is stored (pushed) to the stack memory. 1 void f1(int x) 2 { 3 int a; 4 a = f2(7); 5 x = a + 5; 6 ... 7 } 8 int f2(int y) 9 10 y = 7 line 5

Arguments in Function Call When a function is called with one (or several) argument, the argument is stored (pushed) to the stack memory. 1 void f1(int x) 2 { 3 int a; 4 a = f3(7, 3.2); 5 x = a + 5; 6 ... 7 } 8 int f3(int y, double z) 10 z = 3.2 y = 7 line 5

How to write good code? Make each function's behavior depends on only the input arguments and nothing else. The function has no static variable. The program has no global variable. Read "Global Variables Considered Harmful" by Wulf in 1973.

When a function is called, the statement after the call is stored in the stack memory. If the call has argument (or arguments), the arguments are stored in the stack memory.

The frame of a called function The return location and the arguments' values form a "frame" of the called function. 1 void f1(int x) 2 { 3 int a; 4 a = f3(7, 3.2); 5 x = a + 5; 6 ... 7 } 8 int f3(int y, double z) 9 10 Frame of f3 z = 3.2 y = 7 line 5

Local Variables A function's local variables are also stored in the frame. If a function has several local variables, all of them are stored in the frame. 1 void f1(int x) 2 { 3 int a; 4 a = f3(7, 3.2); 5 x = a + 5; 6 ... 7 } 8 int f3(int y, double z) 9 10 int b = y + 3; Frame of f3 b = 10 z = 3.2 y = 7 line 5

Stack Memory may store three things: return location arguments local variables