next up previous contents
Next: Assignments Up: Binary Operators Previous: Comparison Operators

Boolean Operators

To do a boolean and between two values of type boolean you have to design the code yourself. The general idea is that you check to see if the first value is equal to 0, if it is, the result of the and is also 0. If the value is 1, you move on to the next value to see if it is equal to 0. If it is, the result of the and is also 0. If it is equal to 1, you found that both values are equal to 1 and the result of the and is also 1.

The code to do this is:

 
 ifeq 10    		 if first value is equal to 0, jump 10 bytes (skip 3
                    instructions);

ifeq 8 if second value is equal to 0, jump 8 bytes (skip 3 instructions);

iconst_1 both values are equal to 1 so push a 1 as the result;

goto 5 jump 5 bytes to skip the next 2 instructions;

pop pop the second value if the first value was equal to 0;

iconst_0 one or both of the values were equal to 0, so push a 0.

As the code shows, when you know that the first value is 0, you also know that the result of the expression is 0, so you pop the second value from the operand stack and push a 0 as the result of the and.

Similarly, the code for a boolean or is:

 
 ifne 10    		 if first value is equal to 1, jump 10 bytes (skip 3
                    instructions);

ifne 8 if second value is equal to 1, jump 8 bytes (skip 3 instructions);

iconst_0 both values are equal to 0 so push a 0 as the result;

goto 5 jump 5 bytes to skip the next 2 instructions;

pop pop the second value if the first value was equal to 1;

iconst_1 one or both of the values were equal to 1, so push a 1.



next up previous contents
Next: Assignments Up: Binary Operators Previous: Comparison Operators



mark@bottom.xs4all.nl