DJNZ

Instruction: Decrement value and jump to address if not zero.

DJNZ Value, < # > Address


Result: Value-1 is written to Value.

  • Value (d-field) is the register to decrement and test.
  • Address (s-field) is the register or a 9-bit literal whose value is the address to jump to when the decremented Value is not zero.

Opcode Table:

–INSTR–  ZCRI –CON–    –DEST–        –SRC–

Z Result

C Result

Result

Clocks

 111001    001i    1111    ddddddddd    sssssssss

Result = 0

Unsigned Borrow

Written

4 or 8

Concise Truth Table:

In

Out

Destination

Source

Z

C

Effects

Destination

Z

C

$0000_0002; 2

$----_----; -

-

-

wz wc

$0000_0001; 1

0

0

$0000_0001; 1

$----_----; -

-

-

wz wc

$0000_0000; 0

1

0

$0000_0000; 0

$----_----; -

-

-

wz wc

$FFFF_FFFF; -1

0

1

Explanation

DJNZ decrements the Value register and jumps to Address if the result is not zero.

When the WZ effect is specified, the Z flag is set (1) if the decremented Value is zero. When the WC effect is specified, the C flag is set (1) if the decrement results in an unsigned borrow (32-bit overflow). The decremented result is written to Value unless the NR effect is specified.

DJNZ requires a different amount of clock cycles depending on whether or not it has to jump. If it must jump it takes 4 clock cycles, if no jump occurs it takes 8 clock cycles. Since loops utilizing DJNZ need to be fast, it is optimized in this way for speed.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.