Z80 For Loop

Published 2017-07-05 08:10:27

WHILE - FOR:

We usually use B as counter. The main trick here is that DJNZ decrements the counter in B and then checks, so that you don’t need to do two operations.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
LD C, 5
LD A, 0
LD B, 100

.NEXT2:
push BC
LD B, 10
  
.NEXT:
ADD A, C
DJNZ .NEXT
pop BC
      
DJNZ .NEXT2