Infix, Postfix and Stacks

Slides:



Advertisements
Similar presentations
Computer Architecture and the Fetch-Execute Cycle
Advertisements

Instruction Set Design
There are two types of addressing schemes:
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Computer Organization and Architecture
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
1BA3 G Lacey Lecture 51 Evaluating mathematical expressions  How do computers evaluate x + y or any mathematical expression ?  Answer : “Reverse Polish.
TK 2633 Microprocessor & Interfacing
Execution of an instruction
Infix, Postfix, Prefix.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
The Microarchitecture Level The level above the digital logic level is the microarchitecture level.  Its job is to implement the ISA (Instruction Set.
Tannenbaum Machine. Key Features Simple architecture Simple instruction set Focus on –Input -> Higher Level Language –Output -> Machine Language Addressing.
Group 5 Alain J. Percial Paula A. Ortiz Francis X. Ruiz.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Lecture 13 - Introduction to the Central Processing Unit (CPU)
Lecture 18 Last Lecture Today’s Topic Instruction formats
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Control pins and Writing Microcode. Simple architecture Recall our architecture from the previous week It was a simple bus architecture “Control” was.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
Lecture 3. Diff b/w RAM and Registers Registers are used to hold data immediately applicable to the operation at hand Registers are used to hold data.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Programmer's view on Computer Architecture by Istvan Haller.
Computer Architecture and the Fetch-Execute Cycle
Computer Architecture and the Fetch-Execute Cycle
Microcode Source: Digital Computer Electronics (Malvino and Brown)
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.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Execution of an instruction
PHY 201 (Blum)1 Microcode Source: Digital Computer Electronics (Malvino and Brown)
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Instructions. Portability In addition to making hardware backward compatible, we have also made software portable. In describing software, “portable”
UConn CSE CSE241: Instruction Level Architecture Base CPU/Memory Architecture Registers Fetch-Execute Cycle Instructions Addressing Modes.
CSC 8505 Compiler Construction Runtime Environments.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CSIT 301 (Blum)1 Instructions at the Lowest Level Some of this material can be found in Chapter 3 of Computer Architecture (Carter)
PHY 201 (Blum)1 Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter.
Computer Organization Instructions Language of The Computer (MIPS) 2.
8085 INTERNAL ARCHITECTURE.  Upon completing this topic, you should be able to: State all the register available in the 8085 microprocessor and explain.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
ECE 3430 – Intro to Microcomputer Systems
Instructions at the Lowest Level
Chapter 10 The Stack.
About Instructions Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter PHY 201 (Blum)
CSCE Fall 2013 Prof. Jennifer L. Welch.
Chapter 8 Central Processing Unit
Stacks.
CSCE Fall 2012 Prof. Jennifer L. Welch.
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Instructions.
Computer Organization and Assembly Language
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Infix, Postfix and Stacks CSC 370 (Blum)

Ordering of opcodes and operands Another example of syntax is the ordering of opcode and operand(s). Postfix: operand(s) then opcode 4 5 + Works well with stacks Prefix: opcode then operand(s) + 4 5 Infix: operand opcode operand 4 + 5 CSC 370 (Blum)

Precedence Precedence is the order in which operations occur when an expression contains more than one operation. Operations with higher precedence are performed before operators with lower precedence. 1 + 2 * 3 - 4 1 + 6 - 4 (multiplication has higher precedence) 7 - 4 (start on the left when operators have the same precedence) 3 CSC 370 (Blum)

Infix to postfix To convert 1+2*3-4, put in parentheses even though they’re not strictly necessary for this expression ((1+(2*3))-4) Convert the innermost parentheses to postfix: 2*3 becomes 2 3 * ((1+(2 3 *))-4) Once a group is in postfix order, it should be thought of as a unit (in particular as a single operand (data)), nothing should come in between any of the parts of the group Convert the next set of parentheses ((1 2 3 * +)-4) CSC 370 (Blum)

Infix to postfix The last step eliminated the innermost set of parentheses. Continue to convert from infix to postfix from the innermost to outermost parentheses. (1 2 3 * + 4 -) Note there is one overall set of parentheses that can be thrown away. Also note that the order of the numbers has not changed. CSC 370 (Blum)

Another example 1+ (2+3) * 4 + (5 + 6) * ((7 + 8) * 9) Add parentheses 1+ ((2+3) * 4) + ((5 + 6) * ((7 + 8) * 9)) (1+ ((2+3) * 4)) + ((5 + 6) * ((7 + 8) * 9)) ((1+ ((2+3) * 4)) + ((5 + 6) * ((7 + 8) * 9))) Convert innermost to postfix ((1+ ((2 3 +) * 4)) + ((5 6 +) * ((7 8 +) * 9))) CSC 370 (Blum)

Another Example (Cont.) ((1+ ((2 3 +) * 4)) + ((5 6 +) * ((7 8 +) * 9))) ((1+ (2 3 + 4 * )) + ((5 6 +) * (7 8 + 9 * ))) ((1 2 3 + 4 * +) + (5 6 + 7 8 + 9 * *)) ( 1 2 3 + 4 * + 5 6 + 7 8 + 9 * * + ) CSC 370 (Blum)

Postfix good for Hardware Postfix order is better suited for hardware since one must prepare the inputs (a.k.a. the data, a.k.a. the operands) before operating on them to get an output. Postfix is particularly well suited for architectures that use a stack to perform computations. CSC 370 (Blum)

The stack A stack is a data structure (which may be implemented in hardware or in software) that holds a sequence of data but limits the way in which data is accessed. A stack obeys the Last-In-First-Out (LIFO) protocol, the last item written (pushed) is the first item to be read (popped). CSC 370 (Blum)

Stack 2 is pushed onto the stack 2 is popped off of the stack 4 2 3 3 1 1 1 1 1 CSC 370 (Blum)

Stack Pointer: don’t move all the data just change the pointer X 4 3 1 X 1 X 2 1 X 2 1 X 3 1 The stack pointer is pointing to the next available location in the stack. When it’s pointing at the 2, the 2 is no longer on the stack. CSC 370 (Blum)

Infix Evaluation 1+ (2+3) * 4 + (5 + 6) * ((7 + 8) * 9) 1+(5)*4 + (11)*((15)*9) 1 + 20 + 11*135 1 + 20 + 1485 21 + 1485 1506 CSC 370 (Blum)

Evaluating a postfix expression using a stack (1) Enter the postfix expression and click Step CSC 370 (Blum)

Evaluating a postfix expression using a stack (2) CSC 370 (Blum)

Evaluating a postfix expression using a stack (3) CSC 370 (Blum)

Evaluating a postfix expression using a stack (4) CSC 370 (Blum)

Evaluating a postfix expression using a stack (5) CSC 370 (Blum)

Evaluating a postfix expression using a stack (6) CSC 370 (Blum)

Evaluating a postfix expression using a stack (7) CSC 370 (Blum)

Evaluating a postfix expression using a stack (8) CSC 370 (Blum)

Evaluating a postfix expression using a stack (9) CSC 370 (Blum)

Evaluating a postfix expression using a stack (10) CSC 370 (Blum)

Evaluating a postfix expression using a stack (11) CSC 370 (Blum)

Evaluating a postfix expression using a stack (12) CSC 370 (Blum)

Evaluating a postfix expression using a stack (13) CSC 370 (Blum)

Evaluating a postfix expression using a stack (14) CSC 370 (Blum)

Evaluating a postfix expression using a stack (15) CSC 370 (Blum)

Evaluating a postfix expression using a stack (16) CSC 370 (Blum)

Evaluating a postfix expression using a stack (17) CSC 370 (Blum)

Evaluating a postfix expression using a stack (18) CSC 370 (Blum)

Evaluating a postfix expression using a stack (19) CSC 370 (Blum)

Evaluating a postfix expression using a stack (20) CSC 370 (Blum)

Evaluating a postfix expression using a stack (21) CSC 370 (Blum)

Evaluating a postfix expression using a stack (22) CSC 370 (Blum)

Evaluating a postfix expression using a stack (23) CSC 370 (Blum)

Evaluating a postfix expression using a stack (24) CSC 370 (Blum)

Evaluating a postfix expression using a stack (25) CSC 370 (Blum)

Evaluating a postfix expression using a stack (26) CSC 370 (Blum)

Evaluating a postfix expression using a stack (27) CSC 370 (Blum)

Evaluating a postfix expression using a stack (28) CSC 370 (Blum)

Evaluating a postfix expression using a stack (29) CSC 370 (Blum)

Evaluating a postfix expression using a stack (30) CSC 370 (Blum)

Evaluating a postfix expression using a stack (31) CSC 370 (Blum)

Evaluating a postfix expression using a stack (32) CSC 370 (Blum)

Evaluating a postfix expression using a stack (33) CSC 370 (Blum)

Evaluating a postfix expression using a stack (34) CSC 370 (Blum)

Infix to Postfix (Approach 1) 9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Introduce parentheses that do not change the order of operations 9 + (8 + 7) * 6 + 5 * (4 + ((3 * 2) + 1)) 9 + ((8 + 7) * 6) + (5 * (4 + ((3 * 2) + 1))) (9 + ((8 + 7) * 6)) + (5 * (4 + ((3 * 2) + 1))) ((9 + ((8 + 7) * 6)) + (5 * (4 + ((3 * 2) + 1)))) Note that there are nine operands, eight operators, eight left parentheses and eight right parentheses. CSC 370 (Blum)

Infix to Postfix (Approach 1, Cont.) ((9 + ((8 + 7) * 6)) + (5 * (4 + ((3 * 2) + 1)))) Convert the innermost parentheses to postfix ((9 + ((8 7 +) * 6)) + (5 * (4 + ((3 2 *) + 1)))) ((9 + ((8 7 +) 6 *)) + (5 * (4 + ((3 2 *) 1 +)))) ((9 ((8 7 +) 6 *) +) + (5 * (4 ((3 2 *) 1 +) +))) ((9 ((8 7 +) 6 *) +) + (5 (4 ((3 2 *) 1 +) +) *)) ((9 ((8 7 +) 6 *) +) (5 (4 ((3 2 *) 1 +) +) *) +) 9 8 7 + 6 * +5 4 3 2 * 1 + + * + CSC 370 (Blum)

Backwards 9 8 7 + 6 * +5 4 3 2 * 1 + + * + 9 (8 + 7) 6 * +5 4 3 2 * 1 + + * + 9 ((8 + 7) * 6) +5 4 3 2 * 1 + + * + (9 + ((8 + 7) * 6)) 5 4 3 2 * 1 + + * + (9 + ((8 + 7) * 6)) 5 4 (3 * 2) 1 + + * + (9 + ((8 + 7) * 6)) 5 4 ((3 * 2) + 1) + * + (9 + ((8 + 7) * 6)) 5 (4 + ((3 * 2) + 1)) * + (9 + ((8 + 7) * 6)) (5 * (4 + ((3 * 2) + 1))) + (9 + ((8 + 7) * 6)) + (5 * (4 + ((3 * 2) + 1))) CSC 370 (Blum)

Infix to Postfix The approach taken for converting infix to postfix does not make for a good algorithm as it requires too many passes. One passes over the expression introducing parentheses One pass over the expression converting inner parentheses to postfix Fortunately there is a more efficient algorithm that requires only one pass through the expression. CSC 370 (Blum)

Infix to Postfix (Approach 2) This approach involves a stack used for operators. Proceed through the expression from left to right. Operands (numbers) are automatically added to the postfix expression we are generating (i.e. not put on the operator stack). What happens to a new operator depends on its precedence compared to that of the operator on the top of the stack. CSC 370 (Blum)

Infix to Postfix (Approach 2, Cont.) If the new operator has a higher precedence than the one on the top of the stack, it is pushed onto the stack. If the top of the stack is an open parenthesis, then the new operator is pushed onto the stack. If the operator has an equal or lower precedence than the one on the top of the stack, then the top of the stack is popped and added to the postfix expression until this is no longer the case. Then the current operator is pushed onto the stack. CSC 370 (Blum)

Infix to Postfix (Approach 2, Cont.) Left (opening parentheses) are always added to the stack. Right (closing parentheses) lead to everything up to and including the corresponding left parenthesis to be popped from the stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack Postfix Expression 9 Comment: Add 9 to expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + Postfix Expression 9 Comment: Push + onto operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack ( + Postfix Expression 9 Comment: Push ( onto operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack ( + Postfix Expression 98 Comment: Add 8 to postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + ( Postfix Expression 98 Comment: Push + onto operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + ( Postfix Expression 987 Comment: Add 7 to postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + Postfix Expression 987+ Comment: Pop + and ( from operator stack, add + to postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack * + Postfix Expression 987+ Comment: Push * onto operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack * + Postfix Expression 987+6 Comment: Add 6 to postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + Postfix Expression 987+6*+ Comment: Pop * and + off the stack and add them to the postfix expression, then push the + onto the stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + Postfix Expression 987+6*+5 Comment: Add 5 to the postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack * + Postfix Expression 987+6*+5 Comment: Push * onto the operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack ( * + Postfix Expression 987+6*+5 Comment: Push ( onto the operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack ( * + Postfix Expression 987+6*+54 Comment: Add 4 to the postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + ( * Postfix Expression 987+6*+54 Comment: Push + onto the operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack ( + * Postfix Expression 987+6*+54 Comment: Push ( onto the operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack ( + * Postfix Expression 987+6*+543 Comment: Add 3 to the postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack * ( + Postfix Expression 987+6*+543 Comment: Push * onto the operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack * ( + Postfix Expression 987+6*+5432 Comment: Add 2 to the postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + ( * Postfix Expression 987+6*+5432* Comment: Pop * from the operator stack and add it to the postfix expression, then push + onto operator stack. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + ( * Postfix Expression 987+6*+5432*1 Comment: Add 1 to the postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack + ( * Postfix Expression 987+6*+5432*1+ Comment: Pop the + and ( from the operator stack, add + to the postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack * + Postfix Expression 987+6*+5432*1++ Comment: Pop the + and ( from the operator stack, add + to the postfix expression. CSC 370 (Blum)

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) Operator Stack Postfix Expression 987+6*+5432*1++*+ Comment: Pop the * and then the + from the operator stack, add them the postfix expression. CSC 370 (Blum)

Another use for a stack We have seen a stack used for translating an infix expression into postfix. We have also seen a stack used to calculate a postfix expression. Another use of a stack is in subroutine and/or function calls. CSC 370 (Blum)

Bus Keyboard encoder Input port 1 Accumulator Flags ALU Input port 2 Prog. counter TMP Mem.Add.Reg. B Memory C MDR Output port 3 Display Instr. Reg. Output port 4 CSC 370 (Blum) Control

Jump A processor generally assumes that the next statement to be executed is the next line of the code stored in memory. Recall part of the fetch-execute cycle is to increment the program counter (PC). A jump or goto alters this usual plan. A new value is placed into the program counter. CSC 370 (Blum)

Conditional Jump A conditional jump will change the PC or leave it unchanged based on the state of one of the Flag registers. The ALU can perform some comparison and place the result in a flag register, then the flag’s value could eventually be fed to the PC load control. So the PC is loaded (changed) or not based on the flag which came from the comparison. Conditional jumping gives the programmer ifs and loops. CSC 370 (Blum)

Calling a subroutine A subroutine call involves a jump. This “passes the control” to the subroutine. But a subroutine call differs from an ordinary jump in that one wants to return. One reason for having subroutines is to reduce repeated code. The subroutine may be called from any number of locations, so the return address is not known until the program is running. CSC 370 (Blum)

Storing the PC Prior to executing the jump portion of a call, the PC holds the address of the line of code immediately following the call instruction. And this is where one wants to be upon returning from the subroutine. Thus we have to store the value of the program counter (somewhere) and then change the value of the PC (i.e. execute the jump) CSC 370 (Blum)

A subroutine can call another subroutine that can call another subroutine that …. There cannot be a simple register for storing the PC (return address) because a subroutine can call another subroutine requiring two stored values. CSC 370 (Blum)

Calling and Returning Calling: If Main calls RoutineA, we store the MainReturnAddress. Then if RoutineA calls RoutineB, we store RoutineAReturnAddress. Returning: RoutineB returns control to RoutineA so we first need the last return address stored (RoutineAReturnAddress). Then RoutineA returns the control to Main and we need MainReturnAddress. The last return address stored is the first one needed, this is the protocol of a stack. (LIFO) CSC 370 (Blum)

Call is to Push as Return is to Pop A subroutine call thus requires pushing the value of the PC onto a stack (and then changing the value of the PC). A return from a subroutine requires popping the return address from the stack (and then placing that value in the PC). CSC 370 (Blum)

More to it Subroutines and functions typically have arguments/parameters. This information is not known until run-time and must be passed to the subroutine/function. There must be memory locations (where are they?) to serve as the variables in the subroutine. And any passed information must be written to the appropriate location. CSC 370 (Blum)

By reference or by value The arguments of the subroutine call can be actual numbers Y = sin(3) Or they could be variables associated with the caller. Y = sin(x) There are two possible scenarios in the later case, by value and by reference. CSC 370 (Blum)

By value Passing a parameter by value means that the memory location associated with the subroutine will hold a value and that that value will be written into the memory location when the function is called. Any changes to that memory location have no effect of the memory location associated with the caller. CSC 370 (Blum)

By reference Passing a parameter by reference means that the memory location associated with the subroutine will hold the address of the variable in the caller. All actions on the variable in the subroutine are of the indirect variety (recall the different addressing modes). One goes to this location to find an address, then goes to that address, …. The caller and the called write values to the same memory location. CSC 370 (Blum)

Return value In addition to the passed parameters and any local variables required, a function also has a memory location associated with the value it will send back. CSC 370 (Blum)

Allowing more than one version of a subroutine to be active Where are all of these memory locations associated with subroutines? The answer can depend on the how many active versions of a given subroutine one allows. A subroutine is considered active if it has started executing but hasn’t finished executing. That doesn’t imply it has control as it may have called some other routine before finishing. CSC 370 (Blum)

Activation Record All of the information required for an active subroutine Return address Passed parameters Local variables Return value constitute what is called the activation record. CSC 370 (Blum)

Multiprocessing and recursion One way in which a CPU may end up with more than one active versions of a subroutine is if it is multiprocessing, running more than one program at once (actually swapping between programs to give the illusion of running more than one.) But even a single process may require multiple active versions of a subroutine if the subroutine is recursive. CSC 370 (Blum)

One can’t know ahead of time If one allows for recursive subroutines, then one cannot know beforehand (at compile time) how much memory will be used by a subroutine since each active version requires separate memory. Recursion programs can be very memory intensive. A common approach is that when a subroutine is called, an activation record is placed on the run-time stack. For this reason, activation records are sometimes called stack frames. CSC 370 (Blum)

One more thing to remember Recall that the accumulator and other registers serve as a scratch pad area for the ALU. The ALU may be in the middle of some calculation when the subroutine is called and it may need to remember those temporary, scratch-pad values. z = sin(x) + sqrt(y) We cannot lose our result for sin(x) when we evaluate sqrt(y) Thus the stack frame may also include a copy of various register values. CSC 370 (Blum)

References Computer Architecture, Nichols Carter Modern Programming Languages, Adam Brooks Webber Computers Systems: Organization and Architecture, John Carpinelli http://web.engr.oregonstate.edu/~minoura/cs261/javaProgs/stack/Polish.html CSC 370 (Blum)