Mnemonics

DJNZ

Jump Group
Expansion:
Decrement and Jump if Not Zero
Usage:
djnz {label|address|??}

Decreases register B by 1 and jump if Zero (Z) flag is not set. This is a relative jump (see JR ) so there is a restriction of -128 to +127 byte range for address of the jump.

Note that DJNZ will only work with register B but is a lot more efficent than using two instructions such as dec b than jr {label|address|??}.

Z80.CPU instructions
Instruction Description Opcode
DJNZ {label|address|??} If the Zero (Z) flag is not set then decrement and jump relative 10 {??}

Example

In this example, we loop through two loops counting down from 5 to zero. The inside loop count is reset each time.

ld bc, $0505 ;load b with 5 and c with 5 outer_loop: ld c, 5 ;reset c for the inner loop inner_loop: dec c ;decrement c jr nz, inner_loop ;jump to inner_loop if c is not zero djnz outer_loop ;decrement b and jump to outer_loop if b is not zero

See also