Procedural Programming Paradigm Stacks and Procedures 1 Learning

  • Slides: 31
Download presentation
Procedural Programming Paradigm Stacks and Procedures 1

Procedural Programming Paradigm Stacks and Procedures 1

Learning Objectives Describe the use of parameters, local and global variables as standard programming

Learning 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. 2

When a procedure or function is called: The computer needs to know where to

When a procedure or function is called: The computer needs to know where to return to when the function or procedure is completed (return address). Further, functions and procedures may call other functions and procedures which means that not only must several return addresses be stored but they must be retrieved in the right order. 3

E. g. 3 functions are called after one another. The numbers represent the addresses

E. g. 3 functions are called after one another. The numbers represent the addresses of the instructions following the calls to functions. 4

Note: The addresses will be stored in the order 100, 150 then 250. When

Note: The addresses will be stored in the order 100, 150 then 250. When the returns take place the addresses will be needed in the order 250, 150 then 100. 5

Use a stack to store the return addresses. As last address stored is the

Use a stack to store the return addresses. As last address stored is the first address needed on returning from a function. A stack provides this Last In First Out Facility (LIFO). n A stack used for these addresses is known as the calling stack. 6

A diagram to illustrate: In the previous example, the addresses will be stored in

A diagram to illustrate: In the previous example, the addresses will be stored in the calling stack each time a function is called and will be removed from the calling stack each time a return instruction is executed. 7

Calls and Returns Stack Call Function A Push return address onto stack Stack pointer

Calls and Returns Stack Call Function A Push return address onto stack Stack pointer 100 Stack pointer 150 100 Call Function B Push return address onto stack 8

Calls and Returns Stack Call Function C Push return address onto stack Stack pointer

Calls and Returns Stack Call Function C Push return address onto stack Stack pointer Return from C Pop return address off stack Stack pointer 250 150 100 9

Calls and Returns Stack Return from B Pop return address off stack Stack pointer

Calls and Returns Stack Return from B Pop return address off stack Stack pointer Return from A Pop return address off stack 250 150 100 Stack pointer NULL 10

Parameter passing Means passing variables to a procedure when a procedure is called. n

Parameter passing Means passing variables to a procedure when a procedure is called. n n Actual Parameters (variables passed to the procedure): Formal Parameters (variables received by the procedure). 11

Passing Value Parameters Value n Means the variables are not passed back to the

Passing Value Parameters Value n Means the variables are not passed back to the calling procedure. So should not be changed. This is accomplished by passing the values in the actual parameters then storing these values in formal parameters before use so that the originals are unaltered. n Essentially local copies of the variables are made (not the original variables). 12

Diagram to illustrate Stack pointer is moved each time an address or actual parameter

Diagram to illustrate Stack pointer is moved each time an address or actual parameter is popped onto or popped off the stack. Call Proc A (X 1, X 2) PUSH 200 Stack pointer PUSH X 1 PUSH X 2 6 (X 2) 3 (X 1) 200 (example values of X 1 & X 2 – value parameters) 13

Proc A (By. Val A 1, By. Val A 2) A 2 = X

Proc A (By. Val A 1, By. Val A 2) A 2 = X 2 (POP X 2) A 1 = X 1 (POP X 1) Stack pointer Call Proc B (Y 1, Y 2, Y 3) Stack pointer PUSH 400 PUSH Y 1 PUSH Y 2 PUSH Y 3 Return address for Proc A 6 (X 2) 3 (X 1) 200 15 (Y 3) 8 (Y 2) 18 (Y 1) 400 200 14

Proc B (By. Val B 1, By. Val B 2, By. Val B 3)

Proc B (By. Val B 1, By. Val B 2, By. Val B 3) B 3 = Y 3 (POP Y 3) B 2 = Y 2 (POP Y 2) B 1 = Y 1 (POP Y 1) Stack pointer Return from Proc B Stack pointer 15 (Y 3) 8 (Y 2) 18 (Y 1) 400 200 15

15 (Y 3) 8 (Y 2) 18 (Y 1) 400 200 Return from Proc

15 (Y 3) 8 (Y 2) 18 (Y 1) 400 200 Return from Proc A Stack pointer NULL 16

Passing Reference Parameters Means the variables will be passed back and so should be

Passing Reference Parameters Means the variables will be passed back and so should be changed (otherwise they should be passed by value). This is accomplished by passing the addresses of variables and not their values to the calling stack. The procedures, or functions, will access the actual addresses where the original variables are stored. n Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program. 17

Diagram to illustrate Stack pointer is moved each time an address or actual parameter

Diagram to illustrate Stack pointer is moved each time an address or actual parameter is popped onto or popped off the stack. Call Proc A(X 1, X 2) PUSH 200 Stack pointer PUSH X 1 PUSH X 2 (example address of X 2 – reference parameter) 600 (X 2) 3 (X 1) 200 (example value of X 1 – value parameter) 18

