Functions, Parameters, and Stack Frames in Mac1 The rough outline

advertisement
Functions, Parameters, and Stack Frames in Mac1
The rough outline is as follows:
... start of main function
... set up to call the function:
... PUSH k times (for a function with k parameters)
(Note: push the address for a reference parameter,
push the value for a value parameter)
CALL n
/ call the function that starts on line n
INSP k
/ drop the parameters when return from the function
... rest of main function
HALT
n:
DESP m
/start of function, save room for m local variables
... rest of function code
INSP m
/drop the local variables right before returning
RETN
Example:
Suppose we have the following to implement in Mac1 machine language:
void compute(int x, int y, int & result, int & top)
{
int temp, n, m;
temp = x;
... rest of function code
result = temp;
}
void main(void)
{
int a, b, c, d;
a = 13;
b = 45;
... other code ...
compute(a, b, c, d);
... rest of main function code
}
The corresponding Mac1 code would be:
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
DESP 4
LOCO 13
STOD 68
LOCO 45
STOD 67
... other
... other
LODD 68
PUSH
LODD 67
PUSH
/ save room for main's 4 variables
/ put 13 into a
/ put 45 into b
code ...
code ... then set up to call our function:
/ set up for value parameter x
/ set up for value parameter y
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
LOCO 66
PUSH
LOCO 65
PUSH
CALL 20
INSP 4
... rest
... rest
HALT
DESP 3
LODL 7
STOL 2
... rest
... rest
... rest
LODL 2
PUSH
LODL 6
POPI
INSP 3
RETN
/ use the address for reference parameter result
/ use the address for reference parameter top
/
/
of
of
/
/
/
/
of
of
of
/
/
/
/
/
/
call the function (starting on line 20)
remove the 4 parameters
main function code
main function code
end of main function
save room for the 3 local variables
get the value of parameter x
... and store it in local variable temp
function code
function code
function code
load AC with value from temp
push it temporarily
load AC with reference parameter result (address)
pop answer from the stack into mem[address]
drop the 3 local variables
return to the main function (at line 16, of course)
Not shown is how to handle returning a value in a function name. That
could be done by having the calling function add a local variable, temp
let's call it, to hold the returned value. Then the function that is
called could add a reference parameter, perhaps also called temp, that
contains a pointer to the previous temp. Thus, we basically use the
reference parameter mechanism as above.
Drawing of the run-time stack:
56
57
58
59
60
61
62
63
64
65
66
67
68
|
|
|-----------|
|
13
|
|===========|
|
|
|-----------|
|
|
|-----------|
|
13
|
|-----------|
|
16
|
|-----------|
|
65
|
|-----------|
|
66
|
|-----------|
|
45
|
|-----------|
|
13
|
|===========|
|
|
|-----------|
|
|
|-----------|
|
45
|
|-----------|
|
13
|
-----------
temporary place for the answer
local variable m
local variable n
local variable temp
return address
reference parameter top
reference parameter result
value parameter y
value parameter x
d
c
b
a
Download