1 Procedural Programming Paradigm Stacks and Procedures.

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

Sub and Function Procedures
CPU Review and Programming Models CT101 – Computing Systems.
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.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
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:
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Run-Time Storage Organization
Chapter 9: Subprogram Control
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
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.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
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.
The Stack This is a special data structure: –The first item to be placed into the stack will be the last item taken out. Two basic operations: –Push: Places.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
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:
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Top-down approach / Stepwise Refinement & Procedures & Functions.
CSC 8505 Compiler Construction Runtime Environments.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
Week 12 Methods for passing actual parameters to formal parameters.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Computer Science 210 Computer Organization
CS 153: Concepts of Compiler Design October 5 Class Meeting
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Programming Fundamentals Lecture #7 Functions
In this lecture Global variables Local variables System stack
Pointers and Linked Lists
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Programming paradigms
The University of Adelaide, School of Computer Science
CIS16 Application Development and Programming using Visual Basic.net
10/4: Lecture Topics Overflow and underflow Logical operations
Data structures.
When a function is called...
Computer Organization and Assembly Language
Corresponds with Chapter 5
10.2 Procedures Passing Parameters 30/08/2019.
Procedures and Macros.
Presentation transcript:

1 Procedural Programming Paradigm Stacks and Procedures

2 Learning Objectives Describe the use of parameters, local and global variables as standard programming techniques. Explain how a stack is used to handle procedure calling and parameter passing.

3 When a procedure or function is called: The computer needs to know where to return to when the function or procedure is completed (return address). Further, functions and procedures may call other functions and procedures which means that not only must several return addresses be stored but they must be retrieved in the right order.

4 E.g. 3 functions are called after one another. The numbers represent the addresses of the instructions following the calls to functions.

5 Note: The addresses will be stored in the order 100, 150 then 250. When the returns take place the addresses will be needed in the order 250, 150 then 100.

6 Use a stack to store the return addresses. As last address stored is the first address needed on returning from a function. A stack provides this Last In First Out Facility (LIFO). A stack used for these addresses is known as the calling stack. A stack used for these addresses is known as the calling stack.

7 A diagram to illustrate: In the previous example, the addresses will be stored in the calling stack each time a function is called and will be removed from the calling stack each time a return instruction is executed.

8 Calls and Returns Stack Call Function A Push return address onto stack Stack pointer 100 Call Function B Push return address onto stack Stack pointer

9 Calls and Returns Stack Call Function C Push return address onto stack Stack pointer Return from C Pop return address off stack 250 Stack pointer

10 Calls and Returns Stack Return from B Pop return address off stack Stack pointer 100 Return from A Pop return address off stack Stack pointer NULL

11 Parameter passing Means passing variables to a procedure when a procedure is called. Actual Parameters (variables passed to the procedure): Actual Parameters (variables passed to the procedure): Formal Parameters (variables received by the procedure). Formal Parameters (variables received by the procedure).

12 Passing Value Parameters Value Means the variables are not passed back to the calling procedure. Means the variables are not passed back to the calling procedure. So should not be changed. This is accomplished by passing the values in the actual parameters then storing these values in formal parameters before use so that the originals are unaltered. Essentially local copies of the variables are made (not the original variables). Essentially local copies of the variables are made (not the original variables).

13 Stack pointer6 (X2) 3 (X1) 200 Diagram to illustrate Stack pointer is moved each time an address or actual parameter is popped onto or popped off the stack. Call Proc A (X1, X2) PUSH 200 PUSH X1 PUSH X2 (example values of X1 & X2 – value parameters)

14 6 (X2) 3 (X1) Stack pointer 200 Proc A (ByVal A1, ByVal A2) A2 = X2 (POP X2) A1 = X1 (POP X1) Stack pointer 15 (Y3) 8 (Y2) 18 (Y1) Call Proc B (Y1,Y2,Y3) PUSH 400 PUSH Y1 PUSH Y2 PUSH Y3 Return address for Proc A

15 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer Proc B (ByVal B1, ByVal B2, ByVal B3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) 15 (Y3) 8 (Y2) 18 (Y1) 400 Stack pointer 200 Return from Proc B

