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

CSE331 Lecture-09

The document discusses shift and rotate instructions in x86 assembly language. Shift instructions (SHL, SHR, SAR) shift bits of an operand left or right by a specified count, losing bits that shift out of the operand. Rotate instructions (ROL, ROR, RCL, RCL) shift bits cyclically within the operand, moving bits that shift out of one end back into the other end. The instructions are used to multiply/divide operands by powers of 2 and for other bit manipulation operations.

Uploaded by

Random MindWork
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CSE331 Lecture-09

The document discusses shift and rotate instructions in x86 assembly language. Shift instructions (SHL, SHR, SAR) shift bits of an operand left or right by a specified count, losing bits that shift out of the operand. Rotate instructions (ROL, ROR, RCL, RCL) shift bits cyclically within the operand, moving bits that shift out of one end back into the other end. The instructions are used to multiply/divide operands by powers of 2 and for other bit manipulation operations.

Uploaded by

Random MindWork
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

INSTRUCTION SET

Introduction to shift and rotate instructions


 The shift and rotate instructions shift the bits in the
destination operand by one or more positions either to
the left or right.

 For a shift instruction, the bits shifted out are lost.

 These instructions can be used to multiply and divide


by powers of 2.(shift left to multiply and shift right to
divide by 2)

 For a rotate instruction, bits shifted out from one end


of the operand are put back into the other end.
Shift and Rotate Instruction Set
Instructions to perform shift operations:
•SHL/SAL − Used to shift bits of a byte/word towards left and put
zero(S) in LSBs.
•SHR − Used to shift bits of a byte/word towards the right and put
zero(S) in MSBs.
•SAR − Used to shift bits of a byte/word towards the right and copy the
old MSB into the new MSB.
Instructions to perform rotate operations:
•ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to
LSB and to Carry Flag [CF].
•ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to
MSB and to Carry Flag [CF].
•RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to
CF and CF to MSB.
•RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to
CF and CF to LSB. 3
Shift Instruction Set
Mnemonic Meaning Format Operation Flags Affected

SAL/SHL Shift SAL/SHL D, Shift the (D) left by the number of CF, PF, SF, ZF
arithmetic Count bit positions equal to Count and AF undefined
left/shift fill the vacated bits positions on OF undefined if
logical left the right with zeros count≠1
SHR Shift logical SHR D,Count Shift the (D) right by the number CF, PF, SF, ZF
right of bit positions equal to Count and AF undefined
fill the vacated bit positions on the OF undefined if
left with zeros count≠1

SAR Shift SAR D,Count Shift the (D) right by the number CF, PF, SF, ZF
arithmetic of bit positions equal to Count AF undefined
right and fill the vacated bit positions OF undefined if
on the left with the original most count≠1
significant bit.

4
SAL/SHL Instruction Set
 SAL/SHL Destination, Count
 This instruction performs shift left without conservation of sign
(sign may change)
 If count is greater than 1 then it has to be stored in CL register.
Count 1 can be specified in instruction.
 SHL DH, 1
 A 0 is shifted into the rightmost bit position and the MSB is
shifted into CF.
 For Example: SHL DH, 1. DH=47H= 0100 0111b and carry=1
 After shift left DH=8EH, Carry=0

Before 1 0 1 0 0 0 1 1 1
CF
After 0 1 0 0 0 1 1 1 0
CF
5
SAL/SHL Instruction Set
 Suppose DH contains 8Ah and CL contains 3. What are the
values of DH and of CF after the instruction SHL DH, CL is
executed?
 MOV CL, 03H
 SHL DH, CL
 After execution, DH=50H and Carry=0
Before X 1 0 0 0 1 0 1 0

CF
After 1 0 0 0 1 0 1 0 0 0
CF
0 0 0 1 0 1 0 0 0 0

0 0 1 0 1 0 0 0 0 0

SAL (shift Arithmetic Left) instruction can be used instead of


SHL. Both have same machine codes. 6
The SHL Instruction - Multiplication by SHL
 A left shift on a binary number multiplies it by 2.
 Ex. if AL contains 5d = 00000101b
A left shift gives 10d = 00001010b
Another left shift gives 20d = 00010100
 When we treat left shifts as multiplication, overflow may occur.
 For 1 left shift, CF & OF accurately indicate unsigned and
signed overflow, respectively.
 Overflow flags are not reliable indicators for a multiple left shift.
Ex. SHL BL, CL ; where BL = 80h and CL = 2
CF = OF = 0
even though both signed & unsigned overflow occurred.
SHR Instruction Set
 SHR Destination, Count
 This instruction performs shift right without conservation of sign
(sign may change)
 If count is greater than 1 then it has to be stored in CL register.
Count 1 can be specified in instruction.
 SHR DH, 1
 A 0 is shifted into the msb position and the rightmost bit is
