You are on page 1of 4

Microprocessor Systems (EE 271)

Lab Manual #4

4th Semester (Session 2011)

Name: ________________________________________Reg. #:2011-EE-_________ _Date: __________________________

Logic, Shift & Rotate Instructions


After this lab you will be able to:
Use logic instructions in assembly language Use arithmetic instructions in assembly language Use Shift and Rotate instructions in assembly language 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 Table shown below, 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.

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. 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. The Shift Arithmetic Right (SAR) instruction, is used to manipulate signed numbers. The regular Right Shift (SHR) of a signed number affects the sign bit, which could cause numbers to change their sign. The SAR preserves the sign bit by filling the vacated bits with the sign of the number. 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 used to reposition a certain bit of a number into a desired bit- location. The rotate right or left instructions through the carry flag (RCL and RCR) are similar to the regular rotate instructions (ROL and ROR), but the carry flag is considered as a part of the number. Hence, before the rotate operation, the carry flag bit is appended to the number as the least significant bit in the case of RCL, or as the most significant bit in the case of RCR.
1/4

Microprocessor Systems (EE 271)

Lab Manual #4

4th Semester (Session 2011)

Summary of the Shift and Rotate Instructions

Program1
; This program shows the effect of the logic instructions
.MODEL SMALL .STACK 200 .DATA NUM1 NUM2 .CODE .STARTUP

DW DB

0FA62H 94H

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 .EXIT END

; load AX with number NUM1 ; Reset 6th bit of AX ; Set 6th bit of AL ; Complement the high order byte of NUM1 ; Complement NUM2 ; Clear AX ; Isolate bit 4 of NUM1 ; Complement 4th bit of AX

2/4

Microprocessor Systems (EE 271)

Lab Manual #4

4th Semester (Session 2011)

Fill in following table while running the above program using.

Statement 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

Destination Content Before After

Hex Output
Using shift and rotate instructions we can show contents of registers or memory locations in hexadecimal numbers. BX register contains 16 bits, which equal four hex digit values. To output the contents of BX, we start from the left and get hold of each digit, convert it to a hex character, and output it. The algorithm which follows is similar to that of binary output. Algorithm for Hex Output: For 4 times DO Move BH to DL /*BX holds output values*/ Shift DL 4 times to the right IF DL < 10 Then Convert to character in 0 9 ELSE Convert to character in A F END_IF Output character Rotate BX left 4 times END_FOR

For demonstration of HEX output, you can consult your book.


Lab Assignment:
1) Write a program that displays the contents of BX register in hexadecimal. Before displaying the contents, move some hex number in BX (e.g. 4CA9H). 2) Write a program that prompts the user to enter a character, and on subsequent lines prints its ASCII code in binary, and the number of 1 bits in its ASCII code.
3/4

Microprocessor Systems (EE 271) Sample Execution:

Lab Manual #4

4th Semester (Session 2011)

TYPE A CHARACTER: A THE ASCII CODE OF A IN BINARY IS 01000001 THE NUMBER OF 1 BITS IS 2 3) Write a program that prompts the user to enter a character and prints the ASCII code of the character in hex on the next line. Repeat this process until the user types a carriage return. Sample Execution: TYPE A CHARACTER: Z THE ASCII CODE OF Z IN HEX IS 5A TYPE A CHARACTER:

4/4