5.4. The .for directive

With the .for directive you can generate loops as in modern programming languages. The .for directive takes an init expression list, a boolean expression, and an iteration list separated by a semicolon. The last two arguments and the body are executed as long as the boolean expression evaluates to true.

// Prints the numbers from 0 to 9
.for(var i=0;i<10;i++)  .print "Number " + i  

// Make data for a sine wave
.for(var i=0;i<256;i++) .byte round(127.5+127.5*sin(toRadians(360*i/256)))

Since argument 1 and 3 are lists, you can leave them out, or you can write several expressions separated by comma:

// Print the numbers from 0 to 9
.var i=0
.for (;i<10;) {
    .print i
    .eval i++

// Sum the numbers from 0 to 9 and print the sum at each step
.for(var i=0, var sum=0;i<10;sum=sum+i,i++)  
    .print “The sum at step “ + I “ is “ + sum

With the for loop you can quickly generate tables and unroll loops. You can, for example, do a classic ‘blitter fill’ routine like this:

.var blitterBuffer=$3000
.var charset=$3800
.for (x=0;x<16;x++) {
    .for(var y=0;y<128;y++) {
        if (var y=0)  lda blitterBuffer+x*128+y
        else          eor blitterBuffer+x*128+y
        sta charset+x*128+y