Page 188 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 188
6,4 Calling And Returning Mechanisms 165
The extended local access of stacked local variables can be used with this technique.
Consider the body of the subroutine after the PSHC instruction and before the PULC
instruction as a program segment. The local variables are allocated just after the PSHC
instruction that saves registers and are deallocated just before the PULC instruction that
returns to the calling routine. You now look at the saved register values as stacked local
variables of an outer program segment that includes the PSHD, PSHC, PULC, and PULD
instruction. Using extended local access, you can read these registers and write into them,
too. This allows you to output data in a register, even if the data are saved.
REGCC: EQU 0
REGD: EQU 1
REGX: EQU 3
REGY: EQU 5
*
SUB: PSHY ; Save Y
PSHX ; Save X
PSHD ; Save D
PSHC ; Save CC
LDD REGX, SP ; Get to caller's X value
ADDD REGY, SP ; Add to caller's Y value
STD REGY, SP ; Result to caller's Y value
PULC ; Restore CC
PULD ; Restore D
PULX ; Restore X
PULY ; Restore Y
RTS ; Return
Figure 636. Saving and Restoring All the Registers
SUB LBRA SUBO
LBRA SUB1
LBRA SUB2
SUBO: ; perform initialization
RTS
SUB1: ; perform output
RTS
SUB2: ; perform termination
RTS
Figure 637. A Subroutine with Multiple Entry Points
As an example of this, look at the preceding example again, now using saved
registers as stacked local variables of an outer program segment. See Figure 6.36. This
idea was expanded earlier to cover the passing of arguments on the stack. The basic idea
is that you just have to know where the data are, relative to the current stack pointer SP,
in order to access the data. Thus, access to the saved registers, the caller's stacked local