module invocation & parameters tracing the execution of instructions with the activation stack
TRANSCRIPT
![Page 1: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/1.jpg)
Module Invocation & Parameters Tracing the Execution of Instructions
with the Activation Stack
![Page 2: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/2.jpg)
Module Invocation & Parameters
![Page 3: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/3.jpg)
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.
![Page 4: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/4.jpg)
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
![Page 5: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/5.jpg)
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 endifendfunction
![Page 6: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/6.jpg)
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
![Page 7: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/7.jpg)
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)
![Page 8: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/8.jpg)
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)
![Page 9: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/9.jpg)
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
![Page 10: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/10.jpg)
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
![Page 11: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/11.jpg)
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
![Page 12: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/12.jpg)
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
![Page 13: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/13.jpg)
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).
![Page 14: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/14.jpg)
Comparing Parameter Types
INCopies 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.
OUTFormal 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/OUTFormal parameter is pseudonym for actual parameter and can access its original value and writes its values into actual parameter, overwriting the original.
![Page 15: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/15.jpg)
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_numendfunction
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
![Page 16: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/16.jpg)
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
![Page 17: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/17.jpg)
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
![Page 18: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/18.jpg)
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!
![Page 19: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/19.jpg)
Questions?
![Page 20: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/20.jpg)
Tracing the Execution of Instructionswith the Activation Stack
![Page 21: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/21.jpg)
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 3Push Pop
![Page 22: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/22.jpg)
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
![Page 23: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/23.jpg)
Thing 1
PUSH
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
What is a Stack?
![Page 24: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/24.jpg)
Thing 1
Thing 2
PUSH
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
![Page 25: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/25.jpg)
Thing 1
pop
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
![Page 26: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/26.jpg)
Thing 1
Thing 2
PUSH
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
![Page 27: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/27.jpg)
Thing 1
Thing 2
Thing 3
PUSH
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
![Page 28: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/28.jpg)
Thing 1
Thing 2
pop
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
![Page 29: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/29.jpg)
Thing 1
pop
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
![Page 30: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/30.jpg)
pop
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
![Page 31: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/31.jpg)
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 var2 var3
![Page 32: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/32.jpg)
Algo var1 var2 var3
Proc_1 this_var that_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.
![Page 33: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/33.jpg)
Proc_1 this_var that_var
Algo var1 var2 var3
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).
![Page 34: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/34.jpg)
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)
![Page 35: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/35.jpg)
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_var7 9
4
![Page 36: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/36.jpg)
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)
![Page 37: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/37.jpg)
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 <- 7this_one <- 171
this_var that_var other_var 9471
Print(this_one)
1
![Page 38: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/38.jpg)
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)
![Page 39: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/39.jpg)
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 <- 88
Algo this_var that_var other_var4 7 98
![Page 40: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/40.jpg)
Stack Trace Exampleprocedure 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 + 2endprocedure
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
![Page 41: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/41.jpg)
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 - 2endprocedure
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 a b c1 2 3
1 2 3
Juggle x y z3 3 5 2
5 4 3
RR6
5 4
![Page 42: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/42.jpg)
Questions?
![Page 43: Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack](https://reader036.vdocument.in/reader036/viewer/2022062417/551bcb9a550346b4588b5153/html5/thumbnails/43.jpg)