John Hm
John Hm

Reputation: 31

How can I pass the result of a method back to the stack in assembly programming?

This is the assembly code:

    ldc 5 // a
    ldc 12 // b
    bsr sumsquare
    ajs -2 // delete parameters
    ldr RR // result on the stack
    stl 1 // store in a variable
    ....
    sumsquare:
    link 1 // 1 local variable
    ldl -3 // a
    ldl -3 // a
    mul // a*a
    ldl -2 // b
    ldl -2 // b
    mul // b*b
    add // a*a + b*b
    stl 1 // x
    ldl 1 // x
    str RR // store in RR (=result register)
    unlink 1
    ret

I want to change this piece of code in such a way that the result of the method sumsquare is passed back to the stack. If I'm right, all I have to do is instead of storing it on RR, store it immediately on the stack, where the variables a and b are. So I have to use something else instead of str RR.

instruction set

These are all possible instructions on this simple stack machine

Upvotes: 2

Views: 361

Answers (1)

Erik Eidt
Erik Eidt

Reputation: 26656

The caller pushes 5 and then 12, then a return address, then transfers control to sumsquare, which uses formal parameter a at MP-3 (actual value 5) and b at MP-2 (actual value 12) on the stack behind the return address at MP-1.  Returning from callee to caller pops the return address off the stack, leaving the actual arguments, 5 and 12, on the stack, which the caller then pops off the stack.

There are at least three possible approaches:

One way would be to make the callee (the function) repurpose the first formal parameter as the function ends, by storing the return value into formal parameter a at -3, and then, have the caller pop only the 2nd actual argument off of the stack instead of both actual arguments, leaving a return value on the stack to be consumed normally (i.e. by expression evaluation, e.g. by arithmetic or assignment).  This approach would require special handling for zero parameter functions.

The next approach would be to pass a dummy parameter reserving space for the return value.  The dummy parameter would be passed first, before 5 and 12.  The callee, instead of storing the return value to formal parameter a at -3, the function would store it to the dummy at -4.  The caller would then pop both actual arguments 5, and 12 off the stack as per original (but consume the dummy now holding the return value, normally).  The advantage to this approach is that it is more regular (allowing zero parameter functions to work as all others).

Another approach would be to change the calling convention such that the callee (the function) removes its own formal parameters from the stack, leaving just the return value on the stack.  In order to do that it would have to (1) store the return value at formal parameter a at -3, and then (2) also move the return address from its current location at -1 to the location of b at -2, then (3) pop the old copy of the return address off the stack, and return to the caller (popping the new copy of the return address (aka b) off the stack, and leaving just the return value on the stack).  In this scenario, the caller does not pop the actual arguments, as that is done by the callee.  This form requires functions to do a bit of work at their end, but saves space at the call sites; this can be rationalized by observing there are typically more function calls than functions themselves (functions are called by multiple callers).  (Sometimes special stack instruction is provided to do this more directly.)  However, this form is hostile to varargs (variadic functions) (and would be unworkable if unknown whether any given function could be variadic).


This kind of shuffling is necessitated by sharing a single stack for both expression evaluation and function linkage.

In a different architecture, two independent stacks would allow leaving a return value on the expression stack, while still being able to pop linkage off to return to the caller — though two stacks would add other overhead to the hardware.

And in yet another architectural approach, there would be a register or accumulator, and that's where return values would go.

Upvotes: 2

Related Questions