Professional Documents
Culture Documents
Spring 2016
1
2
Objectives:
1. To get familiar with Rotate and Shift commands in assembly language.
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.
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.
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.
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 ;
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,
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,
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
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
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.
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