Interrupt Subroutines
7-12
7.5.2
ISR with Hand-Coded Assembly
When writing an ISR by hand, it is necessary to handle the same tasks the
C/C++ compiler does. So, the following steps must be taken:
-
All registers used must be saved to the stack before modification. For this
reason, it is preferable to maintain one general purpose register to be used
as a stack pointer in your application. (The C/C++ compiler uses B15.)
-
If another C routine is called from the ISR (with an assembly branch in-
struction to the _c_func_name label) then all registers must be saved to
the stack on entry.
-
A B IRP instruction must be used to return from the routine. If this is the
NMI ISR, a B NRP must be used instead.
-
An NOP 4 is required after the last LDW in this case to ensure that B0 is
restored before returning from the interrupt.
Example 7–7. Hand-Coded Assembly ISR
* Assume Register B0–B4 & A0 are the only registers used by the
* ISR and no other functions are called
STW B0,*B15––
; store B0 to stack
STW A0,*B15––
; store A0 to stack
STW B1,*B15––
; store B1 to stack
STW B2,*B15––
; store B2 to stack
STW B3,*B15––
; store B3 to stack
STW B4,*B15––
; store B4 to stack
* Beginning of ISR code
...
* End of ISR code
LDW *++B15,B4
; restore B4
LDW *++B15,B3
; restore B3
LDW *++B15,B2
; restore B2
LDW *++B15,B1
; restore B1
LDW *++B15,A0
; restore A0
|| B
IRP
; return from interrupt
LDW *++B15,B0
; restore B0
NOP 4
; allow all multi–cycle instructions
; to complete before branch is taken