Review: Translating a compound boolean expression
 

Compound boolean expressions use

  • &&   (and operator)
  • ||   (or operator)      

Example:

   if (x <= a || x >= b)              
      x = x + 1;
   else
      x = x - 1;
   

 

Compilers use the short-circuit boolean evaluation technique to translate compound boolean expressions

Short-circuit boolean evaluation technique for || expressions
 

The short-circuit boolean evaluation for the OR clause is based on the following:

  condition1 || condition2:

     If  condition1 = true 
     then
         Outcometrue   // Outcome can be computed
                          // with ONE condition

           
     If  condition1 = false 
     then
         Outcomecondition2
   

 

We can construct a program flow for a compound || boolean expression using the above short-circuit boolean evaluation technique

Flow chart of the OR condition using short-circuit boolean evaluation
 

The short circuit boolean evaluation will result in the following flow chart:

 

 

If condition C1 ≡ true, then C1 || C2 ≡ true and the program can proceed to the THEN part

But if condition C1 ≡ false, then C1 || C2 will depend on the outcome of C2.

Program flow of a compound || condition

   if ( cond1 || cond2 )   ----->        |
      statement1(s);                     V
   else				   +--------------+ TRUE  
      statement2(s);		   |  eval cond1  |-----------------+    
			           +--------------+	            |
				         | FALSE  		    |
					 V                          |
                                   +--------------+ FALSE           |
			           |  eval cond2  |------------+    |
			           +--------------+            |    |
			                 | TRUE  	       |    |
					 |<--------------------|----+
					 V (then)	       |   
				     statement1(s)             |   
				         |                     |   
			       +<--------+                     |   
			       |     (else)                    |    
			       |     statement2(s) <-----------+  
			       |	 |
			       +-------->+
				         |
	                                 V 

Programming example with compound || condition
 

Example || condition:

   int x, y, a; 

   if (x <= a || x >= b)              
      x = x + 1;
   else
      x = x - 1;
   

As before, I will omit discussing the variable definitions !

(I showed you the variable definitions to give you their data type information !)

Programming example with compound || condition

   if (x <= a || x >= b)              
      x = x + 1;
   else
      x = x - 1; 

The corresponding flow chart is:

Programming example with compound || condition

        .text
main:
        // Test if ( x <= a )
        movw    r0, #:lower16:x
        movt    r0, #:upper16:x         // r0 = addr(x)
        ldr     r0, [r0]                // r0 = x

        movw    r1, #:lower16:a
        movt    r1, #:upper16:a         // r1 = addr(a)
        ldr     r1, [r1]                // r1 = a

        cmp     r0, r1                  // x <= a ?
        ble     then                    // Branch to "then" if  x <= a

        // test if ( x >= b )
        movw    r0, #:lower16:x
        movt    r0, #:upper16:x         // r0 = addr(x)
        ldr     r0, [r0]                // r0 = x

        movw    r1, #:lower16:b
        movt    r1, #:upper16:b         // r1 = addr(b)
        ldr     r1, [r1]                // r1 = b

        cmp     r0, r1                  // x >= b ?
        blt     else                    // Branch to "else" if  x < b

then:
        // x = x + 1
        movw    r0, #:lower16:x
        movt    r0, #:upper16:x         // r0 = addr(x)

        ldr     r1, [r0]                // r1 = x
        add     r1, r1, #1              // r1 = x+1
        str     r1, [r0]                // x = x+1

        b       ifEnd                   // ** SKIP over the else part !!! ***     

else:
        // x = x - 1
        movw    r0, #:lower16:x
        movt    r0, #:upper16:x         // r0 = addr(x)

        ldr     r1, [r0]                // r1 = x
        sub     r1, r1, #1              // r1 = x-1
        str     r1, [r0]                // x = x-1

ifEnd:

        .data
x:      .4byte   5      // Try x: 10 < x < 20
a:      .4byte  10
b:      .4byte  20
        .end 

DEMO:   /home/cs255001/demo/asm/6-if/if-or.s