4.3.1 Logical Operations

The logical instructions—AND, OR, XOR, and NOT—operate on each bit in one operand and on the corresponding bit in the other. The following list shows how each instruction works. Except for NOT, these instructions require two integers of the same size.

Instruction Sets a Bit to 1 under These Conditions

AND Both corresponding bits in the operands have the value 1.
OR Either of the corresponding bits in the operands has the value 1.
XOR Either, but not both, of the corresponding bits in the operands has the value 1.
NOT The corresponding bit in the operand is 0. (This instruction takes only one operand.)

NOTE:

Do not confuse logical instructions with the logical operators, which perform these operations at assembly time, not run time. Although the names are the same, the assembler recognizes the difference from context.

The following example shows the result of the AND, OR, XOR, and NOT instructions operating on a value in the AX register and in a mask. A mask is a binary or hexadecimal number with appropriate bits set for the intended operation.

mov ax, 035h ; Load value 00110101

and ax, 0FBh ; Clear bit 2 AND 11111011

; --------

; Value is now 31h 00110001

or ax, 016h ; Set bits 4,2,1 OR 00010110

; --------

; Value is now 37h 00110111

xor ax, 0ADh ; Toggle bits 7,5,3,2,0 XOR 10101101

; --------

; Value is now 9Ah 10011010

not ax ; Value is now 65h 01100101

Summary: Use AND, OR, and XOR to set or clear specific bits.

You can use the AND instruction to clear the value of specific bits regardless of their current settings. To do this, put the target value in one operand and a mask of the bits you want to clear in the other. The bits of the mask should be 0 for any bit positions you want to clear and 1 for any bit positions you want to remain unchanged.

You can use the OR instruction to force specific bits to 1 regardless of their current settings. The bits of the mask should be 1 for any bit positions you want to set and 0 for any bit positions you want to remain unchanged.

You can use the XOR instruction to toggle the value of specific bits (reverse them from their current settings). This instruction sets a bit to 1 if the corresponding bits are different or to 0 if they are the same. The bits of the mask should be 1 for any bit positions you want to toggle and 0 for any bit positions you want to remain unchanged.

The following examples show an application for each of these instructions. The code illustrating the AND instruction converts a “y” or “n” read from the keyboard to uppercase, since bit 5 is always clear in uppercase letters. In the example for OR, the first statement is faster and uses fewer bytes than cmp bx, 0. When the operands for XOR are identical, each bit cancels itself, producing 0.

; Converts characters to uppercase

mov ah, 7 ; Get character without echo

int 21h

and al, 11011111y ; Convert to uppercase by clearing

; bit 5

cmp al, 'Y' ; Is it Y?

je yes ; If so, do Yes actions

. ; else do No actions

.

yes: .

; Compares operand to 0

or bx, bx ; Compare to 0

; 2 bytes, 2 clocks on 8088

jg positive ; BX is positive

jl negative ; BX is negative

; else BX is zero

; Sets a register to 0

xor cx, cx ; 2 bytes, 3 clocks on 8088

sub cx, cx ; 2 bytes, 3 clocks on 8088

mov cx, 0 ; 3 bytes, 4 clocks on 8088

On the 80386 and 80486, the BSF (Bit Scan Forward) and the BSR (Bit Scan Reverse) instructions perform operations similar to those of the logical instructions. They scan the contents of a register to find the first-set or last-set bit. You can use BSF or BSR to find the position of a set bit in a mask or to check if a register value is 0.