# `djnz`

[Z80 Documentation](https://asm-editor.specy.app/documentation/z80.md) › [Instructions](https://asm-editor.specy.app/documentation/z80/instruction.md)

Web page: https://asm-editor.specy.app/documentation/z80/instruction/djnz

**Decrement B and jump if not zero**

Category: Control flow

Decrements B and jumps to the target when B is not zero.

2 bytes · 13/8 cycles · 13 cycles when it jumps, 8 when it does not

Used in: [Loops and djnz](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/loops-and-djnz.md) (Z80 assembly), [Arrays, strings and ix](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/arrays-strings-and-ix.md) (Z80 assembly), [The stack, push and pop](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/the-stack-push-and-pop.md) (Z80 assembly), [call, ret and passing values](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/call-ret-and-passing-values.md) (Z80 assembly), [Fill an array with the numbers from 1 to 10](https://asm-editor.specy.app/learn/courses/z80/examples/counting-loop.md) (Z80 assembly), [Sum of an array](https://asm-editor.specy.app/learn/courses/z80/examples/sum-of-an-array.md) (Z80 assembly), [The largest element](https://asm-editor.specy.app/learn/courses/z80/examples/max-of-an-array.md) (Z80 assembly), [Reverse a string in place](https://asm-editor.specy.app/learn/courses/z80/examples/reverse-a-string.md) (Z80 assembly), [Multiply and divide, with the remainder](https://asm-editor.specy.app/learn/courses/z80/examples/multiply-and-divide.md) (Z80 assembly), [Even or odd, count the set bits, multiply by shifting](https://asm-editor.specy.app/learn/courses/z80/examples/bit-tricks.md) (Z80 assembly)

## Flags

| Flag | Effect |
| --- | --- |
| S | not changed |
| Z | not changed |
| H | not changed |
| P/V | not changed |
| N | not changed |
| C | not changed |

## Forms

- `djnz offset` · The B register is decremented, and if not zero, the signed value `offset` is added to PC. The jump is measured from the start of the instruction opcode.

## Operands

- [`offset`](https://asm-editor.specy.app/documentation/z80/registers/operand-offset.md): A relative-jump target, written as a label or address. The assembler calculates the displacement, `-128` to `127`, and reports an error if the target is out of range.

## Example

```z80
        .org 0x8000
        ld b, 3
loop:   djnz loop
        halt
```
