University of Technology
Department of Electrical Engineering
Microprocessor Engineering
Third Class
Lecture Five/Part2
8086 Instruction Set2
Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel Nasrat Abdullah
Bit Manipulation Instructions
These instructions are used at the bit level.
These instructions can be used for: Testing a zero bit, Set or
reset a bit, Shift bits across registers.
LOGICAL SHIFT ROTATE
INSTRUCTIONS INSTRUCTIONS INSTRUCTIONS
NOT ROL
AND SHL / SAL ROR
OR SHR
XOR SAR RCL
RCR
TEST
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N.
Abdullah 2
LOGICAL INSTRUCTIONS
Memonic Meaning Format Operation Flags
Effected
NOT Logical NOT NOT D ( D¯) (D) None
AND Logical AND AND D,S (S) . D) (D) O, S, Z, P, C
Logical
OR
Inclusive OR
OR D,S (S) + D) (D) O, S, Z, P, C
Logical
XOR Exclusive OR XOR D,S (S) D) (D) O, S, Z, P, C
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel 3
N. Abdullah
Logical
Instructions
Logical AND: used to clear certain bits in the operand(masking)
Example: Clear the high nibble of BL register
AND BL, 0FH ; (xxxxxxxx AND 0000 1111 = 0000 xxxx)
Example: Clear bit 5 of DH register
AND DH, DFH ; (xxxxxxxx AND 1101 1111 = xx0xxxxx)
Logical OR: Used to set certain bits
Example: Set the lower three bits of BL register
OR BL, 07H ; (xxxxxxxx OR 0000 0111 = xxxx x111)
Example: Set bit 7 of AX register
ORAH, 80H ; (xxxxxxxx OR 1000 0000 = 1xxxxxxx)
Logical XOR
Used to invert certain bits (toggling bits)
Used to clear a register by XORed it with itself
Example: Invert bit 2 of DL register
XOR BL, 04H ; (xxxx xxxx OR 0000 0100 =xxxx xxx)
Example: Clear DX register
XOR DX, DX (DX will be 0000H)
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 4
Logical
Instructions
Example: Clear bits 0 and 1, set bits 6 and 7, invert bit 5 of
register CL:
AND CL, OFCH ; 1111 1100B
OR CL, 0C0H ; 1100 0000B
XOR CL, 020H ; 0010 0000B
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 5
Shift
Instructions
Shift instructions are used to
Align data
Isolate bit of a byte of word so that it can be tested
Perform simple multiply and divide computations
Mnem. Meaning Format Operation Flags Effected
Shift Shift the (D) left by the number of C, P, S, Z
SAL D, Count
SAL/ arithmetic SHL D, Count bit positions equal to Count and A undefined
SHL left /shift fill the vacated bits positions on O undefined
logical left the right with zeros if count ≠1
Shift the (D) right by the number C, P, S, Z
shift logical of bit positions equal to Count and A undefined
SHR SHR D,Count
right fill the vacated bit positions on the O undefined
left with zeros if count ≠1
Shift the (D) right by the number
Shift of bit positions equal to Count and C, P, S, Z
A undefined
SAR arithmetic OR D,S fill the vacated bit positions on the O undefined
right left with the original most
if count ≠1
significant bit
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 6
Shift
Instructions
Example: let AX=1234H what is the value of AX after execution of next
instruction
SHL AX, 1
Solution:
AX before
AX after
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 7
Shift
Instructions
Example:
MOV CL, 2H
SHR DX, CL
Solution:
DX before
DX after
Example: Assume CL= 2 and AX= 091AH. Determine the new contents
of AX and CF after the instruction SAR AX, CL is executed.
Solution:
AX before
AX after
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 8
Shift
Instructions
Example: Multiply AX by 10 using shift instructions
Solution:
SHL AX, 1
MOV BX, AX
MOV CL,2
SHL AX,CL
ADD AX, BX
Example: Assume DL contains signed number; divide it by 4
using shift instruction?
Solution:
MOV CL , 2
SAR DL , CL
Lecture 5/ Part 2: Bit Manipulation Instruction 9
Assist. Prof. Dr. Hadeel N. Abdullah
Rotate
Instructions
Mne Meanin Flags
m. g Format Operation Effected
Rotate the (D) left by the number of
C
ROL Rotate ROL D, Count bit positions equal to Count. Each bit O undefined if
left shifted out from the leftmost bit goes
count ≠1
back into the rightmost bit position.
Rotate the (D) right by the number of
bit positions equal to Count. Each bit C,O
Rotate
RCL right ROR D,Count shifted out from the rightmost bit undefined if
goes back into the leftmost bit count ≠1
position.
Rotate
C,O
left Same as ROL except carry is attached
RCL RCL D,Count undefined if
through to (D) for rotation. count ≠1
carry
Rotate C,O
right Same as ROR except carry is attached
RCR RCR D,Count undefined if
through to (D) for rotation. count ≠1
carry
Lecture 5/ Part 2: Bit Manipulation Instruction 10
Assist. Prof. Dr. Hadeel N. Abdullah
Rotate
Instructions
Example: Assume AX = 1234H , what is the result of executing the instruction
ROL AX, 1
Solution:
AX before
AX after
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 11
Rotate
Instructions
Example: If (CL) =04H and AX=1234A. Determine the new contents of AX and the carry
flag after executing the instructions:
a) ROL AX, 1
b) ROR AX, CL
Solution:
AX before
AX after
AX before
AX after
12
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah
13
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah
TEST Instruction
Test instruction: is similar to the AND instruction. The
difference is that the AND instruction change the destination
operand, while the TEST instruction does not. A TEST only affects
the condition of the flag register, which indicates the result of the
test. The TEST instruction uses the same addressing modes as
AND instruction.
Lecture 5/ Part 2: Bit Manipulation Instruction 14
Assist. Prof. Dr. Hadeel N. Abdullah
Homework
1. Fill in the blanks
a. Two examples of logical instructions are___________________.
b. Two examples of shift instructions in 8086 are_________________.
c. TEST instruction in 8086 is used _____________________
2. Specify True or False
a. OR AL, FFh makes AL register contents equal to zero
(True/False)
b. OF flag is set if the leftmost bit has changed as a result of the
shift (True/False)
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 15
Homework
3. ROL instruction is used for
a. Shifting left
b. Byte wise rotate left
c. Bit wise rotate left
d. None of above
4. Which of the flag is not affected as a result of shift instruction?
a. Parity flag
b. Zero flag
c. Auxiliary carry flag
d. Sign flag
5. How can an ASCII digit be converted to Decimal Digit using Logical
Instructions. Explain with example
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 16
Homework
6. The instruction that performs logical AND operation and the result of
the operation is not available is
a) AAA
b) AND
c) TEST
d) XOR
Lecture 5/ Part 2: Bit Manipulation Instruction
Assist. Prof. Dr.
Hadeel N. Abdullah 17