Presentation is loading. Please wait.

Presentation is loading. Please wait.

The method invocation mechanism and the System Stack

Similar presentations


Presentation on theme: "The method invocation mechanism and the System Stack"— Presentation transcript:

1 The method invocation mechanism and the System Stack

2 Introduction We have now learned all material necessary to understand how the method invocation mechanism works. Here is a method invocation:

3 Introduction (cont.) In this webpage, I will show you what happens behind the scene in a method invocation Know what is going on will help you understand the concept of the parameter passing mechanism (and why there is no side effect)

4 Previously discussed Execution units:
Statement = the smallest execution unit in a Java program Method = contains multiple statements that is identified by a (method) name A statement does very little "work" A complex task requires multiple statements to complete. A method is a suitable unit of execution for programming activities A method usually performs (solves) one complex task.

5 What happens when you invoke a method
Purpose of calling (invoking) a method: We want the (invoked) method to perform (solve) a (complex) task for us (Because the method was designed to perform (solve) a (complex) task)

6 What happens when you invoke a method (cont.)
Phases of a method invocation: Give the necessary information to do its job. Example: During this phase, the parameter variables are created and initialized (given) with the proper value If you want to find the minimum of 2 numbers, then you need to know the values of the numbers

7 What happens when you invoke a method (cont.)
The method performs the task In order to perform the task, the method may need some scratch space to perform the necessary computations. In other words: The method may need to create some local variables

8 What happens when you invoke a method (cont.)
The method may return back a return value to be used at the point of invocation The mechanism to "return a value" has been discussed previously – See: As you have learned previously, these local variables are privately owned and used exclusively by the method. No other method can access them.

9 What happens when you invoke a method (cont.)
Summary: The return value is saved in a register inside the CPU:

10 What happens when you invoke a method (cont.)
When the execution returns back to the point of invocation, the computer will use the value in the return location:

11 What happens when you invoke a method (cont.)
4. The method returns back to point of invocation Notice that a method can be invoked (called) from different program locations:

12 What happens when you invoke a method (cont.)
Important fact on method invocation: In order to know where the method must return back to, the method invocation must give (= pass) the point (location) of invocation to the method

13 What happens when you invoke a method (cont.)
Example:

14 What happens when you invoke a method (cont.)
Now you know the theory of method invocation I will now show you exactly what happens with an example of method invocation

15 Preparation for an example of method invocation
I will use the following program:

16 Preparation for an example of method invocation (cont.)
Note: The program on the left are written in Java The Java program must be compiled (translated) into machine (computer) instruction first and store in RAM memory for execution The program (in RAM memory) on the left are the compiled machine (computer) instruction

17 Preparation for an example of method invocation (cont.)
The point A and point B are 2 memory addresses where the method ToolBox.min are invoked Notice that the locations have different addresses because they are different locations in RAM memory.

18 Preparation for an example of method invocation (cont.)
Attention: OK, let's rock and roll... Pay attention to the order in which the parameter variables and local variables are created and destroyed You should discover the LIFO ordering and realize that we are using a stack

19 The method invocation mechanism - explained by an example
We begin with the start of the program: The execution begins in the main method:

20 The method invocation mechanism - explained by an example (cont.)
When execution reaches double r, some space is reserve on the System Stack for the variable:

21 The method invocation mechanism - explained by an example (cont.)
When execution reaches r = ToolBox.min(1.0, 4.0);, it triggers the method invocation mechanism that we will discuss next:

22 The method invocation mechanism - explained by an example (cont.)
Phase 1: give (pass) the necessary information to the method ToolBox.min The value 1.0 and 4.0 are passed as follows: Create a parameter variable for each value on the System Stack Copy the value into the parameter variables

23 The method invocation mechanism - explained by an example (cont.)
Schematically:

24 The method invocation mechanism - explained by an example (cont.)
There is one more thing that the method invocation mechanism must do: We must make sure that the method ToolBox.min can return back to the proper invocation point !!! To enable the method ToolBox.min to return back to the proper invocation point, we must also pass (copy) the invocation location A on the System Stack !!!

25 The method invocation mechanism - explained by an example (cont.)
Schematically:

26 The method invocation mechanism - explained by an example (cont.)
Phase 1 is now complete and the execution continues with the method ToolBox.min:

27 The method invocation mechanism - explained by an example (cont.)
Phase 2: the method ToolBox.min performs its task When execution reaches double m = 0;, some space is reserve on the System Stack for the variable:

28 The method invocation mechanism - explained by an example (cont.)
When execution reaches the if-else statement, it uses the variables on the System Stack to compute the result:

29 The method invocation mechanism - explained by an example (cont.)
Phase 3: the method ToolBox.min returns back a return value When execution reaches return(m), the value in variable m is copied into a return location (which is usually a register inside the CPU):

30 The method invocation mechanism - explained by an example (cont.)
When execution reaches the end of the method, the local variable(s) m is destroyed:

31 The method invocation mechanism - explained by an example (cont.)
Note: Pay attention to the fact that the variables are destroyed in the opposite order of their creation !!! This make is possible to use a stack to reserve space for parameter and local variables.

32 The method invocation mechanism - explained by an example (cont.)
Phase 4: the method ToolBox.min returns back to the point of invocation A The program will now use the value saved return location to return to the point of invocation (A):

33 The method invocation mechanism - explained by an example (cont.)
The execution will then destroy the return location and parameter variables that it had created at the beginning of the method invocation:

34 The method invocation mechanism - explained by an example (cont.)
The method invocation mechanism is now completed !!

35 The method invocation mechanism - explained by an example (cont.)
Continued: The execution will continue will the assignment statement which will use the value stored in the return location:

36 The method invocation mechanism - explained by an example (cont.)
Conclusion: The variable r is assigned the value 1.0 which is the minimum of the 2 input values....

37 Terminology: stack frame, procedure activation record
The method invocation mechanism always constructs the following structure on the System Stack: This structure is called a "stack frame" or (older term) a "procedure activation record".

38 Terminology: stack frame, procedure activation record (cont.)
Example: (see above in Phase 2)


Download ppt "The method invocation mechanism and the System Stack"

Similar presentations


Ads by Google