You are on page 1of 11

Independent University, Bangladesh (IUB)

School of Engineering and Computer Science


Department of Electrical and Electronic Engineering

Spring 2016

COURSE CODE : ECR 209L


COURSE NAME : Microprocessors and Interfacing Laboratory
CREDIT : 1
COURSE TEACHER : Dr. Mustafa Habib Chowdhury

LAB 3 – Intel 8086 Microprocessor:

Shift, Rotate and Loops in Assembly


Language

1
2
Objectives:
1. To get familiar with Rotate and Shift commands in assembly language.

2. To use loops in complex problems.

Introduction:
Shift and Rotate command:

Shift and Rotate commands are used to convert a number to another form where
some bits are shifted or rotated. Basic difference between “shift” and “rotate” is
shift command makes “fall of” bits at the end of register whereas rotate
command makes “Wrap around” at the end of the register. There are both
arithmetic (SAL and SAR) and logical (SHL and SHR) Shift instructions.
Graphical operations for these commands are shown below.

SAL and SHL are two mnemonics for the same instruction. This instruction shifts
each bit in the specified destination some number of bit positions to the left. As a
bit is shifted out of the LSB operation, a 0 is put in the LSB position. The MSB will
be shifted into CF. In the case of multi-bit shift, CF will contain the bit most
recently shifted out from the MSB. Bits shifted into CF previously will be lost.

The SAR instruction shifts each bit in the specified destination some number of
bit positions to the right. As a bit is shifted out of the MSB position, a copy
of the old MSB is put in the MSB position. In other words, the sign bit is
copied into the MSB. The LSB will be shifted into CF. In the case of multiple-
bit shift, CF will contain the bit most recently shifted out from the LSB. Bits
shifted into CF previously will be lost.

The SHR instruction shifts each bit in the specified destination some number of
bit positions to the right. As a bit is shifted out of the MSB position, a 0 is put in
its place. The bit shifted out of the LSB position goes to CF. In the case of multi-
3
bit shifts, CF will contain the bit most recently shifted out from the LSB. Bits
shifted into CF previously will be lost.

The following figure gives an overview:

There are 4 types (ROL, ROR, RCL, RCR) of Rotate instructions.

ROL = Rotate LEFT

This instruction rotates all the bits in a specified word or byte to the left some
number of bit positions. The data bit rotated out of MSB is circled back into the
LSB. It is also copied into CF. In the case of multiple-bit rotate, CF will contain a
copy of the bit most recently moved out of the MSB.

4
ROR = Rotate RIGHT

This instruction rotates all the bits in a specified word or byte some number of bit positions to
right. The operation is desired as a rotate rather than shift, because the bit moved out of the
LSB is rotated around into the MSB. The data bit moved out of the LSB is also copied
into CF. In the case of multiple bit rotates, CF will contain a copy of the bit most recently
moved out of the LSB.

RCL – Rotate with Carry LEFT; RCL Destination, Count

This instruction rotates all the bits in a specified word or byte some number of bit
positions to the left. The operation is circular because the MSB of the operand is
rotated into the carry flag and the bit in the carry flag is rotated around into LSB
of the operand. For multi-bit rotates, CF will contain the bit most recently rotated
out of the MSB.

RCR – Rotate with Carry RIGHT; RCR Destination, Count

This instruction rotates all the bits in a specified word or byte some number of bit
positions to the right. The operation is circular because the LSB of the operand is
rotated into the carry flag and the bit in the carry flag is rotate around into MSB
of the operand. For multi-bit rotate, CF will contain the bit most recently rotated
out of the LSB.

5
Some simple codes can be given to clarify the idea.

MOV CL,03H ;
MOV AX,02F3H; In binary 0000 0010 1111 0011
SHR AX,CL ; In binary 0000 0000 0101 1110

In this procedure, SHR commands inserts 0’s from right side. Each time a 0 is
inserted left most bit is vanished from register content.

MOV CL,03H ;
MOV AX,82F3H ; In binary 1000 0010 1111 0011
SAR AX,CL ; In binary 1111 0000 0101 1110

In this procedure, SHR commands inserts MSB content from right side. Each time
it is inserted, left most bit is vanished from register content.

Note: Only the CL register can be used to contain the value (or count) by
which the Operand will shift/rotate.

MOV CL,03H ;

MOV AX,82F3H ; In binary 1000 0010 1111 0011

ROR AX,CL ; In binary 0111 0000 0101 1110

The whole procedure can be visualized as follows.

6
Here rotate by 3 operation is shown. It is clearly seen that every bit is assigned
to a new position that is 3 places away from previous one. Unlike the shift
command no right bit is destroyed. It is placed in the leftmost position.

Exercise part 1:

(a) Program 1:

MOV CL,02H

MOV AX,105AH

SHL AX,CL

RET

Obtain AX register value and write the previous value and present value of the AX
register in binary form. What type of operation is this?

(b) Program 2:

MOV CL,04H

MOV AX,564AH

SAL AX,CL

RET

Obtain the AX register value and write the previous value and present value of
the AX register in binary form. What type of operation is this ?

(c) Perform for similar values of AX and CL with ROL, ROR, RCL, RCR command.

7
LOOP in assembly language:

Loop commands are used to perform the same operation again and again. This is
like ‘for’ or ‘while’ type loop instructions in ‘C’ or ‘MATLAB’. A common example
can be shown as,

MOV CX, 0100D


MOV AX, 564AH
Lev: DEC AX
Loop LEV
RET

Here CX acts as a count register. Loop Lev instruction leads instruction to go back
to Lev level until CX is zero. Each time Lev level is executed CX is decreased by
1. Loop command can be used for waiting purposes. Such as,

MOV CX, 0100D


Wt: NOP
Loop Wt
RET

Here the loop is executed until CX is zero. If 1 loop takes 1ms, the program will
wait for 100ms.

Exercise part 2:

(a) Program 1:

MOV AX,1025H
MOV BX,475AH
MOV CX,50H
Lev: INC AX
DEC BX
LOOP Lev
RET

Observe and describe the operation of this code.

8
(b) Program 2: This code is to find the Greatest Common Divisor (GCD) of two
numbers. The flow chart shown below explains the algorithm to find the GCD of
two arbitrary numbers:

The GCD of two numbers is performed by dividing the greater number by the
smaller number till the remainder is zero. If it is zero, the divisor is the GCD. If
the remainder is not zero, the remainder and the divisor of the previous division
are the new set of two numbers. The process is repeated by dividing the greater
of the two numbers by the smaller number till the remainder is zero and the GCD
is found.

The following program computes the GCD of any two numbers (e.g., 5 and 3) and
stores the result in the BX register and the ‘GCD’ variable.

9
.model small

DATA SEGMENT
NUM1 DW 0005H
NUM2 DW 0003H
GCD DW ?
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA

START: MOV AX, DATA ;Load the Data to AX.


MOV DS, AX ;Move the Data AX to DS.
MOV AX, NUM1 ;Move the first number to AX.
MOV BX, NUM2 ;Move the second number to BX.

UP: CMP AX, BX ;Compare the two numbers.


JE EXIT ;If equal, go to EXIT label.
JB EXCG ;If first number is below than second, go to EXCG label.

UP1: MOV DX, 0H ;Initialize the DX.


DIV BX ;Divide the first number in AX by the second number in BX, the
; result is stored in AX and the remainder is stored in DX
CMP DX, 0 ;Compare remainder to check if it is zero or not.
JE EXIT ;If remainder = zero, jump to EXIT label.
MOV AX, DX ;If remainder is non-zero, move remainder to AX.
JMP UP ;Jump to UP label.

EXCG: XCHG AX, BX ;Exchange the remainder and quotient.


JMP UP1 ;Jump to UP1.

EXIT: MOV GCD,BX ;Store the result in GCD.


MOV AH,4CH
INT 21H
CODE ENDS

In the above program, the GCD of 5 and 3 are found. You can change the values
of AX and BX and obtain the result for any other values. Find GCD of 08D4H and
235H ?

10
Homework Questions:

1. Suppose x = 20 and y = 28. Add y with x for 30 times and store the result in
DX.

2. Multiply 12 by 6 and store the result in DX.

3. Find Least Common Multiplier of 12d and 9d. First draw a flowchart showing
your logic and then write an assembly code based on the flow chart.

11

You might also like