Week9 L2 Chap7 Shift & Rotation Instructions
Week9 L2 Chap7 Shift & Rotation Instructions
Shift
and Rotation
Arithmetical shift
Shift arithmetic left
Shift arithmetic right
Reference
Introduction
Shift operation move the bits in a pattern, changing the position of the bits. They can
move bits to the left or to the right. We can divide shift operation into two categories:
1. Logical Shift
2 Arithmetic Shift
2.
Logical Shift
A logical shift moves the bits within the cell one position to the right or to the
left
In a logical right shift, the least significant bit LSB is discarded and the most
significant bit MSB is assigned 0.
0
In a logical left shift, the LSB is assigned 0 and the msb is discarded.
A shift instruction will have an operand that specifies how many times the one
position
iti shift
hift iis applied.
li d
There are two kinds of Logical shift:
1. Shift Left
2. Shift Right
Left Shift
• A shift left logical of one position moves each bit to the left by one.
one The low-order bit
LSB is replaced by a zero bit and the high-order bit MSB move to CF (Carry Flag)
• Shifting by two positions is the same as performing a one-position shift two times.
Shifting
g byy zero p
positions leaves the p
pattern unchanged.
g Shifting
g an N-bit p
pattern left
by N or more positions changes all of the bits to zero.
• The picture shows the operation performed on eight bits. The original pattern is
11010011. The resulting pattern is 10100110.
Right shift
• A shift right logical of one position moves each bit to the right by one. The high-order
bit MSB is replaced by a zero bit and the low-order bit LSB move to CF (Carry Flag)
• Shifting by two positions is the same as performing a one-position shift two times.
Shifting by zero positions leaves the pattern unchanged. Shifting an N-bit pattern right
by N or more positions changes all of the bits to zero.
zero
• The picture shows the operation performed on eight bits. The original pattern is
00010111. The resulting pattern is 00001011.
Arithmetic shift
Arithmetic shift operations assume that the bit pattern is a signed
integer in two’s complement format. Arithmetic right shift is used to
divide an integer by two
two, while arithmetic left shift is used to multiply
an integer by two.
There are two kinds of arithmetic shift:
1. Left arithmetic shift
2. Right arithmetic shift
Left Arithmetic shift
• An shift arithmetic left is the same as a logical left shift
• A shift arithmetic left of one position moves each bit to the left by one. The low-
order bit LSB is replaced by a zero bit and the high-order bit MSB move to CF (Carry
Flag)
• A left
l f arithmetic
h shift
h f by
b n is equivalent
l to multiplying
l l by
b 2n . In 2’s
’ complement,
l
positive or negative, a logical left shift, is equivalent to multiplication by two.
• The picture shows the operation performed on eight bits. The original pattern is
11010011 The resulting pattern is 10100110
11010011. 10100110.
Shift Arithmetic Right
A shift arithmetic right of one position moves each bit to the right by one. The high-order bit
MSB iis replaced
l dbby sign
i bibit and
d the
h llow-order
d bibit LSB move to CF (C
(Carry Fl
Flag))
A shift arithmetic right is equivalent to integer division by two.
In 2’s complement, positive or negative, division by two is accomplished via an shift arithmetic
right .
The picture shows the operation performed on eight bits. The original pattern is 00010111. The
resulting pattern is 00001011
Contents
• Types of Rotation • RCL Instruction
• ROL Instruction • RCR Instruction
• ROR Instruction
• Difference
Types
ROL Instruction
CF
1 1 0 1 1 0 1 1 1 BH
2
1
1 0 1 1 0 1 1 1 1
3
011
1 0 1 1 1 1 0 1 1
011
ROR Instruction
ROR (rotate right) shifts each bit to the right
The lowest bit is copied
p into both the Carry
y flag
g and into
the highest bit
No bits are lost
ROR is for unsigned data
A few examples on ROR:
1
2
CF
1 BH 1 0 1 1 0 1 1 1
1
2 1 1 0 1 1 0 1 1 1
011
3 0 1 1 1 1 0 1 1 0
011
RCL Instruction
CF
0 1 0 0 0 1 0 0 0 BL
1 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 1
1
RCR Instruction
RCR (rotate carry right) shifts each bit to the right
Copies the Carry flag to the most significant bit
Copies the least significant bit to the Carry flag
Example:
a p e:
STC ; CF = 1
MOV AH,10H ; CF,AH = 00010000 1
RCR AH,1 ; CF,AH = 10001000 0
CF
AH 0 0 0 1 0 0 0 0 1
1 1 0 0 0 1 0 0 0 0
Difference
The
Th difference
diff b
between
t ROR and d RCR iis only
l the
th way
of operation. In RCR, every bit that is rotated will enter
the carry flag before entering the leftmost bit
RCL works like just like ROL except that CF is part of the
circle of bits being rotated.
rotated