Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack.

Slides:



Advertisements
Similar presentations
Introduction to Visual Basic and the VB IDE
Advertisements

FPU structure. Assumptions (to shorten execution trace) – 2 instructions dispatched in order per cycle – execution begins in same cycle as dispatch –
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: Parameter.
Remote Procedure Call in SR Programming Language By Tze-Kin Tsang 3/20/2000.
An introduction to systems programming
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
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.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
Logic Problems This is an assignment. There are no speaker notes.
Introduction to Method. Example Java Method ( 1 ) The 2 types (kinds) of methods in Java Class methods Instance methods Methods can do more work than.
Functions in Assembly CS 210 Tutorial 7 Functions in Assembly Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/7/
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Introduction to Recursion. Recursion Defined A procedure or function which calls itself. Powerful mechanism for repetition. Makes algorithms more compact.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Methods.
CS Algorithm Development Modularity. Objectives Do “high-level design” Practice abstraction by decomposing problems into their sub-tasks Define.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Improving the support for ARM in IgProf
Chapter 5 Function Basics
Chapter 6 Functions.
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
ALGORITHMS & FLOWCHARTING II
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
” روان شناسی تربیتی ” Educational Psychology
Chapter 5 Function Basics
بسم الله الرحمن الرحيم هل اختلف دور المعلم بعد تطبيق المنهج الحديث الذي ينادي بتوفير خبرات تعليمية مناسبة للطلبة ؟ هل اختلف دور المعلم ؟ ن.ن. ع.
The method invocation mechanism and the System Stack
المدخل إلى تكنولوجيا التعليم في ضوء الاتجاهات الحديثة
Topological Ordering Algorithm: Example
مهارات التدريس الفعال.
Understanding Program Address Space
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Chapter 6 Methods.
Introduction to Recursion
Chapter 10: Method Overriding and method Overloading
Topological Ordering Algorithm: Example
More Functions Chapter 20.
Topological Ordering Algorithm: Example
Identification of Variation Points Using Dynamic Analysis
C H A P T E R F I V E Memory Management.
Handout-16 Recursion Overview
Unit-1 Introduction to Java
Optimizing J2ME applications with cross-module method inlining
Type Topic in here! Created by Educational Technology Network
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Topological Ordering Algorithm: Example
Runtime Stack Activation record for hanoi;
Procedures & Macros Introduction Syntax Difference.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack

Module Invocation & Parameters

As Modules are Invoked algorithm Nonsense // dummy alg to show calls this_var isoftype Num this_var <- 6 this_var <- Square(this_var) Output_Results(this_var) endalgorithm // Nonsense Use parameters to specify which of your variables you wish to have communicate with the subroutine you are calling. In this case, this_var is being passed first to function Square, and then is passed to procedure Output_Results.

As Modules are Declared function Square returnsa Num (the_num isoftype in Num) // returns the square of the_num Square returns (the_num * the_num) endfunction procedure Output_Results (answer isoftype in Num) // prints out answer with text message print(“The answer is”, answer) endprocedure Each parameter variable has two types: a parameter type and a data type Each function also has its own data type specifying the type of data of its result

Specifying Multiple Parameters function Min returnsa Num (num_one, num_two, num_three isoftype in Num) //returns smallest of three numbers if ((num_one <= num_two) AND (num_one <= num_three )) then Min returns num_one elseif ((num_two <= num_three ) AND (num_two <= num_one)) then Min returns num_two else Min returns num_three endif endfunction

Using Multiple Parameters In use… algorithm Example small, medium, large, smallest isoftype Num small <- 4 medium <- 6 large <- 34 smallest <- Min(small, medium, large) print(“The smallest is”, smallest) endalgorithm

Formal Parameter Lists In the declaration header: function Min returnsa Num (num_one,num_two,num_three isoftype in Num) the formal parameter list (parameter variables and their types are declared here)

Actual Parameter Lists In the call to the module: smallest <- Min(small,medium,large) the actual parameter list (cannot tell what their types are from here)

Rules for Parameter Lists The number of parameters in the actual and formal parameter lists must be consistent. Parameter association is positional: the first actual parameter matches the first formal parameter, the second matches the second, and so on. Actual parameters and formal parameters must be of the same data type

Information Flow Between Modules In the calling module/algorithm (client) Do_Something(param1, param2, param3) Procedure Do_Something ( my_num isoftype in num, my_char isoftype in/out char, your_string isoftype out string) In the called module (server) actual parameters formal parameters

Input Parameters Pass a copy of the original to a module Advantage: IN parameters protect data integrity The module that receives an input parameter can modify only its own copy of it. It cannot modify the original, thus is safer. Can pass any expression as input parameters (since the original will not be changed) For functions, all parameters must be input parameters (no side-effects allowed) For procedures, use input parameters unless you have a reason to use one of the other parameter types

Output Parameters Output parameters are a means by which procedures may return values Output parameters overwrite the original values (if any) of variables given in the actual parameter list. Actual output parameters can not be literals, constants, or expressions – they must be variables The module does not know the original values of the actual parameters Functions cannot have outside effects, so output parameters are not permitted in functions

Input / Output Parameters Input/Output parameters allow data to be passed both ways. The original value of the actual parameter is accessible to the procedure. The original value can be overwritten by the procedure. The calling algorithm gives the procedure access to both read and overwrite the original value of the actual parameter. In/out parameters can only be variables (so that we can write information into them).

