Note, this is intended for a beginner (as I was at the time when I wrote this) who thinks in terms of high level languages.
Firstly, it's best to type down what exactly we'd like to write in a higher level language:
int i;
for ( i = 0 ; i < 10 ; i++ ) {
/* do something */
}
As per C-like languages, iteration statement is defined as follows:
for ( <statement> ; <expression> ; <expression> )
<statement...>
For convenience I give a name to expressions in the for statement as such:
for ( loop_init ; loop_cond ; loop_next )
loop_body
loop_end
In for-loop, the order of execution in expressions as follows:
for ( [1] ; [2] ; [4] )
[3]
[5]
In assembly we'll first place our labels in their order of execution acording to for-loop:
.loop_init // [1]
.loop_cond // [2]
.loop_body // [3]
.loop_next // [4]
.loop_end // [5]
Using the above template, we can now write a for-loop as follows (for convenience I am using 8-bit register b for the index variable):
.loop_init
ld b, #0 ;; i = 0
.loop_cond
ld a, 10 ;; i != 10
cp b
jp z, loop_exit
.loop_body
;; do something
.loop_next
inc b ;; i++
jp loop_cond
.loop_end
For clarity is important, there is no harm in keeping unused labels in there. But if you like, they can be omitted for brevity:
ld b, #0 ;; i = 0
.loop_cond
ld a, 10 ;; i != 10
cp b
jp z, loop_exit
;; do something
inc b ;; i++
jp loop_cond
.loop_exit
👍