3.3 Invoke from a subroutine

Can we invoke a subroutine from a subroutine? Absolutely! In fact, none of the mechanism discussed so far has to change.


Listing 3: Invoking from a subroutine.
1define sub s1 
2  xx+1 
3end define sub 
4define sub s2 
5  invoke s1 
6  x x+10 
7end define sub 
8x
9invoke s2 
10invoke s1

The trace of this algorithm is as follows (the first column identifies a row in the trace for easier reference):







trace
A
B C D

explanation







1
line#
x







2
pre
?







3 8
0







4 9 retline#

At this point, column C is the leftmost available column.







5
10







6 5 retline#

At this point, column C is already in use. As a result, when “s1” is invoked from “s2”, column D is the leftmost available column.







7
6







8 2
1







9 3 retline#

At the end of a subroutine, the rightmost allocated “retline#” is used to indicate where to continue execution. In this case, we continue execution on line 6.







10 6
11







11 7 retline#

Because column D is already deallocated, column C becomes the rightmost “retline#” column. As a result, we continue execution on line 10 in the next row of the trace after column C is deallocated.







12 10 retline#

At this point, column C is already deallocated (by the previous row of the trace). As a result, it is available again to store the “retline#” when “s1” is invoked.







13
post







14 2
12







15 3 retline#







16
post