shifted into CF.
 For Example: SHR DH, 1. DH=47H= 0100 0111b and carry=1
 After shift right DH=23H, Carry=1

0 1 0 0 0 1 1 1 1 Before
CF
0 0 0 1 0 0 0 1 1 1 After
CF
8
SHR Instruction Set
 Suppose DH contains 8Ah and CL contains 2. What are the
values of DH and of CF after the instruction SHR DH, CL is
executed?
 MOV CL, 02H
 SHR DH, CL
 After execution, DH=22H and Carry=1

1 0 0 0 1 0 1 0 X Before
CF
0 1 0 0 0 1 0 1 0 After
0
CF
0 0 0 1 0 0 0 1 0 1

SAL (shift Arithmetic Left) instruction can be used instead of


SHL. Both have same machine codes.
9
SAR Instruction Set
 SAR Destination, Count
 This instruction performs shift right with conservation of sign
(sign can not change)
 If count is greater than 1 then it has to be stored in CL register.
Count 1 can be specified in instruction.
 SHR DH, 1
 In an arithmetic shift to the right, the SAR operation, the
vacated bits at the left are filled with the value of the original
MSB of the operand and the rightmost bit is shifted into CF.
 For Example: SHR DH, 1. DH=47H= 0100 0111b and carry=1
 After shift right DH=23H, Carry=1
0 1 0 0 0 1 1 1 1 Before
CF
0 0 1 0 0 0 1 1 1 After
CF
10
SAR Instruction Set
 Assume that CL contains 0216 and AX contains 091A16.
Determine the new contents of AX and the carry flag after the
instruction
 SAR AX, CL is executed
 MOV CL, 02H
 SAR DH, CL
 After execution, AX=0246H and Carry=1
0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 X Before
CF
0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 After
CF
0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1

SAR (shift Arithmetic Right) instruction can be used instead


of SHR. Both have same machine codes.
11
The SHR Instruction - Division by SHR
 For even numbers, a right shift divides it by 2.
 Ex. if AL contains 6d = 00000110b
A right shift gives 3d = 00000011b
 For odd numbers, a right shift halves it and rounds down to
the nearest integer.
 Ex. if AL contains 5d = 00000101b
A right shift gives 2d = 00000010b
 Use SHR to divide 65143 by 4, put the quotient in AX.
MOV AX, 65143 ; AX has number
MOV CL, 2 ; CL has number of right shifts
SHR AX, CL ; divide by 4
Rotate Instruction Set

13
ROL Instruction Set

 ROL Destination, Count


 This instruction performs rotate bits left without carry.
 If count is greater than 1 then it has to be stored in CL register.
Count 1 can be specified in instruction.
 ROL DH, 1
 The msb is shifted into the rightmost bit and also into the CF.
 For Example: ROL DH, 1. DH=47H= 0100 0111b and carry=1
 After shift left DH=8EH, Carry=0

Before 1 0 1 0 0 0 1 1 1
CF
After 0 1 0 0 0 1 1 1 0

CF
14
ROL Instruction Set

 ROL AX, 1

15
ROR Instruction Set
 ROR Destination, Count
 This instruction performs rotate bits right without carry.
 If count is greater than 1 then it has to be stored in CL register.
Count 1 can be specified in instruction.
 ROR DH, 1
 The rightmost is shifted into the msb bit and also into the CF.
 For Example: ROL DH, 1. DH=47H= 0100 0111b and carry=1
 After shift left DH=A3H, Carry=1

0 1 0 0 0 1 1 1 1 Before
CF
1 0 1 0 0 0 1 1 1 After
CF
16
ROR Instruction Set

 ROR AX, CL; CL=4

17
RCL Instruction Set
 RCL Destination, Count
 This instruction performs rotate bits left with carry.
 If count is greater than 1 then it has to be stored in CL
register. Count 1 can be specified in instruction.
 RCL DH, 1
 For Example: RCL DH, 1. DH=47H= 0100 0111b and
carry=1
 After shift left DH=8FH, Carry=0

Before 1 0 1 0 0 0 1 1 1
CF
After 0 1 0 0 0 1 1 1 1

CF
18
RCR Instruction Set
 RCR Destination, Count
 This instruction performs rotate bits right with carry.
 If count is greater than 1 then it has to be stored in CL
register. Count 1 can be specified in instruction.
 RCR DH, 1
 For Example: ROR DH, 1. DH=47H= 0100 0111b
and carry=1
 After shift left DH=A3H, Carry=1
0 1 0 0 0 1 1 1 1 Before
CF
1 0 1 0 0 0 1 1 1 After
CF
19
RCR Instruction Set
 What is the result in BX and CF after execution of the
following instruction?
 RCR BX, CL
 Assume that, prior to execution of the instruction
CL=0416, BX=1234H, and CF=0.

(BX)=10000001001000112 =812316
(CF)= 02

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy