There are four common boolean operators: AND,OR,XOR and NOT. A truth table shows the result of each operation for each possible value of its operands.
3.2.1 The AND operation
The result of the AND of two bits is only 1 if both bits are 1, else the result is 0 as the truth table in Table 3.1 shows.
Processors support these operations as instructions that act indepen- dently on all the bits of data in parallel. For example, if the contents ofAL andBL areANDed together, the basicAND operation is applied to each of the 8 pairs of corresponding bits in the two registers as Figure 3.2 shows. Below is a code example:
1 mov ax, 0C123H
2 and ax, 82F6H ; ax = 8022H
3.2.2 The OR operation
The inclusive OR of 2 bits is 0 only if both bits are 0, else the result is 1 as the truth table in Table 3.2 shows. Below is a code example:
1 mov ax, 0C123H
X Y X ORY
0 0 0
0 1 1
1 0 1
1 1 1
Table 3.2: The OR operation X Y X XORY
0 0 0
0 1 1
1 0 1
1 1 0
Table 3.3: The XOR operation 3.2.3 The XOR operation
The exclusive OR of 2 bits is 0 only if and only if both bits are equal, else the result is 1 as the truth table in Table 3.3 shows. Below is a code example:
1 mov ax, 0C123H
2 xor ax, 0E831H ; ax = 2912H
3.2.4 The NOT operation
The NOT operation is a unary operation (i.e., it acts on one operand, not two like binary operations such as AND). The NOT of a bit is the opposite value of the bit as the truth table in Table 3.4 shows. Below is a code example:
1 mov ax, 0C123H
2 not ax ; ax = 3EDCH
Note that theNOT finds the one’s complement. Unlike the other bitwise operations, the NOTinstruction does not change any of the bits in theFLAGS register.
3.2.5 The TEST instruction
The TEST instruction performs an AND operation, but does not store the result. It only sets the FLAGS register based on what the result would be (much like how theCMPinstruction performs a subtraction but only sets FLAGS). For example, if the result would be zero, ZFwould be set.
X NOT X
0 1
1 0
Table 3.4: The NOT operation
Turn on biti OR the number with 2i (which is the binary number with just bit i on)
Turn off biti AND the number with the binary number with only bit i off. This operand is often called amask
Complement biti XOR the number with 2i
Table 3.5: Uses of boolean operations 3.2.6 Uses of boolean operations
Boolean operations are very useful for manipulating individual bits of data without modifying the other bits. Table 3.5 shows three common uses of these operations. Below is some example code, implementing these ideas.
1 mov ax, 0C123H
2 or ax, 8 ; turn on bit 3, ax = C12BH
3 and ax, 0FFDFH ; turn off bit 5, ax = C10BH
4 xor ax, 8000H ; invert bit 31, ax = 410BH
5 or ax, 0F00H ; turn on nibble, ax = 4F0BH
6 and ax, 0FFF0H ; turn off nibble, ax = 4F00H
7 xor ax, 0F00FH ; invert nibbles, ax = BF0FH
8 xor ax, 0FFFFH ; 1’s complement, ax = 40F0H
TheAND operation can also be used to find the remainder of a division by a power of two. To find the remainder of a division by 2i, AND the
number with a mask equal to 2i−1. This mask will contain ones from bit 0 up to biti−1. It is just these bits that contain the remainder. The result of theAND will keep these bits and zero out the others. Next is a snippet of code that finds the quotient and remainder of the division of 100 by 16.
1 mov eax, 100 ; 100 = 64H
2 mov ebx, 0000000FH ; mask = 16 - 1 = 15 or F
3 and ebx, eax ; ebx = remainder = 4
4 shr eax, 4 ; eax = quotient of eax/2^4 = 6
Using theCLregister it is possible to modify arbitrary bits of data. Next is an example that sets (turns on) an arbitrary bit inEAX. The number of the bit to set is stored inBH.
1 mov cl, bh ; first build the number to OR with
2 mov ebx, 1
3 shl ebx, cl ; shift left cl times
4 or eax, ebx ; turn on bit
Turning a bit off is just a little harder.
1 mov cl, bh ; first build the number to AND with
2 mov ebx, 1
3 shl ebx, cl ; shift left cl times
4 not ebx ; invert bits
5 and eax, ebx ; turn off bit
Code to complement an arbitrary bit is left as an exercise for the reader. It is not uncommon to see the following puzzling instruction in a 80x86 program:
xor eax, eax ; eax = 0
A numberXOR’ed with itself always results in zero. This instruction is used because its machine code is smaller than the corresponding MOVinstruction.