Crossware

Table of Contents        Previous topic       Next topic       

CROSS ASSEMBLER->Structured Assembly->FOR/ENDF

The FOR/ENDF construct will generate code to loop until an incrementing or decrementing counter matches a value.

The construct for an incrementing counter is:

     FOR.<size>  <o1> = <o2> TO <o3> [BY <o4>] DO.<distance>
     <your assembly code>
     ENDF

The construct for an decrementing counter is:

     FOR.<size>  <o1> = <o2> DOWNTO <o3> [BY <o4>] DO.<distance>
     <your assembly code>
     ENDF

<size> can be .B, .W or .L and the assembler will use this size when it generates the compare and add or subtract instructions.  If no size is specified, the assembler will assume .W.

<distance> can be .S, .W and for chips that support long branches .L.  The assembler will use this when it generates code for forward branches.

<o1> is the index of the loop.
<o2> is the starting value of the loop.
<o3> is the ending value of the loop.
<o4> is optional and the incrementing or decrementing value.

The <o1> operand cannot be immediate, (An)+ or (An). It must be compatible with the <o3> operand so that a valid compare instruction can be generated.  If the optional <o4> operand is used, it must be compatible with it so that a valid add or subtract instruction can be generated.

For incrementing loops, the loop will execute if <o2> is less than or equal to <o3>.  For decrementing loops, the loop will execute if <o2> is greater than or equal to <o3>.  <o2> and <o3> are assumed to be unsigned.

If the optional BY operand <o4> is omitted, the loop index will be incremented or decremented by 1.

As an example, the FOR/ENDF construct:

  FOR.L D0 = D1 TO D2 BY D3 DO.S
  <your assembly code>
  ENDF

will generate the following assembler code:

         MOVE.L    D1,D0
.00000    CMP.L     D2,D0
         BHI.S     .00001
         <your assembly code>
         ADD.L     D3,D0
         BRA.S     .00000
.00001

although the labels .00000 and .00001 may be different.