Page 183 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 183
160 Chapter 6 Assembly Language Subroutines
Notice that the return address is pulled into X before the local variables are allocated
and that the labels for the parameters are now used as offsets with X. In particular, the
label PSIZE used in the JMP instruction automatically allows the proper return. If we
make the assumption that the global variables V, W, and DTPD are moved to and from
the parameter list, a calling sequence would look as in Figure 6.26.
One should note that this technique generally takes more bytes of code than doing
the correction within the subroutine because each call requires an additional two bytes.
A second alternative permits the return from the subroutine to be simply an RTS
instruction without modifying the return address saved by the BSR instruction. To
account for the BRA instruction, the labels for the parameters have to be increased by 2
so that the subroutine is now written as in Figure 6.27. The trick is to put a BRA
instruction in front of the argument list to branch around it, as shown in Figure 6.28.
The typical use of passing parameters after the call assumes that all of the
arguments are either constant addresses or constant values. They are often just addresses.
The addresses are not usually modified, although the data at the addresses can be modified.
In particular, one will always be modifying the data at the addresses where output
parameters are placed. Calling sequences for this situation are particularly simple, as we
show in Figure 6.30 for our dot product example in Figure 6.29.
Because programs are usually in ROM in microprocessor applications, parameters
passed after the call must be constants or constant addresses. At a place in the program,
these addresses are constants. At another place, the addresses would be different constants.
Passing parameters in an in-line argument list is often used in FORTRAN
programs. A FORTRAN compiler passes the addresses of parameters, such as the
parameters in the example above. Like the stack method, this method is general enough
for FORTRAN, and it is easy to implement in the compiler. In assembly language
routines, this method has the appeal that it looks like an "instruction," with the opcode
replaced by the calling instruction and addressing modes replaced by the argument list,
BSR SWITCH
DC.W LO,L1,L2,L3
LO: ; program segment for case 0
BRA L4
*
LI: ; program segment for case 1
BRA L4
*
L2: ; program segment for case 2
BRA L4
*
L3: ; program segment for case 3
L4: BRA *
a. A calling sequence with an in-line argument list of jump addresses
SWITCH: CLRA
LSLD
PULX
JMP [D,X]
b. The subroutine
Figure 631. Implementation of a C or C++ Switch—Case Statement