Recursive Programming

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

1 Procedural Programming Paradigm Stacks and Procedures.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
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
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
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)
Runtime Environments Source language issues Storage organization
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Run-Time Storage Organization
Intro to Computer Architecture
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
Run time vs. Compile time
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
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.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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.
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.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
CSC 8505 Compiler Construction Runtime Environments.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Storage Allocation Mechanisms
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.
Functions.
Computer Science 210 Computer Organization
Run-Time Storage Organization
Run-Time Storage Organization
Procedures (Functions)
Functions and Procedures
Activation Records and Function Calls
Stack Frame Linkage.
MIPS Instructions.
ECEG-3202 Computer Architecture and Organization
Chapter 6 - Procedures and Macros
ECEG-3202 Computer Architecture and Organization
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Program and memory layout
Program and memory layout
Course Overview PART I: overview material PART II: inside a compiler
Runtime Environments What is in the memory?.
The structure of programming
Program and memory layout
Program and memory layout
Thinking procedurally
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
SPL – PS2 C++ Memory Handling.
Runtime Stack Activation record for hanoi;
Presentation transcript:

Recursive Programming By E. W. Dijkstra

The Aim Old-skool way: static allocation Two problems: More memory is used than is needed No recursion!!!

The Aim Dijkstra's way: the stack!

Stack Requirements A static stack pointer variable Points to the top of the stack, or the first free space available to the stack Bunches of consecutive space to put the stack That's it!

Call Stack We need to organize our variables somehow. Parameters Local variables Anonymous immediate results--values that expressions evaluate to mid-procedure Function call return values Nested algebraic expressions These can be placed in a separate stack, or atop the call stack.

Nested Algebraic Expressions Plotting “(” as a function call and “)” as a return on a time line results in correctly-nested pairs of parentheses. Likewise, complex algebraic expressions can be computed as a bunch of tiny subroutines, the parameters of which (the numbers) can be stored on a stack efficiently and simply.

Nested Algebraic Expressions This can be achieved with two simple operations: select a new number X, described as: vk := X; k := k+1; Perform an arithmetic operation OP: k:= k-1; vk-1 := vk-1 OP vk;

Nested Algebraic Expressions For example: A+(B-C)X(D/E+F) yields: V0 := A; v1:= B; v2 := C; v1 := v1-v2; v2 := D; v3 := E; v2 := v2/v3; v3 := F; v2 := v2+v3; v1 := v1Xv2; v0 := v0+v1; V0 is the required result at the end.

Nested Algebraic Expressions What if symbols A, B, C, etc. are functions requiring further calculations? For example, if C := P/Q-R+SxT then C's value would still end up in v2, and the subroutine's temporary data could be stored on the stack the same way without ruining anything before or after the call of the C function. This means Dijkstra's simple stack system works for lotsa cases. This is a good thang.

Performance Issues Dijkstra suggests future systems designs might need to be created with stacks in mind to achieve desirable performance. MIPS has registers for parameter passing, return values, the stack pointer, and the return address. C / C++ uses these registers when they are sufficient, but Java pushes all parameters to the stack. For nested algebraic expressions, magnetic tape can be used in the most efficient way possible for stack storage. HA!

Performance Issues The stack also allows programs written with block structuring to save space at the cost of time by allocating space at the top of the stack for variables within a block only when they are needed. Space is saved at the cost of performance because the address in relation to the stack pointer must be calculated at runtime.

The End