Unit 5.1 -- Simple Do Loops

PROG

Sum the numbers from 1 to N, leaving result in SUM

PED

learn how to write a do loop





FOR var:=START TO END DO
   body;

     |
     V

A FOR Loop can be converted to a WHILE Loop using a register
as the counter.
        |
        V
var:=start;
WHILE (var <= END) DO BEGIN
  body
  var := var + 1
END WHILE

       |
       V

A  WHILE  Loop  can then be converted to ASSEMBLY.   (For  a
general conversion process see the Concepts Section  on  the
next page.)


CONCEPTS


WHILE (condition) DO
  statements
END

converts to

LOOPnn EQU *
      branch if condition not true to LOOPnnE
      statements
      B LOOP1
LOOPnnE EQU *

SF

none
SUM := 0
FOR I :=1 to N DO
  SUM := SUM + I;

     |
     |
     V
SUM:=0
I:=1;
WHILE (I<=N) do begin
  SUM:=SUM+I
  I:=I+1
END


8.html
                           Section 5

                            Do Loops

                            Unit 5.1

In this section, we learn about the template to translate  a
WHILE statement into ASSEMBLER.

Keep  in  mind that any "FOR" loop can be easily  translated
into "WHILE" statements as follows.

SUM:=0;
FOR var:=1 to N DO
  statements

gets translated to

SUM:=0;
var:=1
WHILE (var <=n) DO BEGIN
  statements
  var:=var+1;
END;

The  "var <=n" stops var when it equals N.  The "var:=var+1"
does the incrementing that is implied by the "FOR"

Now,  we  can learn the template for the "WHILE"  statement.
Using  the  above template and knowing how to convert  WHILE
statements  to  ASSEMBLER gives us  a  two-step  process  to
convert FOR's to ASSEMBLER.  It is very similar to that  for
an  "IF."  If the condition isn't true, you go to the end of
the  loop.   If the condition is true, then we fall  through
and  execute one more time through the loop.  At the  bottom
of  the  code for the loop, the unconditional branch returns
to the program.

Note  that  like the condition for an IF, we branch  on  the
opposite case.  We keep on going through the loop,  as  long
as the condition is true.

Looking at our example,

This is equivalent to:

SUM:=0
I:=1;
WHILE (I<=N) do begin
  SUM:=SUM+I
  I:=I+1
END

Now we learn how to translate a while.

When we encounter a "WHILE (condition)" a branch is made  to
a  label  at the end of the loop when the condition  is  not
true.  This gets us out of the loop, when it is time to end.
Then  we translate the statements inside the loop.  Finally,
we  branch  back to the beginning of the loop to  check  the
condition  and if appropriate, go through the  loop  another
time.

Now, we see the translation of this WHILE in the example for
this section.  Remember we are looking at the result of  the
translation on page 91.

lines  9  through 10 compute "SUM:=0"  Note the use  of  the
shorthand  SR 1,1 to get zero in register one.  We  use  the
fact  that  anything minus itself is always zero,  including
whatever random bit pattern might be in register one.

We then initialize I to 1 in lines 11 to 12.

At  line  13 the label, "LOOP1 EQU *" is the place where  we
return to at the end of the do loop to execute another time.

We check I against M in lines 14 through 17.

In lines 19 through 20, I is added to SUM.

In lines 22 to 24, we add one to I.

And  finally,  in  line  25, the "B LOOP1"  returns  to  the
beginning of the loop.

At  line  25, "LOOP2 EQU *", provides a place to  branch  to
when I becomes greater than