The VZ200
BASIC to ML: For Loop
Home | Assembly Language & Machine Code | My Blog | Creating a Free Software Library | Submit Content | Tape Recorders

This page shows you how to make a Simple For Loop in Machine Language just like you would in BASIC.

Here is the For Loop in BASIC:
For x = (a + b) to a step -1
// Stuff
Next
 
In Assemble Language:
LD HC,(b-1)
LD BC,(a-1)
LD (mm),HL
ADD HL,BC
// Stuff
LD HL,(mm)
LD A,0
DEC HL
CP L
JP NZ, (START)
CP H
JP NZ, (START)
mm is two blank memory spots mm and mm+1 to store HL
HL is the same as X
START is the address of where Line 3 begins
 
The ML alternative only works for a + b is less then 65,535.
It is however much faster.
The basic framework should work for most of your For Loop needs.

Example:
To fill the screen with red squares
 
For x = 29183 to 28672 Step -1
Poke x, 191
Next X
 
8000: LD HC, 0200
8003: LD BC, 6FFF
8006: LD 7FFE, HL
8009: ADD HL, BC
800A: LD (HL), BF
800C: LD HL, 7FFF
800F: LD A, 0
8011: DEC HL
8012: CP L
8013: JP NZ, 8006
8016: CP H
8017: JP NZ, 8006

VZ200 is maintained by me Ben Pazolli in Perth, Western Australia. Please E-Mail me at bpazolli@gmail.com