How Method Calls are made

Method Call Stack

The Stack Data Structure

1.      A list structure in which the elements are added and removed from the top only

2.      The last element to be added is the first one out

3.      Adding an element to the stack is called a push

4.      Removing an element is called a pop

5.      called a LIFO (last in, first out) data structure

 

 

The program counter

1.      When a program executes, the system keeps track of the next line of code to be executed

2.      The register that keeps the address of the next line of code to be executed is called the program counter

3.      The CPU just keeps executing sequentially the line of code in the program counter

4.      To make the CPU jump to a different place in RAM and execute there, the address where it should jump is put in to the program counter

 

Loading and executing the program

1.      The program loaded in static RAM

2.      This includes both the executable statements and declared variables

3.      The program counter is set to address at line 3

4.      Line 3 executed – sets up space for object in dynamic RAM

5.      The program counter is increment to execute the next line of code

6.      Line 4 executed– call maxF.determineMax() method

7.      The method call causes a lot of things to go on behind the scenes

 

The maxF.determineMax( ) method call

1.      Push return address onto the method call stack

2.      Load determineMax( ) code into RAM

3.      Set aside space for local variables

4.      The program counter is set to the first line of executable code in the method

5.      Lines 9,10,11, 12, and 13 are executed in sequence

6.      Line 14 is executed – this is a call to method maximum( )

7.      All the behind the scenes activity occurs

 

The maximum( ) method call

1.      Push return address onto the method call stack

2.      Load maximum( ) code into RAM

3.      Set aside space for local variables

4.      The arguments to the method are considered local variables

5.      The values passed from the call are copied to the arguments

6.      The program counter is set to the first line of executable code in the method

7.      Lines 19, 20, 21, 22, 23 are executed

8.      Line 24 is executed this is a return from the method maximum( )

9.       The return from a method causes behind the scenes activity

 

The return from maximum( )

1.      The return value is saved in a temporary variable

2.      The RAM set aside for the method is reclaimed by the Operating System (no longer accessible to the program)

3.      The top address is popped off the method call stack and put in the program counter

4.      Execution resumes at the address in the program counter

5.      At line 14 --the return value takes the place of the method call

6.      Lines 14 and 15 are executed

7.      Line  15 is executed

–  this is a return from the method – behind the scenes activity

 

The return from determineMaximum( )

1.      The RAM set aside for the method is reclaimed by the Operating System

2.      The top address is popped off the stack and put in the program counter

3.      Execution resumes at the address in the program counter

4.      Line 5 executes -- this is a return from the main method

5.      Control goes back to the operating system