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:

DO
<expr>          { Emit(SUBQ #1,D0);
                  L = NewLabel;
                  PostLabel(L);
                  Emit(MOVE D0,-(SP) }
<block>
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;
begin
   Match('d');
   L := NewLabel;
   Expression;
   EmitLn('SUBQ #1,D0');
   PostLabel(L);
   EmitLn('MOVE D0,-(SP)');
   Block;
   EmitLn('MOVE (SP)+,D0');
   EmitLn('DBRA D0,' + L);
end;

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