Objectives, Time Allocation an Shift Instructions
 Logical Instructions

## 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.

Pentium provides five logical instructions. All logical instructions need two operands except NOT instructions which is a unary.

 AND destination, source OR destination, source XOR destination, source TEST destination, source NOT destination

 The result of the operation is stored in the Destination, which must be a general register or a memory location. The Source may be an immediate value, register, or memory location. The Destination and Source CANNOT both be memory locations. The Destination and Source must be of the same size (8-, 16-. 32-bit).

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
movBL, 01010111b
andBL, 0F0h

Result in BL is:
 0 1 0 1 0 0 0 0

## 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,

1. 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.

2. 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.

3. 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:

LetterASCII codeLetterASCII code
'a'
 0 1 1 0 0 0 0 1
'A'
 0 1 0 0 0 0 0 1
'b'
 0 1 1 0 0 0 1 0
'B'
 0 1 0 0 0 0 1 0
'c'
 0 1 1 0 0 0 1 1
'C'
 0 1 0 0 0 0 1 1
...
 0 1 1 X X X X X
...
 0 1 0 X X X X X
'z'
 0 1 1 1 1 0 1 0
'Z'
 0 1 0 1 1 0 1 0

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
 mov AL , 'M' or AL, 20h

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 digitASCII codeDecimal digitBinary code
'0'
 0 0 1 1 0 0 0 0
0
 0 0 0 0 0 0 0 0
'1'
 0 0 1 1 0 0 0 1
1
 0 0 0 0 0 0 0 1
'2'
 0 0 1 1 0 0 1 0
2
 0 0 0 0 0 0 1 0
x
 0 0 1 1 x x x x
x
 0 0 0 0 x x x x
'9'
 0 0 1 1 1 0 0 1
9
 0 0 0 0 1 0 0 1

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
 mov BH , '3' and BH, 0Fh

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
 mov BH , 6 or BH, 30h

## 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

The followings are ways to examine whether or not any general-purpose register is equal to zero.

 or AL, AL and EDX, EDX test ESI, ESI

## 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.

The followings are ways to clear any general-purpose register to zero.

 mov BL, 0 sub AX, AX and CL, 0 xor DH, DH

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