Passing paramenters on the stack

Posted by oxinabox.ucc.asn.au on Stack Overflow See other posts from Stack Overflow or by oxinabox.ucc.asn.au
Published on 2010-04-09T05:28:21Z Indexed on 2010/04/09 5:33 UTC
Read the original article Hit count: 515

Filed under:
|
|
|
|

When you pass parameters to a function on the cpu stack,

You put the parameters on then JSR puts the return address on the stack. So than means in your function you must take the top item of the stack (the return address) before you can take the others off)

eg is the following the correct way to go about it:

...
|Let’s do some addition with a function,
MOVE.L #4, -(SP)
MOVE.L #5, -(SP)
JSR add
    |the result of the addition (4+5) is in D0 (9)
...

add: 
    MOVE.L   (SP)+,  A1     |store the return address
            |in a register
    MOVE.L  D0, -(SP)   |get 1st parameter, put in D0
    MOVE.L  D2, -(SP)   |get 2nd parameter, put in D0
    ADD.L      D2, D0   |add them, 
            |storing the result in D0
    MOVE.L  A1, -(SP)   |put the address back on the 
            |Stack
    RTS     |return

© Stack Overflow or respective owner

Related posts about asm

Related posts about assembly