/opcodes/djnz

Control Flow // DJNZ (Decrement reg. b and Jump if Not Zero)

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. if B ≠ 0:

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 8, 80 ;Set up counter LD HL, Inbuf ;Set up pointers LD DE, Outbuf LOOP: LID A, (HL) ;Get next byte from ;input buffer LD (DE), A ;Store in output buffer CP ODH ;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: Call and Return Group The following call and return group instructions are each described in this section. Simply click to jump to an instruction’s description to learn more. CALL nn – see page 281 CALL cc, nn – see page 283 RET – see page 285 RET cc – see page 286 RETI – see page 288 RETN – see page 290 RST p – see page 292

Opcodes