Table 15, Example usage of the djnz instruction – Zilog Z08470 User Manual

Page 70

Advertising
background image

Z80 CPU Instructions

UM008007-0715

58

Z80 CPU
User Manual

gram Counter to form a jump address. The call and return instructions allow for simple
subroutine and interrupt handling. Two special return instruction are included in the Z80
family of microprocessors. The return from interrupt instruction (RETI) and the return
from nonmaskable interrupt (RETN) are treated in the CPU as an unconditional return
identical to the op code C9h. The difference is that (RETI) can be used at the end of an
interrupt routine and all Z80 peripheral chips recognize the execution of this instruction
for proper control of nested priority interrupt handling. This instruction, coupled with the
Z80 CPU’s peripheral devices implementation, simplifies the normal return from nested
interrupt. Without this feature, the following software sequence is necessary to inform the
interrupting device that the interrupt routine is completed:

Disable Interrupt

; Prevent interrupt before routine is exited.

LD A, n

; Notify peripheral that service routine

; is complete.

OUT n, A

Enable Interrupt

Return

This seven-byte sequence can be replaced with the one-byte EI instruction and the two-
byte RETI instruction in the Z80 CPU. This instruction is important because interrupt ser-
vice time often must be minimized.

The DJNZ instruction is used to facilitate program loop control. This two-byte relative
jump instruction decrements Register B, and the jump occurs if Register B is not decre-
mented to 0. The relative displacement is expressed as a signed two’s complement num-
ber. A simple example of its use is shown in Table 15.

Table 16 lists the eight op codes for the Restart instruction, which is a single-byte call to
any of the eight addresses listed. A simple mnemonic for each of these eight calls is also
listed. The Restart instruction is useful for frequently-used routines due to its minimal
memory consumption.

Table 15. Example Usage of the DJNZ Instruction

Address

Instruction

Comments

N, N+1

LD B, 7

; Set B Register to count of 7

N+2 to N+9

(Perform a sequence of instructions)

; Loop to be performed 7 times

N+10,N+11

DJNZ – 8

; To jump from N+12 to N+2

N + 12

(Next instruction)

Advertising
This manual is related to the following products: