Page 172 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 172
6.2 Passing Parameters 149
Using the stack marker of the second example, the following instructions will
access the variable.
LDX 0, SP ; Get to segment B
LDX 0, X ; Get to segment A
LDAA SA, X ; Access local variable SA
The extended local access mechanism appears to be a bit simpler for smaller assembly
language programs because it takes fewer instructions or directives. The stack marker
mechanism seems to be used in some compilers because the compiler program has less
to "remember" using this approach than using the other, where the compiler has to keep
track of the sizes of each allocation of stacked local variables, particularly if a subroutine
is called by many program segments that have different numbers of local variables. It is
not unreasonable to expect a large program to have 20 levels of nesting. Because the
stack marker to the next outer segment is always on top of the stack, the compiler does
not have to remember where it is. In fact, the labels for stack markers are not really
necessary for access at all, their only real use being for allocation and deallocation of the
local variables. With two good mechanisms, you will find it easy to use one of them to
handle nested program segments to many levels.
This section introduced the idea of a local variable and the techniques for storing
local variables on the 6812. We demonstrated that local variables should not be stored in
global variables, whether using the same name (TEMP) for each or giving each variable a
unique name. Local variables can be stored on the stack in any program segment. They
are especially easy to use in the 6812, because the LEAS instructions are able to allocate
and deallocate them, the index addressing mode using the stack pointer is useful in
accessing them, and the EQU or DS directives are very useful in binding the symbolic
names to offsets to the stack pointer. These techniques can be used within subroutines,
as we discuss in the remainder of this chapter. They can also be used with program
segments that are within macros or those that are written as part of a larger program. The
nested program segments can be readily handled too, using either the extended local
access or stack marker technique to access local variables of outer program segments.
6.2 Passing Parameters
We now examine how parameters are passed between the subroutine and the calling
routine. We do this because an assembly-language programmer will have frequent
occasions to use subroutines written by others. These subroutines may come from other
programmers that are part of a large programming project, or they may be subroutines
that are taken from already documented software, such as assembly-language subroutines
from a C support package. They may also come from a collection of subroutines
supplied by the manufacturer in a user's library. In any case, it is necessary to understand
the different ways in which parameters are passed to subroutines, if only to be able to use
the subroutines correctly in your own programs or perhaps modify them for your own
specific applications.