()د+,-.( ا012,.( ا3455.ا 678) (0345.( ا90:.ا ( ?087.م ا2A) 678)-(0,;<=.( ا043.ا 116ext 3932961 :Tel (0D<8E. ا678)
Kingdom of Saudi Arabia Royal Commission at Yanbu Yanbu University College (Boys' and Girls Campus) Department of CS / MIS Yanbu Al-Sinaiyah
BACHELOR OF SCIENCE IN COMPUTER SCIENCES
Lab Work
Computer Organization and Assembly ( CS203) Second Semester - Academic Year 2007 – 08
Lab Sheet 4 Practicing Logical and Bit Manipulation Instructions in 8086
Objective: The objective of this lab is to make students practice 1. Demonstrate AND and OR instructions 2. Practice XOR and NOT instructions 3. Practice shifting instructions, SHL and SHR 4. Practice rotate instructions, ROL and ROR
Outline: Using AND, OR, XOR, NOT, SHL, SHR, ROL, and ROR instructions in 8086
___________________________________________________________________________________________________ 1 Prepared by: Khurram Tanvir CS Department YUC
Introduction: In this experiment you will be introduced to logical operations and logical shift and rotate instructions. Logical Instructions: Logic shift and rotate instructions are called bit manipulation operations. These operations are designed for low-level operations, and are commonly used for low-level control of input/output devices. The list of the logic operations of the 8086 is given in the table, along with examples, and the effect of these operations on the flags. The “*” in the table means that the corresponding flag may change as a result of executing the instruction. The “-” means that the corresponding flag is not affected by the instruction, whereas the “?” means that the flag is undefined after executing the instruction.
Instruction
Example AND AX, FFDFH OR AL, 20H XOR NUM1, FF00 NOT NUM2
AND OR XOR NOT
Meaning AX ← AX AND FFDFH AL ← AL OR 20H [NUM1]←[NUM1]XOR FF00 _______ [NUM2]←[ NUM2]
Flags OF
SF
ZF
AF
PF
0 0 0 -
* * * -
* * * -
? ? ? -
* * * -
Summary of the Logic Instructions of the 8086 Microprocessor The logic operations are the software analogy of logic gates. They are commonly used to separate a bit or a group of bits in a register or in a memory location, for the purpose of testing, resetting or complementing. For example, if b is the value of a certain bit in a number. The related effects of the basic operations on a single bit are indicated in Table 5.3: Operation b AND 0 = b OR 1 = b XOR 1 = b XOR 0 =
0 1 b’ b
Effect Reset the bit Set the bit Complement the bit -
Effects on bits of the basic logic instructions The Shift Operations: The shift operations are used to multiply or divide a number by another number that is a power of 2 (i.e. 2n or 2 –n). Multiplication by 2 is achieved by a one-bit left shift, while division by 2 is achieved by a one-bit right shift.
___________________________________________________________________________________________________ 2 Prepared by: Khurram Tanvir CS Department YUC
• • • •
shl shl shl shl
ax, ax, ax, ax,
1 2 3 4
;Equivalent ;Equivalent ;Equivalent ;Equivalent
to to to to
AX*2 AX*4 AX*8 AX*16
The rotate operations are very similar to the shift operations, but the bits are shifted out from one end of a number and fed back into the other end to fill the vacated bits. They are provided to facilitate the shift of long numbers (i.e. numbers of more than 16 bits). They are also used to reposition a certain bit of a number into a desired bit-location.
Right shifting
Left shifting
Rotate left
Rotate right
Type
6. Instruction
Example
Shift Rotate
SHL
SHL AX,1
SHR
SHR NUM2,CL
ROL
ROL BH,CL
ROR
ROR NUM1,1
Meaning Shift AX left by 1 bit. Fill vacated bit with 0. Shift NUM2 right by the number of bits indicated in CL. Fill vacated bits with 0. Same as SHL, but shifted bits are fed back to fill vacated bits. Same as SHR, but shifted bits are fed back to fill vacated bits.
Flags
OF
SF
ZF
AF
PF
CF
*
*
*
?
*
*
*
0
*
?
*
*
*
-
-
-
-
*
*
-
-
-
-
*
Summary of the Shift and Rotate Instructions of the 8086 Microprocessor
___________________________________________________________________________________________________ 3 Prepared by: Khurram Tanvir CS Department YUC
Practice Program 1 Fill in table while running the above program using CodeView. TITLE “Logic Instructions” ; This program shows the effect of the logic instructions .MODEL SMALL .STACK 200 .DATA NUM1 DW NUM2 DB .CODE .STARTUP
0FA62H 94H
MOV AND OR XOR
AX, NUM1 AX, 0FFDFH AL, 20H NUM1, 0FF00H ; NUM1 NOT NUM2 XOR AX, AX MOV AX, NUM1 AND AX, 0008H XOR AX, 0080H
;load AX with number NUM1 ;Reset 6th bit of AX ;Set 6th bit of AL ;Complement the high order byte of ;Complement NUM2 ;Clear AX ; Isolate bit 4 of NUM1 ;Complement 4th bit of AX
.EXIT END
Destination Content Statement
1. 2. 3. 4. 5. 6. 7. 8. 9.
Before
After
Status Flags O F
D F
I F
S F
Z F
A F
P F
C F
MOV AX, NUM1 AND AX, 0FFDFH OR AL, 20H XOR NUM1, 0FF00H NOT NUM2 XOR AX, AX MOV AX, NUM1 AND AX, 0008H XOR AX, 0080H
___________________________________________________________________________________________________ 4 Prepared by: Khurram Tanvir CS Department YUC
Practice Program 2 Fill in table while running the above program using CodeView. TITLE “Logical and Shifting Instructions” ; This program shows the effect of the logic instructions .MODEL SMALL .STACK 200 .CODE .STARTUP MOV AH, 34H SHL AH, 1 AND AH, 65H SHR AH, 3 NOT AH ROR AH, 1 XOR AH, 88H ROL AH, 1 .EXIT END Destination Content Statement
1. 2. 3. 4. 5. 6. 7. 8.
Before
After
Status Flags O F
D F
I F
S F
Z F
A F
P F
C F
MOV AH, 34H SHL AH, 1 AND AH, 65H SHR AH, 3 NOT AH ROR AH, 1 XOR AH, 88H ROL AH, 1
Exercise Write a program to store a value shown in the following table in AH register. Write further instructions to change the value in AH register so that a) b7 and b6 are inverted. b) b1 and b0 are set to 1. c) B2 is cleared to 0. b7 b6 b5 b4 b3 b2 b1 b0 d) Right Shift the result in AH by 3 0 1 1 0 1 1 0 0 e) Rotate left the result in AH by 1 Use code view to view output of your code line by line. ___________________________________________________________________________________________________ 5 Prepared by: Khurram Tanvir CS Department YUC