Lab 09
Lab 09
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
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.
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
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]