Lecture 6 Logical and Bit Manipulation Instructions
Lecture 6 Logical and Bit Manipulation Instructions
Lecture 6 Logical and Bit Manipulation Instructions
ا
ا د
Royal Commission at Yanbu ا ا
University College – Yanbu - ا ا
Department of ACS & AIT
Yanbu Al-Sinaiyah ا
Bitwise Logical instructions are the most primitive operations needed by every computer
architecture. The logical operations are performed at bit-by-bit basis.
All logical instructions need two operands except NOT. It needs only one operand.
• The result of the operation is stored in the destination, which must be a general
register or a memory location as shown in the table.
• 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 or 16 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
Carry Flag Overflow Flag Zero Flag Sign Flag Parity Flag Auxiliary Flag
0 0 Modified Modified Modified Undefined
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
Result in BL is: 0 1 0 1 0 0 0 0
The main usage of bitwise logical instructions are: some selected bits in the destination
operand.
To isolate the bit(s), 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:
is executed. The mask bits are chosen based on the following properties of AND, OR,
and XOR:
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.
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:
'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.
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.
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.
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:
'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
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.
MOV BH , 6
OR BH, 30h
The Logic Instructions can be used to examine the status of selected bits in the
destination operand.
OR AL, AL
AND DX, DX
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.
MOV VAR1, 0
AND ARRAY[2], 0
Another interpretation:
Logical shift instructions work on unsigned binary numbers
Example
All shift instructions affect some flags like other instructions. However, the way Carry
Flag (CF) and Overflow Flag (OF) affected are different.
Carry Flag (CF) Overflow Flag (OF) Zero Flag (ZF) Sign Flag (SF) Parity Flag(PF) Auxiliary Flag (AF)
Yes Yes Yes Yes Yes NO
Semantic:
SHL shifts the leftmost bit into the Carry Flag (CF) and overwrites the current value of
the Carry Flag.
Each of the remaining bits is shifted leftwise and 0 is shifted into the rightmost bit.
Example: Shifting AL=3 leftwise by 1 bit
mov AL, 3
shl AL, 1 ; shift left AL 1 place
SOLUTION
Initially AL = 3 = 0 0 0 0 0 0 1 1
Finally AL = 6 = 0 0 0 0 0 1 1 0
CF = 0
SF = 0
ZF = 0 because the result is not equal to 0.
PF = 1 because there are 2 bits 1 in AL.
OF = 0 Why?
SHR shifts the rightmost bit of operand into the Carry Flag; the current value of the Carry
Flag is lost. Each of the remaining bits is shifted right and 0 is shifted into the leftmost bit
of operand.
Example
mov BL , 00000101B ; BL := 5
shr BL , 1 ; BL := 2
Rotate instructions are unparalleled instructions compared to any high level languages.
A rotate operation shifts the bits within a cell without discarding. For example, a rotate
right shifts bits to the right. Instead of throwing away the rightmost bit (LSB), it is placed
in the leftmost position of the rotated cell.
A rotate left shifts bits to the left. Instead of throwing away the leftmost bit, it is placed in
the rightmost position of the rotated cell. In addition, at the same time the leftmost bit is
copied into the carry flag.
a. Shifting left
b. Byte wise rotate left
c. Bit wise rotate left
d. None of above
a. Parity flag
b. Zero flag
c. Auxiliary carry flag
d. Sign flag
Question: How can an ASCII digit be converted to Decimal Digit using Logical
Instructions. Explain with example