Page 193 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 193
170 Chapter 6 Assembly Language Subroutines
Early in this book, we said that the SWI instruction can be used at the end of each
program. This instruction is really a breakpoint. You cannot turn off a microcomputer at
the end of a program, but you can return to the debug program in order to examine the
results of your program. The "halt" instruction is just a return to the debug program.
The TRAP instructions do essentially the same thing as the SWI instruction except
that the program counter is loaded from different consecutive addresses ($FFF8). These
instructions also happen to be two words long rather than one. The handlers, whose
addresses are put there, are used in lieu of subroutines for very commonly used operations
needing to save all of the registers. For example, operating system subroutines frequently
use these instructions.
* TRAP HANDLER
* saved registers
REGCC: EQU 0 ; saved condition code register
REGB: EQU 1 ; saved accumulator B
REGA: EQU 2 ; saved accumulator A
REGX: EQU 3 ; saved index register X
REGY: EQU 5 ; saved index register Y
REGPC: EQU 7 ; saved PC
JUMPVECTOR: DC.W FO,F1,F2,F3
*
TRAP: CLRA
LDX REGPC,SP
LDAB -1,X
SUBB #$30
LSLD
LDX #JUMPVECTOR
JMP [D,X]
FO: RTI ; do inner product
F1: RTI ; do quadradic
F2: RTI ; do temperature conversion
F3: RTI ; do parallel resistor
Figure 6.45. A Trap Handler
entry: MOW #TRAP,$FFF8
LDAA #1
LDAB #2
LDX #3
LDY #4
DC.W $1831 ; TRAP #$31
LDX #5
LDY #6
DC.W $1833 ; TRAP #$33
bra *
Figure 6.46. Calling a Trap Handler