LOGICAL INSTRUCTION
Bitwise Logical instructions are the most primitive operations needed by every computer architecture. At a minimum, an architecture could provide a NAND operations, since all other logical functions can be derived from NAND operations.
These logical operations are semantically different to what is known as in most of high level programming language.
The difference lies down at the fact that bitwise logical operations are performed at bit-by-bit basis.
All logic instructions, except TEST, modify the Destination operand. The TEST instruction does not modify any of its operands; however it affects the flags similar to the AND instruction.
All logical instructions, except NOT, affect the status flags
Effects on Status Flag
Since logical instructions operate on a bit-by-bit basis, no carry or overflow is generated.
 | Except NOT, all logical instructions clear carry flag (CF) and overflow flag (OF).
|
 | AF is undefined
|
 | Remaining three flags record useful information: Zero flag (ZF), Sign flag (SF), Parity flag (PF).
|
Carry Flag | Overflow Flag | Zero Flag | Sign Flag | Parity Flag | Auxiliary Flag
|
0 | 0 | Modified | Modified | Modified | Undefined
|
The Truth tables
Bit1 | Bit2 | Bit1 AND Bit2 | Bit1 TEST Bit2 | Bit1 OR Bit2 | Bit1 XOR Bit2
|
0 | 0 | 0 | 0 | 0 | 0
|
0 | 1 | 0 | 0 | 1 | 1
|
1 | 0 | 0 | 0 | 1 | 1
|
1 | 1 | 1 | 1 | 1 | 0
|
 Example: AND BL, 0F0h instruction |
mov | BL, 01010111b
| and | BL, 0F0h
| |
| Result in BL is: |
| |
The Usage
The main usage of bitwise logical instructions is:
 | to set
|
 | to clear
|
 | to invert
|
 | to isolate
|
some selected bits in the Destination operand.
To do this, a Source bit pattern known as a mask is constructed. The mask bits are chosen so that the selected bits are modified in the desired manner when an instruction of the form:
LOGIC_INSTRUCTION Destination , Mask
is executed. The Mask bits are chosen based on the following properties of AND, OR, and XOR :
If X represents a bit (either 0 or 1) then:
AND | OR | XOR
|
X AND 0 = 0 | X OR 0 = X | X XOR 0 = X
|
X AND 1 = X | X OR 1 = 1 | X XOR 1 = X'
|
Thus,
-
The AND instruction can be used to CLEAR specific Destination bits while preserving the others. A zero mask bit clears the corresponding Destination bit; a one mask bit preserves the corresponding destination bit.
-
The OR instruction can be used to SET specific Destination bits while preserving the others. A one mask bit sets the corresponding Destination bit; a zero mask bit preserves the corresponding Destination bit.
-
The XOR instruction can be used to INVERT specific Destination bits while preserving the others. A one mask bit inverts the corresponding Destination bit; a zero mask bit preserves the corresponding Destination bit.
 Example: Clearing bit 2, 4, 6 and 7 of destination using AND operation |
Destination | a(7) | a(6) | a(5) | a(4) | a(3) | a(2) | a(1) | a(0)
| Mask | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 1
| AND | 0 | 0 | a(5) | 0 | a(3) | 0 | a(1) | a(0)
|
|
 Example: Setting bit 7, 6, 5, 3 and 0 of destination using OR operation |
Destination | a(7) | a(6) | a(5) | a(4) | a(3) | a(2) | a(1) | a(0)
| Mask | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 1
| OR | 1 | 1 | 1 | a(4) | 1 | a(2) | a(1) | 1
| |
 Example: Toggling bit 7, 2, and 0 of destination using OR operation |
Destination | a(7) | a(6) | a(5) | a(4) | a(3) | a(2) | a(1) | a(0)
| Mask | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 1
| XOR | ~a(7) | a(6) | a(5) | a(4) | a(3) | ~a(2) | a(1) | ~a(0)
|
|
 Example: To clear the 4 low-order bits of BL while leaving the 4 high-order bits unchanged. |
AND BL , 11110000B
|
 Example: To Set bits 15 and 14 of AX while leaving other bits unchanged. |
AX , 1100000000000000B
|
Example: To Inverts bits 1 and 3 of CL while preserving the others. |
The instruction XOR CL , 00001010B
|
Changing a letter to its opposite case
For any alphabetic letter, bit 5 of its ASCII code is 1; but for the corresponding uppercase letter bit 5 is 0. The remaining bits are similar:
Letter | ASCII code | Letter | ASCII code
|
'a' |
| 'A' |
|
'b' |
| 'B' |
|
'c' |
| 'C' |
|
... |
| ... |
|
'z' |
| 'Z' |
|
Thus a lowercase alphabetic letter can also be converted to uppercase by clearing bit 5 of its ASCII code. This can be done by using an AND instruction with the mask 11011111B or 0DFh.
 Example: To convert letter |
| mov | | DL , 'j'
| | and | | DL , 11011111B
| |
An uppercase alphabetic letter can also be converted to lowercase by setting bit 5 of its ASCII code. This can be done by using an OR instruction with the mask 00100000B or 20H.
 Example: To convert letter |
|
To convert a lowercase or uppercase letter to its opposite case we need only invert bit 5 of its ASCII code. This can be done by using an XOR instruction with the mask 00100000B.
Converting an ASCII digit to a Decimal digit and vice versa
For any ASCII digits, bit 4 and 5 of its ASCII code are 11; but for the corresponding decimal digit bit 4 and 5 are 00. The remaining bits are similar:
ASCII digit | ASCII code | Decimal digit | Binary code
|
'0' |
| 0 |
|
'1' |
| 1 |
|
'2' |
| 2 |
|
x |
| x |
|
'9' |
| 9 |
|
Thus another way of converting an ASCII digit to the corresponding Decimal digit is to use the AND instruction with the mask 00001111B (i.e. 0FH) or with the mask 11001111B (i.e. 0CFH) to clear bits 5 and 6 of the ASCII digit.
 Example: To convert ASCII digit 3 into decimal number 3 |
|
Similarly, another way of converting a Decimal digit to the corresponding ASCII digit is to use the OR instruction with the mask 00110000B (i.e. 30H) to set bits 5 and 6 of the Decimal digit.
 Example: To convert decimal number 6 into ASCII digit 6 |
|
Examining selected bits in the Destination Operand
The Logic Instructions can be used to examine the status of selected bits in the destination operand.
Example: To check whether bit 2 of AL is set or clear: |
test AL,00000100b
jz IS_CLEAR
. . . ; action if bit 2 is set
jmp DONE
IS_CLEAR: . . . ; action if bit 2 is clear
. . .
DONE:
|
Example: To check whether bits 0, 2, 4 and 5 of DL are all clear: |
test DL, 00110101B
jz BITS_CLEAR
. . . ; action if any of bits 0, 2, 4, and 5 is set
jmp DONE
BITS_CLEAR: ... ; action if each of bits 0, 2, 4, and 5 is clear
DONE:
|
Example: To check if ANY of bits 0, 2, 4 and 5 of DL is clear: |
push DX
or DL, 11001010B
cmp DL, 11111111B
pop DX
jne ATLEASTONE_CLEAR
. . .
jmp DONE
ATLEASTONE_CLEAR: . . .
DONE:
|
Determine whether a general-purpose register is equal to zero
Clearing a general-purpose register operand or a memory operand to zero
A register operand can be cleared to zero using any of the instructions: MOV, SUB, AND, and XOR.
A memory operand can be cleared to zero using either the MOV or AND instruction.
The followings are ways to clear any memory location to zero.
 | mov VAR1, 0
|  | and ARRAY[2], 0
| |