1 procedural programming paradigm stacks and procedures
Post on 14-Dec-2015
236 Views
Preview:
TRANSCRIPT
11
Procedural Programming Procedural Programming ParadigmParadigm
Stacks and ProceduresStacks and Procedures
22
Learning ObjectivesLearning 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.
33
When a procedure or function is When a procedure or function is called:called:
The computer needs to know where to The computer needs to know where to return to when the function or procedure is return to when the function or procedure is completed (completed (return addressreturn address).).
Further, functions and procedures may call Further, functions and procedures may call other functions and procedures which other functions and procedures which means that not only must several return means that not only must several return addresses be stored but they must be addresses be stored but they must be retrieved in the right order. retrieved in the right order.
44
E.g. 3 functions are called after one another.E.g. 3 functions are called after one another.
The numbers represent the addresses of the The numbers represent the addresses of the instructions following the calls to functions. instructions following the calls to functions.
55
Note:Note:
The addresses will be stored in the order The addresses will be stored in the order 100, 150 then 250. 100, 150 then 250.
When the returns take place the When the returns take place the addresses will be needed in the order 250, addresses will be needed in the order 250, 150 then 100.150 then 100.
66
Use a stack to store the return Use a stack to store the return addresses.addresses.
As last address stored is the first address As last address stored is the first address needed on returning from a function.needed on returning from a function.
A stack provides this A stack provides this LLast ast IIn n FFirst irst OOut ut FFacility (acility (LIFOLIFO).). A stack used for these addresses is known as A stack used for these addresses is known as
the calling stack.the calling stack.
77
A diagram to illustrate:A diagram to illustrate:
In the previous example, the addresses In the previous example, the addresses will be stored in the calling stack each time will be stored in the calling stack each time a function is called and will be removed a function is called and will be removed from the calling stack each time a return from the calling stack each time a return instruction is executed. instruction is executed.
88
Calls and Returns Calls and Returns StackStackCall Function ACall Function A
Push return address Push return address
onto stackonto stack
Stack pointerStack pointer 100100
Call Function BCall Function B
Push return address Push return address
onto stackonto stack
Stack pointerStack pointer 150 150
100100
99
Calls and Returns Calls and Returns StackStackCall Function CCall Function C
Push return address Push return address
onto stackonto stack Stack pointerStack pointer 250250150150100100
Return from CReturn from C
Pop return addressPop return address
off stackoff stack250250
Stack pointerStack pointer 150 150
100100
1010
Calls and Returns Calls and Returns StackStackReturn from BReturn from B
Pop return addressPop return address
off stackoff stack 250250150150
Stack pointerStack pointer 100100
Return from AReturn from A
Pop return addressPop return address
off stackoff stack250250
150 150
100100
Stack pointer NULLStack pointer NULL
1111
Parameter passingParameter passing
Means passing variables to a procedure Means passing variables to a procedure when a procedure is called.when a procedure is called.
Actual Parameters (variables passed to the Actual Parameters (variables passed to the procedure):procedure):
Formal Parameters (variables received by Formal Parameters (variables received by the procedure).the procedure).
1212
Passing Value ParametersPassing Value Parameters
ValueValue Means the variables are not passed back to Means the variables are not passed back to
the calling procedure.the calling procedure.So should not be changed.So should not be changed.
This is accomplished by passing the This is accomplished by passing the values in the actual parameters then values in the actual parameters then storing these values in formalstoring these values in formal parameters parameters before use so that the originals are before use so that the originals are unaltered.unaltered. Essentially local copies of the variables are Essentially local copies of the variables are
made (not the original variables).made (not the original variables).
1313
Stack pointer 6 (X2)
3 (X1)
200
Diagram to illustrateDiagram to illustrate
Stack pointer is moved each time an address or Stack pointer is moved each time an address or actual parameter is popped onto or popped off actual parameter is popped onto or popped off the stack.the stack.
Call Proc A (X1, X2)PUSH 200PUSH X1PUSH X2
(example values of X1 & X2 – value parameters)
1414
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)
400
200
Call Proc B (Y1,Y2,Y3)PUSH 400PUSH Y1PUSH Y2PUSH Y3
Return address for Proc A
1515
15 (Y3)
8 (Y2)
18 (Y1)
Stack pointer 400
200
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
1616
15 (Y3)
8 (Y2)
18 (Y1)
400
200
Stack pointer NULL Stack pointer NULL
Return from Proc A
1717
Passing Reference ParametersPassing Reference Parameters
Means the variables will be passed back and so Means the variables will be passed back and so should be changed should be changed (otherwise they should be passed (otherwise they should be passed by value)by value)..This is accomplished by passing the addresses This is accomplished by passing the addresses of variables and not their values to the calling of variables and not their values to the calling stack. stack. The procedures, or functions, will access the The procedures, or functions, will access the actual addresses where the original variables actual addresses where the original variables are stored. are stored. Procedure may change the value because it does not Procedure may change the value because it does not
make a separate copy of it so any changes will be make a separate copy of it so any changes will be passed back to the calling program.passed back to the calling program.
1818
Diagram to illustrateDiagram to illustrate
Stack pointer is moved each time an address or Stack pointer is moved each time an address or actual parameter is popped onto or popped off actual parameter is popped onto or popped off the stack.the stack.
Call Proc A(X1, X2)PUSH 200PUSH X1PUSH X2
(example address of X2 – reference parameter)
(example value of X1 – value parameter)
Stack pointer 600 (X2)
3 (X1)
200
1919
600 (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)
400
200
Call Proc B(Y1,Y2,Y3)PUSH 400PUSH Y1PUSH Y2PUSH Y3
Return address for Proc A
2020
1500 (Y3)
8 (Y2)
18 (Y1)
Stack pointer 400
200
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
2121
1500 (Y3)
8 (Y2)
18 (Y1)
400
200
Stack pointer NULL Stack pointer NULL
Return from Proc A
2222
How do functions return values?How do functions return values?
Functions do not have to return their “results” Functions do not have to return their “results” by passing a parameter by reference. by passing a parameter by reference. (Variable name)(Variable name) = = (Function Name)(Function Name)(Parameters)(Parameters) (Control Name)(Control Name).Text = .Text = (Function Name)(Function Name)(Parameters)(Parameters)
Private Function …(ByVal … As …, ByVal… As …)Private Function …(ByVal … As …, ByVal… As …) DimDim (Variable name to be returned) (Variable name to be returned) As As (Data Type)(Data Type) …… Return (Variable name to be returned) Return (Variable name to be returned) End FunctionEnd Function
2323
How do functions return values How do functions return values (without use of reference parameters)?(without use of reference parameters)?
They push the value on the stack They push the value on the stack immediately before returning. immediately before returning. The calling program can then pop the The calling program can then pop the value off the stack. value off the stack.
N.B. N.B. The return address has to be popped off the The return address has to be popped off the
stack before pushing the return value onto the stack before pushing the return value onto the stack.stack.
2424
Stack pointer 6 (X2)
3 (X1)
200
Diagram to illustrateDiagram to illustrate
Stack pointer is moved each time an address or Stack pointer is moved each time an address or actual parameter is popped onto or popped off actual parameter is popped onto or popped off the stack.the stack.
Fn A (X1, X2)PUSH 200PUSH X1PUSH X2
(example values of X1 & X2 – value parameters)
2525
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)
400
200
Fn B (Y1,Y2,Y3)PUSH 400PUSH Y1PUSH Y2PUSH Y3
Return address for Fn A
2626
15 (Y3)
8 (Y2)
18 (Y1)
Stack pointer 400
200
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 Ba. Return address popped offReturn address popped off.
2727
15 (Y3)
8 (Y2)
18 (Y1)
Stack pointer 16
200
Return from Fn Bb. Return value pushed on.Return value pushed on.
(value returned by Fn B)
15 (Y3)
8 (Y2)
18 (Y1)
16
Stack pointer 200
Fn Ac. Return value popped off.Return value popped off.
2828
15 (Y3)
8 (Y2)
18 (Y1)
16
Stack pointer 32
Return from Fn Ab. Return value pushed on.Return value pushed on.
(value returned by Fn A)
15 (Y3)
8 (Y2)
18 (Y1)
16
200
Stack pointer NULL Stack pointer NULL
Return from Fn Aa. Return address popped offReturn address popped off.
2929
15 (Y3)
8 (Y2)
18 (Y1)
16
32
Stack pointer NULL Stack pointer NULL
Main Programc. Return address popped offReturn address popped off.
3030
PlenaryPlenary
Explain the passing of parameters by Explain the passing of parameters by reference and by value. reference and by value.
3131
PlenaryPlenary
ValueValue A constant or value of a variable is passed to a A constant or value of a variable is passed to a
procedure using the calling stack.procedure using the calling stack. Procedure makes a copy of it before using it so that Procedure makes a copy of it before using it so that
the original is unaltered.the original is unaltered.
ReferenceReference The address of the value is passed to the procedure The address of the value is passed to the procedure
using the calling stack.using the calling stack. Procedure may change the value because it does Procedure may change the value because it does
not make a separate copy of it so any changes will not make a separate copy of it so any changes will be passed back to the calling program.be passed back to the calling program.
top related