0% found this document useful (0 votes)
31 views

Lab 09

The document discusses shift and rotate instructions in assembly language. It describes logical and arithmetic shift instructions (SHL, SRL, SAL, SAR) that shift bits left or right by multiplying or dividing the operand. It also covers rotate instructions (ROL, ROR, RCL, RCR) that shift and copy bits while maintaining the same number of bits. Code examples are provided to multiply values using shifts and to isolate bits from a data field using shifts and masks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lab 09

The document discusses shift and rotate instructions in assembly language. It describes logical and arithmetic shift instructions (SHL, SRL, SAL, SAR) that shift bits left or right by multiplying or dividing the operand. It also covers rotate instructions (ROL, ROR, RCL, RCR) that shift and copy bits while maintaining the same number of bits. Code examples are provided to multiply values using shifts and to isolate bits from a data field using shifts and masks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 09: Shift and Rotate Instructions

OBJECTIVE
To learn the basic ‘shift and rotate’ instructions and their use.
Shift Instructions
The 8086 can perform two types of Shift operations, the logical shift and the
arithmetic shift. There are four shift operations (SHL, SRL, SAL, SAR).
Logical Shift: Fills the newly created bit position with 0 (zero):

SHR

Arithmetic Shift: Right shift fills the newly created bit positions with a copy
of the number’s sign bit (MSB):
SAR

1
‘Shifting Left’ by 1-bit multiplies a number by 2, and shifting left n-bits
multiplies the operand by 2n.
‘Shifting Right’ by 1-bit divides a number by 2, and shifting right by n-bits
divides the operand by 2n.
Command Meaning Format
SAL Shift Arithmetic Left SAL D, count
SAR Shift Arithmetic Right SAR D, count
SHL Shift Logical Left SHL D, count
SHR Shift Logical Right SHR D, count

Note: Count can be an ‘immediate value’ or may be contained in ‘CL’ register.


If the source operand is specified as CL instead of a number, then the count in
this register represents the bit positions the contents of the source operand are
to be shifted. This permits the count to be defined under software control and
allows a range of shifts from 1 to 255 bits.
Command Addressing Description
Format
Shift Left instruction performs a ‘Logical Left Shift’
on the destination operand, filling the lowest bit with
REG, immediate
0(zero).
REG, CL
SHL Shifting Left 1-bit multiplies a number by 2.
memory, immediate
Example:
memory, CL
mov al,2
shl al,2 ; al = 2 * 22 = 2 * 4 = 8 ; shift left 2-bits.
Note: SAL (Shift Arith. Left) is identical to SHL.

It performs a ‘Logical Right Shift’ on the destination


REG, immediate
SHR operand. The highest bit positon is filled with a 0
REG, CL
(zero). See ‘SHR’ by 1-bit divides a number by 2. Fig

2
memory, immediate Shifting Right 1-bit divides a number by 2.
memory, CL Example:
mov al,8
shr al,2 ; al = 8 / 22 = 8 / 4 = 2 ; shift right 2-bits.

It performs a ‘Right Arithmetic Shift’ on the


destination operand. See ‘SAR’ Figure.
REG, immediate
Note: An ‘Arithmetic Shift’ preserves the no’s sign.
REG, CL
SAR Example:
memory, immediate
mov al, -8
memory, CL sar al,1 ; al = -4
mov cl,2
sar al,2 ; al = -1

Multiplication and Division using Shift Instructions:


The SHL instruction can be used to multiply an operand by 2n and the SHR
instruction can be used to divide an operand by 2n.
The MUL and DIV instructions take much longer to execute than the ‘shift’
instructions. Therefore, when multiplying/dividing an operand by a small
number, it is better to use ‘Shift’ instruction than to use the MUL/DIV
instructions. For example, ‘MUL BL’ when BL=2, takes many more clock
cycles than ‘SHL BL,1’.
Code-01: Write a program to multiply AX=2 by 7 using only Shift and
ADD instruction. You should not use the MUL instruction.
Hint: Recall that shifting left n-bit multiplies the operand by 2n.
If the multiplier is not an absolute power of 2, then express the multiplier as a
sum of terms which are absolute power of 2.

3
For example, to multiply AX by 7. (7 = 4 + 2 + 1 = 22 + 21 + 1).
Answer = AX shifted left by 2 + AX shifted left by 1 + AX.
Code:
.model small
.stack 100h
.data
.code
Main proc
Mov AX,2 ; operand to be multiplied
Mov BX,AX
Mov CX, AX
SHL BX,2 ; AX*4 = 2 * 4 = 8
SHL CX,1 ; AX*2 = 2 * 2 = 4
Add BX,CX ; 4*AX + 2*AX = 6*AX = 8 + 4 = 12
Add AX,BX ; 6*AX + 1*AX = 7*AX = 12 + 2 = 14
Main endp
End main
Code: To multiply AX by 36. Use the Hint 36 = 32 + 4. Also 26 = 16+8+2.
Rotate Instructions
The 8086 can perform two types of rotate instructions; the rotate without carry
and the rotate through carry. There are four rotate operations (ROL, ROR,
RCL and RCR). Note: In a ‘Rotate operation’, NO bits are lost.
Rotate without Carry: Shifts each bit to the left or the right. The
highest/lowest bit is copied into both the carry flag and the highest/lowest bit.

4
ROL

ROR

Rotate with Carry: Shifts each bit to the left or right. The carry flag bit (CF)
is inserted into left/right bit position based on the respective operation.
RCL

RCR

ROL shifts each bit to the left. The highest bit (MSB)
REG, immediate is copied into both the Carry Flag (CF) and into the
REG, CL lowest bit (LSB). No bits are lost. See ‘ROL’ Fig.
ROL
memory, immediate Example:
memory, CL mov al, 11110000b
rol al,1 ; al = 11100001, CF = 1.

ROR shifts each bit to the right. The lowest bit (LSB)
REG, immediate is copied into both the Carry Flag (CF) and into the
REG, CL highest bit (MSB). No bits are lost. See ‘ROR’ Fig.
ROR
memory, immediate Example:
memory, CL mov al, 11110000b
ror al,1 ; al = 01111000, CF = 0.

5
RCL (Rotate Carry Left) shifts each bit to the left.
Copies the Carry Flag (CF) to the Least Significant
REG, immediate
Bit (LSB). Copies the Most Significant Bit (MSB) to
REG, CL
RCL the Carry Flag (CF). See ‘RCL’ Figure.
memory, immediate
Example:
memory, CL
clc ; clear carry flag, CF = 0
mov al, 88h ; CF,AL = 0 10001000b
rcl al,1 ; CF,AL = 1 00010000b

RCR (Rotate Carry Right) shifts each bit to the right.


Copies the Carry Flag (CF) to the Most Significant
REG, immediate Bit (MSB). Copies the Least Significant Bit (LSB) to
REG, CL the Carry Flag (CF). See ‘RCR’ Figure.
RCR
memory, immediate Example:
memory, CL stc ; set carry flag, CF = 1
mov al, 10h ; CF,AL = 1 00010000b
rcr al,1 ; CF,AL = 0 10001000b

Code-02: The MS-DOS file Date field packs the ‘year, month, day’ into
16-bits. Isolate the ‘month’ field from the bit stream given below. [2Ah]

Code: mov ax,dx ; make a copy of DX = 0010_0110_0110_1010b


Shr ax,5 ; shift right 5 bits
And al,00001111b ; clear bits 4 to 7
mov month,al ; save in month. Note: same to hours:min:sec. [2Ch]
< The End >
6
TASK:
Write an assembly program to perform various shift and rotate
operations.

You might also like