16 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer NULL Return from Proc A

17 Passing Reference Parameters Means the variables will be passed back and so should be changed (otherwise they should be passed by value). This is accomplished by passing the addresses of variables and not their values to the calling stack. The procedures, or functions, will access the actual addresses where the original variables are stored. Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program. Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program.

18 Diagram to illustrate Stack pointer is moved each time an address or actual parameter is popped onto or popped off the stack. Call Proc A(X1, X2) PUSH 200 PUSH X1 PUSH X2 (example address of X2 – reference parameter) (example value of X1 – value parameter) Stack pointer 600 (X2) 3 (X1) 200

(X2) 3 (X1) Stack pointer 200 Proc A (ByVal A1, ByRef A2) A2 = X2 (POP X2) A1 = X1 (POP X1) Stack pointer 1500 (Y3) 8 (Y2) 18 (Y1) Call Proc B(Y1,Y2,Y3) PUSH 400 PUSH Y1 PUSH Y2 PUSH Y3 Return address for Proc A

(Y3) 8 (Y2) 18 (Y1) Stack pointer Proc B (ByVal B1, ByVal B2, ByRef B3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) 1500 (Y3) 8 (Y2) 18 (Y1) 400 Stack pointer 200 Return from Proc B

(Y3) 8 (Y2) 18 (Y1) Stack pointer NULL Return from Proc A

22 How do functions return values? Functions do not have to return their “results” by passing a parameter by reference. (Variable name) = (Function Name)(Parameters) (Variable name) = (Function Name)(Parameters) (Control Name).Text = (Function Name)(Parameters) (Control Name).Text = (Function Name)(Parameters) Private Function …(ByVal … As …, ByVal… As …) Private Function …(ByVal … As …, ByVal… As …) Dim (Variable name to be returned) As (Data Type) Dim (Variable name to be returned) As (Data Type) … Return (Variable name to be returned) Return (Variable name to be returned) End Function End Function

23 How do functions return values (without use of reference parameters)? They push the value on the stack immediately before returning. The calling program can then pop the value off the stack. N.B. The return address has to be popped off the stack before pushing the return value onto the stack. The return address has to be popped off the stack before pushing the return value onto the stack.

24 Stack pointer6 (X2) 3 (X1) 200 Diagram to illustrate Stack pointer is moved each time an address or actual parameter is popped onto or popped off the stack. Fn A (X1, X2) PUSH 200 PUSH X1 PUSH X2 (example values of X1 & X2 – value parameters)

25 6 (X2) 3 (X1) Stack pointer 200 Fn A (A1,A2) A2 = X2 (POP X2) A1 = X1 (POP X1) Stack pointer 15 (Y3) 8 (Y2) 18 (Y1) Fn B (Y1,Y2,Y3) PUSH 400 PUSH Y1 PUSH Y2 PUSH Y3 Return address for Fn A

26 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer Fn B(B1,B2,B3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) 15 (Y3) 8 (Y2) 18 (Y1) 400 Stack pointer 200 Return from Fn B Return address popped off a. Return address popped off.

27 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer Return from Fn B Return value pushed on. b. Return value pushed on. (value returned by Fn B) 15 (Y3) 8 (Y2) 18 (Y1) 16 Stack pointer 200 Fn A Return value popped off. c. Return value popped off.

28 15 (Y3) 8 (Y2) 18 (Y1) 16 Stack pointer 32 Return from Fn A Return value pushed on. b. Return value pushed on. (value returned by Fn A) 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer NULL Return from Fn A Return address popped off a. Return address popped off.

29 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer NULL Main Program Return address popped off c. Return address popped off.

30 Plenary Explain the passing of parameters by reference and by value.

31 Plenary Value A constant or value of a variable is passed to a procedure using the calling stack. A constant or value of a variable is passed to a procedure using the calling stack. Procedure makes a copy of it before using it so that the original is unaltered. Procedure makes a copy of it before using it so that the original is unaltered.Reference The address of the value is passed to the procedure using the calling stack. The address of the value is passed to the procedure using the calling stack. Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program. Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program.