The conceptually simplest mechanism for parameter transmission is called ``by value'' - this is the mechanism used by C. In this mechanism, the value you actually want to send is pushed on the stack. If you pass parameters by value, you have no way of getting a changed parameter back to the caller. So to pass a parameter by value, you need to do something like for local variables:
ldaa 3,x * get local variable into a accumulator
psha * push on stack
or something like this for global constants:
ldab const1 * get global const1 into b accumulator
pshb * push on stack
On the HC11, access to a parameter passed by value can be obtained by using indexed addressing. For reading a parameter, something like this will work:
ldaa 6,x
where the parameter is six bytes away from the x
index
register. (Remember that the x
index
register can be obtained inside a routine by using the
tsx
instruction.)
The other common technique is parameter passing by reference: instead of passing in the value of the parameter on the stack, you pass its address. This is the technique commonly associated with FORTRAN, and is available with C++.
Using by-reference parameters is a little harder on the HC11 than using by-value parameters. To get at a by-reference parameter from the stack, you have to extract the address of the data from the parameter, and then use indexed addressing (with an index of 0) to get the actual value from the address location.
First, to pass the parameter you need to get its address. If it's a global variable, that's easy. It looks like this:
ldx #global
pshx
It's a bit trickier to pass the address of a local variable, or
something that was originally passed to the caller as a value parameter.
The cases for local variable and for parameter (passed in as by-value)
are the same:
xgdx * get local variable into D
addd #7 * add offset to variable for actual memory address that you want
pshb * push D (using two instructions)
psha
subd #7 * get value back to original memory address
xgdx * get back into local variable
Something originally passed in as a by-reference parameter needs to be
handled a little differently. Something like this could work:
ldy #5,x * get address
pshy * push it
Now to access a by-reference parameter off of the stack, you need to get the pointer and dereference it. So you get something like:
ldy 8,x * get address
ldaa 0,y * fetch from there
Remember that you will have to use tsx
to get the stack
pointer into x before you can attempt to get the parameter sent in.