Page 179 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 179
! 56 Chapter 6 Assembly Language Subroutines
Putting the additional offset of NBYTES+2 in the subroutine, which is written only
once, makes the calling sequence, which may be written many times, more
straightforward. However, keeping the EQU directives with the subroutine as shown will
force 2-byte offsets for parameter accesses in all of the calling sequences placed before the
subroutine. For this reason, the EQU tables for subroutines might be placed at the
beginning of a program to improve the static efficiency of the calling sequences. One
could also force 1-byte offsets by using "<" before the expressions that access the
parameters, as in LDAA <LOCW+NBYTES+2 , SP. This, however, would still not get
the 5-bit offset for those local variables that would be accessed with offset expressions in
the range from zero to fifteen.
The reader should recognize that in this method of passing parameters, the calling
sequence is just another instance of a balanced program segment, with local variables that
are the parameters to the subroutine. Variables are copied from the calling routine to the
parameter locations and back so that the subroutine will have a precise place to find
them. Compare this to the earlier example where parameters were passed by global
variables, and review the discussion after that example.
There is another way to think of this technique, which some students have found to
be more concrete. You can think of "holes in the stack." To pass an argument out of the
subroutine, such as LOG DP, you create room for it in the calling routine. The instruction
LEAS -PSIZE, SP creates a hole for this output parameter, among other things, and
the subroutine puts some data in this hole. Conversely, the input parameters LOCV and
LOCW are used up in the subroutine and leave holes after the subroutine is completed.
These holes are erased by the instruction LEAS PS IZE, SP.
Figure 6.24. Parameters Passed after the Call
Some readers may appreciate the generality of the idea of parameters as local
variables of the calling sequence, whereas others may prefer the more concrete technique
of providing and removing holes for input and output parameters. They are the same.
The reason that the stack mode of passing arguments is recommended is that it is
very general. Because registers are quite limited in number and are useful for other
functions, it is hard to pass many parameters through registers. You can pass as many
arguments to or from a subroutine as you will ever need using the stack. Compilers
often use this technique. It is easier to use a completely general method in a compiler,