5.8. The DO statement

All this made me wish for a simpler version of the FOR loop. The reason for all the code above is the need to have the loop counter accessible as a variable within the loop. If all we need is a counting loop to make us go through something a specified number of times, but don't need access to the counter itself, there is a much easier solution. The 68000 has a decrement and branch nonzero instruction built in which is ideal for counting. For good measure, let's add this construct, too. This will be the last of our loop structures.

The syntax and its translation is:

<expr>          { Emit(SUBQ #1,D0);
                  L = NewLabel;
                  Emit(MOVE D0,-(SP) }
ENDDO           { Emit(MOVE (SP)+,D0;
                  Emit(DBRA D0,L) }

That's quite a bit simpler! The loop will execute <expr> times. Here's the code:

{ Parse and Translate a DO Statement }
procedure Dodo;
var L: string;
   L := NewLabel;
   EmitLn('SUBQ #1,D0');
   EmitLn('MOVE D0,-(SP)');
   EmitLn('MOVE (SP)+,D0');
   EmitLn('DBRA D0,' + L);

I think you'll have to agree, that's a whole lot simpler than the classical FOR. Still, each construct has its place.