Unit 4.4 --- IF's with logical and's

As you know, PASCAL supports the ability to have the logical
expression  in an if (or while) contain "and"'s  and  "or"s.
This  section  will  illustrate the use  of  logical  "and."
Logical  "ors"  are  left as an exercise to  the  proverbial
"interested reader."

Whenever we have a case of

if (var1>var2) then

endif

We  branch  to an appropriate label if it is not  true  that
"var1>var2".

In the case of
if cond1 and cond2 and cond3 and cond4 then


endif

We  do the same thing with conditions containing "or's."  IF
"cond1  and cond2 and cond3 and cond4" is not true, we  then
go to the label corresponding to the "endif.".

We  can view the compare and branch conditional generated by
the template as a way of going to the endif location when we
find  that it is no longer true that var1 > var2.  We go  to
the  endif location when we find that the logical expression
of  the if is not true.  In general, as soon as we find  any
of the cond's not true, we go to the "ENDIF" label.

If  cond1  is  not  true, then we know  the  entire  logical
expresion  (cond1..cond4) is not true.  As soon as  we  find
that  cond1  is  not  true, we can then go  to  the  "endif"
location.

When   the  cond1  is  not  true,  will  go  to  the  "else"
statements.   Otherwise,  the  flow  of  control  will  fall
through   to  the  code  which  will  check  the  code   for
condition2. If this is not true, we again go to  the  "else"
statement.   If  it  was true, we go to the  code  to  check
condition3, etc.

Before  discussing how we apply this to our example, let  us
discuss the rationale of the PASCAL program converted.

Again, it computes the maximum of three variables, A,B,  and
C.  However,  it  has one condition for each  of  the  three
possibilities:

*    A is the maximum

*    B is the maximum

*    C is the maximum

If A is the maximum, by definition that means  A  is  larger
than  B  and  A is larger than C.  This corresponds  to  the
condition "A>B and A>C".

If  B  is the maximum, by definition, that means B is larger
than  both A and C.  This corresponds to the condition  "B>A
and B>C".

If  C  is the maximum, by definition, that means C is larger
than  both  A and B.  This corresponds to the conditon  "C>A
and C>B"

In  the event that none of these conditions are true,  which
signifies an error state or program bug, then we set MAX  to
-1

Now to see how we convert the statements.

We sometimes refer to the condition part of each of the if's
as guards.  Thus, the three guards in the program would be:
A>=B AND A>=C
B>=A AND B>=C
C>=A and C>=B

The first guard is from lines 16 to 23.

The second guard is from 28 to 35

The third guard is converted from lines 38 to 46.

Let us look at the first guard.

A  and B are loaded into registers and compared. At line 19,
if A is < B, the opposite of A>=B, we go to I001, the second
guard.

Otherwise we check A with C and do a compare. At line 23, if
A  is  <  C, the opposite of A>=C, we again go to I001,  the
second guard.

If  we  fall through these two tests, then A is assigned  to
MAX  at lines 24 through 25. The B FI001 goes to the end  of
the if sequence.

Now we look at the second guard.

A  and B are loaded into registers and compared. (Note  that
we now do a CR 2,1 instead of a CR 1,2). At line 32, if B is
< A, the opposite of B>=A, we go to I002, the third guard.

Otherwise we check B with C and do a compare.  At  line  36,
if  B is < C, the opposite of B>=C, we again go to I002, the
third guard.

If  we  fall through these two tests, then B is assigned  to
MAX at lines 37 through 38.  The B FI001 goes to the end  of
the statement.

Now we look at the third guard.

A  and C are loaded into registers and compared.  (Note that
we  now do a CR 3,1 instead of a CR 1,2).  At line 44, if  C
is  <  A,  the opposite of C>=A, we go to I003,  the  fourth
guard.

Otherwise we check B with C and do a compare.  At  line  48,
if  C is < B, the opposite of C>=B, we again go to I003, the
fourth guard.

If  we  fall through these two tests, then C is assigned  to
MAX at lines 49 through 50.  The B FI001 goes to the end  of
the statement.

At  I003 is the fourth guard which sets MAX to -1. If  there
are no bugs, we should never get here.