DJNZ e

Operation

B <- B-1
If B = 0, continue
If B != 0, PC <- PC + e

Mnemonic

DJNZ

Operands

e

b7b6b5b4b3b2b1b0
0 0 0 1 0 0 0 0$10
e-2

Description

This instruction is similar to the conditional jump instructions except that a register value is used to determine branching. Register B is decremented, and if a nonzero value remains, the value of displacement e is added to the Program Counter (PC). The next instruction is fetched from the location designated by the new contents of the PC. The jump is measured from the address of the instruction op code and contains a range of -126 to +129 bytes. The assembler automatically adjusts for the twice incremented PC.

If the result of decrementing leaves B with a zero value, the next instruction executed is taken from the location following this instruction.

Condition Bits Affected

None

Example

A typical software routine is used to demonstrate the use of the DJNZ instruction. This routine moves a line from an input buffer (INBUF) to an output buffer (OUTBUF). It moves the bytes until it finds a CR, or until it has moved 80 bytes, whichever occurs first.

	LD	B, 80		;Set up counter
	LD	HL, INBUF	;Set up pointers
	LD	DE, OUTBUF

LOOP:	LD	A, (HL)		;Get next byte from input buffer
	LD	(DE), A		;Store in output buffer
	CP	$0D		;Is it a CR?
	JR	Z, DONE		;Yes finished
	INC	HL		;Increment pointers
	INC	DE
	DJNZ	LOOP		;Loop back if 80 bytes have not been moved.
DONE: