Consider the following example. The routine below stores the con-
tents of the A register in temporary storage location in memory
called TEMPI. No interrupts are active. Several instructions after
the store, an interrupt occurs. As the interrupts are enabled, the in-
terrupt is acted on and the interrupt processing routine is entered.
During the course of the interrupt processing, the interrupt process-
ing routine calls subroutine FINDIT. As the subroutine was being
executed when the interrupt occurred, it is reentered. During the
course of the routine, a new value is stored in TEMPI. Later, the
subroutine is executed and a return made back to the interrupt proc-
essing routine. Eventually, the interrupt processing routine finishes,
restores the environment, and executes a RETI or RETN to return
back to the interrupted point, in this case location BACKHR. At the
next instruction, the contents of the A register is reloaded, but the
value reloaded is the value stored for the second entry of FINDIT,
not the first! Reentrancy has destroyed the previous contents of
TEMPI.
REENTRY - FINDIT
NTERRUPT
LD
LD
LD
ADD
LD
(TEMP 1), A
A, (HL)
B, (IX + 30H)
A, B
(HL), A
SAVE A TEMP
GT NXT PARAM
GT 2ND VALU
STORE RESULT
ENTER HERE -* BACKHR
LD
A, (TEMP1)
RESTORE
AFTER
INTERRUPT
PROCESSING
ET
ETURN
TEMPI
DEFS
1
TMPRYSTRG
TEMP2
DEFS
1
There are many ways around reentrancy. The easiest is to never
alter common memory locations within a subroutine. The stack ac-
tions for storage will automatically save parameters and provide
almost unlimited temporary storage of variables. With stack storage,
a subroutine can be reentered as many times as practical while still
preserving temporary storage for each entry level. If it becomes
necessary to use areas of memory for storage, then reentrancy is still
possible if there is a separate user area for each level of reentrancy.
In a simple example of this, let us say there are five users that cause
five separate interrupts. The interrupt processing for each of the
interrupts calls subroutine GETCH that reads the next available
character from a keyboard input and stores it in the next buffer loca-
217
Need help?
Do you have a question about the Z80 and is the answer not in the manual?
Questions and answers