Proc A (By. Val A 1, By. Ref A 2) A 2 = X

Proc A (By. Val A 1, By. Ref A 2) A 2 = X 2 (POP X 2) A 1 = X 1 (POP X 1) Stack pointer Call Proc B(Y 1, Y 2, Y 3) PUSH 400 PUSH Y 1 PUSH Y 2 PUSH Y 3 Return address for Proc A Stack pointer 600 (X 2) 3 (X 1) 200 1500 (Y 3) 8 (Y 2) 18 (Y 1) 400 200 19

Proc B (By. Val B 1, By. Val B 2, By. Ref B 3)

Proc B (By. Val B 1, By. Val B 2, By. Ref B 3) B 3 = Y 3 (POP Y 3) B 2 = Y 2 (POP Y 2) B 1 = Y 1 (POP Y 1) Stack pointer Return from Proc B Stack pointer 1500 (Y 3) 8 (Y 2) 18 (Y 1) 400 200 20

1500 (Y 3) 8 (Y 2) 18 (Y 1) 400 200 Return from Proc

1500 (Y 3) 8 (Y 2) 18 (Y 1) 400 200 Return from Proc A Stack pointer NULL 21

How do functions return values? Functions do not have to return their “results” by

How do functions return values? Functions do not have to return their “results” by passing a parameter by reference. n n n n (Variable name) = (Function Name)(Parameters) (Control Name). Text = (Function Name)(Parameters) Private Function …(By. Val … As …, By. Val… As …) Dim (Variable name to be returned) As (Data Type) … Return (Variable name to be returned) End Function 22

How do functions return values (without use of reference parameters)? They push the value

How do functions return values (without use of reference parameters)? They push the value on the stack immediately before returning. The calling program can then pop the value off the stack. N. B. n The return address has to be popped off the stack before pushing the return value onto the stack. 23

Diagram to illustrate Stack pointer is moved each time an address or actual parameter

Diagram to illustrate Stack pointer is moved each time an address or actual parameter is popped onto or popped off the stack. Fn A (X 1, X 2) PUSH 200 PUSH X 1 PUSH X 2 Stack pointer 6 (X 2) 3 (X 1) 200 (example values of X 1 & X 2 – value parameters) 24

Fn A (A 1, A 2) A 2 = X 2 (POP X 2)

Fn A (A 1, A 2) A 2 = X 2 (POP X 2) A 1 = X 1 (POP X 1) Stack pointer Fn B (Y 1, Y 2, Y 3) PUSH 400 PUSH Y 1 PUSH Y 2 PUSH Y 3 Return address for Fn A Stack pointer 6 (X 2) 3 (X 1) 200 15 (Y 3) 8 (Y 2) 18 (Y 1) 400 25

Fn B(B 1, B 2, B 3) B 3 = Y 3 (POP Y

Fn B(B 1, B 2, B 3) B 3 = Y 3 (POP Y 3) B 2 = Y 2 (POP Y 2) B 1 = Y 1 (POP Y 1) 15 (Y 3) 8 (Y 2) 18 (Y 1) 400 200 Stack pointer Return from Fn B a. Return address popped off Stack pointer 15 (Y 3) 8 (Y 2) 18 (Y 1) 400 26

Return from Fn B b. Return value pushed on. Stack pointer Fn A c.

Return from Fn B b. Return value pushed on. Stack pointer Fn A c. Return value popped off. Stack pointer 15 (Y 3) 8 (Y 2) 18 (Y 1) 16 200 (value returned by Fn B) 15 (Y 3) 8 (Y 2) 18 (Y 1) 16 200 27

15 (Y 3) 8 (Y 2) 18 (Y 1) 16 200 Return from Fn

15 (Y 3) 8 (Y 2) 18 (Y 1) 16 200 Return from Fn A a. Return address popped off Stack pointer NULL Return from Fn A b. Return value pushed on. Stack pointer 15 (Y 3) 8 (Y 2) 18 (Y 1) 16 32 (value returned by Fn A) 28

15 (Y 3) 8 (Y 2) 18 (Y 1) 16 32 Main Program c.

15 (Y 3) 8 (Y 2) 18 (Y 1) 16 32 Main Program c. Return address popped off Stack pointer NULL 29

Plenary Explain the passing of parameters by reference and by value. 30

Plenary Explain the passing of parameters by reference and by value. 30

Plenary Value n n A constant or value of a variable is passed to

Plenary Value n n A constant or value of a variable is passed to a procedure using the calling stack. Procedure makes a copy of it before using it so that the original is unaltered. Reference n n The address of the value is passed to the procedure using the calling stack. Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program. 31