Chapter
7.
Interrupts
WRITING
INTERRUPT
SUBROUTINES
74
In
general, any registers or condition bits changed
by
an
interrupt subroutine must
be
restored before returning
to the interrupted program, or errors
will
occur.
For example, suppose a program
is
interrupted just prior to the instruction:
jC
LOC
.
and the carry bit equals
1.
If
the interrupt subroutine happens to reset the carry bit before returning to the
interrupted program, the jump to
LOC
which should have occurred
will
not, causing the interrupted program
to produce erroneous results.
Like any other subroutine then, any interrupt subroutine should
save
at
least the condition bits and restore them
before performing a
RETURN operation. (The obvious and most convenient way
to
do this
is
to save the data
in
the stack, using
PUSH
and
POP
operations.)
Further, the interrupt enable system
is
automatically disabled whenever an interrupt
is
acknowledged. Except
in
special cases, therefore, an interrupt subroutine should include
an
EI
instruction somewhere to permit detection
and handling
of
future interrupts. One instruction after
an
EI
is
executed, the interrupt subroutine may itself
be
interrupted. This process may continue to any level, but
as
long
as
all
pertinent data are saved and restored,
correct program execution
will
continue automatically.
A typical interrupt subroutine, then, could appear
as
follows:
Code
PUSH
EI
POP
RET
Operand
PSW
PSW
Comment
;SAVE
CONDITION BITS
AND
ACCUMULATOR
;RE-ENABLE INTERRUPTS
;PERFORM NECESSARY ACTIONS
TO
SERVICE
;THE
INTERRUPT
;RESTORE MACHINE STATUS
;RETURN
TO
INTERRUPTED PROGRAM