DOC

Functions, Parameters, and Stack Frames in Mac1

By Alicia Coleman,2014-02-13 20:37
10 views 0
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..

     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: DESP 4 / save room for main's 4 variables 1: LOCO 13

    2: STOD 68 / put 13 into a

    3: LOCO 45

    4: STOD 67 / put 45 into b

    5: ... other code ...

    6: ... other code ... then set up to call our function: 7: LODD 68 / set up for value parameter x 8: PUSH

    9: LODD 67 / set up for value parameter y 10: PUSH

    11: LOCO 66 / use the address for reference parameter result 12: PUSH

    13: LOCO 65 / use the address for reference parameter top 14: PUSH

    15: CALL 20 / call the function (starting on line 20) 16: INSP 4 / remove the 4 parameters

    17: ... rest of main function code

    18: ... rest of main function code

    19: HALT / end of main function

    20: DESP 3 / save room for the 3 local variables 21: LODL 7 / get the value of parameter x

    22: STOL 2 / ... and store it in local variable temp 23: ... rest of function code

    24: ... rest of function code

    25: ... rest of function code

    26: LODL 2 / load AC with value from temp

    27: PUSH / push it temporarily

    28: LODL 6 / load AC with reference parameter result (address) 29: POPI / pop answer from the stack into mem[address] 30: INSP 3 / drop the 3 local variables

    31: RETN / 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 | 13 | temporary place for the answer

     |===========|

     57 | | local variable m

     |-----------|

     58 | | local variable n

     |-----------|

     59 | 13 | local variable temp

     |-----------|

     60 | 16 | return address

     |-----------|

     61 | 65 | reference parameter top

     |-----------|

     62 | 66 | reference parameter result

     |-----------|

     63 | 45 | value parameter y

     |-----------|

     64 | 13 | value parameter x

     |===========|

     65 | | d

     |-----------|

     66 | | c

     |-----------|

     67 | 45 | b

     |-----------|

     68 | 13 | a

     -----------

Report this document

For any questions or suggestions please email
cust-service@docsford.com