Comparing Parameter Types IN Copies value of actual parameter into formal parameter when procedure starts; the actual parameter’s value cannot be changed, and only the module’s copy of it is modified. OUT Formal parameter is pseudonym for actual parameter, the module writes its value into actual parameter, overwrites the actual parameter’s original value, and cannot see what data was originally stored there. IN/OUT Formal parameter is pseudonym for actual parameter and can access its original value and writes its values into actual parameter, overwriting the original.

procedure myread (out_num isoftype ??? Num) print(“Enter a number:”) read(out_num) // out_num <- ? endprocedure function double returnsa Num (in_num iot ??? Num) double returns 2 * in_num endfunction algorithm Get_Numbers num_one, num_two isoftype Num myread(num_one) num_one <- double(num_one) myread(num_two) num_two <- double(num_two) print(num_one, num_two) endalgorithm out in

procedure double_them (num1, num2 iot ?????? Num) num1 <- num1 * 2 num2 <- num2 * 2 endprocedure // double_them algorithm Number_Magic num_three, num_four isoftype Num print (“Please enter two numbers”) read (num_three, num_four) double_them(num_three, num_four) print(“Double values:”, num_three, num_four) endalgorithm in/out

Summary of Parameter Usage Use parameters for all communication between modules Input parameters are safe; the module gets a copy Output parameters are used to pass values back from a procedure –The procedure gets write access but no read access to the original, actual parameter Input/Output parameters give a procedure both read and write access to the original Functions may only have input parameters Procedures may use all three types as needed Use input parameter unless there is a reason to do otherwise

procedure demo(a iot in Num, b iot in/out Num, c iot out Num) a <- a * 2 b <- b * 2 c <- c * 2 c <- a + b print(a, b, c) endprocedure algorithm test x, y, z isoftype Num x <- 2 y <- 4 z <- 6 print(x, y, z) demo(x, y, z) print(x, y, z) endalgorithm Pop Quiz LB // ILLEGAL!

Questions?

Tracing the Execution of Instructions with the Activation Stack

Stacks Is a “pile” or “collection” or “set” of items. Items can only be added to the top Items can only be taken off the top “Last-in-first-out” (”LIFO”) Thing 1 Thing 2 Thing 3 PushPop

What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

Thing 1 PUSH A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO). What is a Stack?

Thing 1 Thing 2 PUSH What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

Thing 1 pop What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

Thing 1 Thing 2 PUSH What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

Thing 1Thing 2 Thing 3 PUSH What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

Thing 1Thing 2 pop What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

Thing 1 pop What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

pop What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

The Activation Stack The white box represents the computers memory. The algorithm gets the first frame at the bottom of the stack. Variables which are used within a module reside in that module’s stack frame. Algo var1 var2var3

Algo var1var2var3 Proc_1 this_varthat_var Adding Modules to the Stack When main calls a module, the module gets its own frame “pushed” on the stack. The module’s variables live in that new frame. ONLYONLY the top frame is active. All frames underneath it are stopped.

Proc_1 this_varthat_var Algo var1var2var3 Removing modules from the stack When the module completes its instructions, it’s frame is “popped” off the stack and all of its data dies. To survive, they must actually be stored in a frame that still exists (passed back).

How In Parameters Work In formal parameters get a copy of the matching actual parameter. In the algorithm: Proc_One (this_var) In the module: procedure Proc_One(this_one iot in num)

Proc_One this_one 4 Algo 4 How In Parameters Work In formal parameters get a copy of the matching actual parameter. this_var that_var other_var 7 9 4

How In/Out Parameters Work In/out formal parameters act as a reference to the matching actual parameter. Reading and writing are allowed. In the algorithm: Proc_One (this_var) In the module: procedure Proc_One(this_one iot in/out num)

Algo 7 Proc_One this_one How In/Out Parameters Work In/out formal parameters act as a reference to the matching actual parameter. Reading and writing are allowed. R this_one <- 7 this_one < this_var that_var other_var Print(this_one) 1

How Out Parameters Work Out formal parameters act as a reference to the matching actual parameter. Writing is only allowed at first. After writing, reading is also allowed. In the algorithm: Proc_One (this_var) In the module: procedure Proc_One(this_one iot out num)

How Out Parameters Work Out formal parameters act as a reference to the matching actual parameter. Writing is only allowed at first. After writing, reading is also allowed. Proc_One this_one R this_one <- 8 8 Algo this_var that_var other_var

Stack Trace Example procedure juggle (x isoftype in num, y isoftype out num, z isoftype in/out num) y <- x + z print (x, y, z) x <- z + 4 z <- x + 2 endprocedure algorithm TraceExample a, b, c isoftype num a <- 1 b <- 2 c <- 3 print(a, b, c) juggle(c, a, b) print(a, b, c) endalgorithm

Procedure Juggle(x iot in Num, y iot out Num, z iot in/out Num) y <- x + z print(x,y,z) x <- z + 4 z <- x - 2 endprocedure Simple Stack Trace Example Algorithm Demo a, b, c isoftype num a <- 1 b <- 2 c <- 3 print(a,b,c) juggle(c,a,b) print(a,b,c) endalgorithm Output Demo abc Juggle xyz R R 6 5 4

Questions?