0% found this document useful (0 votes)
16 views16 pages

Lecture No.15

Chapter 7 of 'Assembly Language for x86 Processors' covers integer arithmetic, focusing on shift and rotate instructions. It explains logical and arithmetic shifts, the SHL and SHR instructions, and how these operations can be used for fast multiplication and division. Additionally, it discusses rotate instructions (ROL, ROR, RCL, RCR) and their effects on data in registers.

Uploaded by

ahada8878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views16 pages

Lecture No.15

Chapter 7 of 'Assembly Language for x86 Processors' covers integer arithmetic, focusing on shift and rotate instructions. It explains logical and arithmetic shifts, the SHL and SHR instructions, and how these operations can be used for fast multiplication and division. Additionally, it discusses rotate instructions (ROL, ROR, RCL, RCR) and their effects on data in registers.

Uploaded by

ahada8878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Assembly Language for x86 Processors

7th Edition
Kip R. Irvine

Chapter 7: Integer Arithmetic

Slides prepared by the author


Revision date: 1/15/2014

(c) Pearson Education, 2014. All rights reserved. You may modify and copy this slide show for your personal use, or for
use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
Shift and Rotate Instructions

• Logical vs Arithmetic Shifts


• SHL Instruction
• SHR Instruction
• SAL and SAR Instructions
• ROL Instruction
• ROR Instruction
• RCL and RCR Instructions
• SHLD/SHRD Instructions

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 2


Logical Shift

• A logical shift fills the newly created bit position with


zero:

0
CF

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 3


Arithmetic Shift

• An arithmetic shift fills the newly created bit position


with a copy of the number’s sign bit:

CF

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 4


SHL Instruction
• The SHL (shift left) instruction performs a logical left
shift on the destination operand, filling the lowest bit
with 0.

• Operand types for SHL:


SHL reg,imm8
SHL mem,imm8 (Same for all shift and
SHL reg,CL rotate instructions)
SHL mem,CL

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 5


Fast Multiplication

Shifting left 1 bit multiplies a number by 2

mov dl,5 Before: 00000101 =5


shl dl,1
After: 00001010 = 10

Shifting left n bits multiplies the operand by 2n


For example, 5 * 22 = 20
mov dl,5
shl dl,2 ; DL = 20

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 6


SHR Instruction
• The SHR (shift right) instruction performs a logical
right shift on the destination operand. The highest bit
position is filled with a zero.

0
CF

Shifting right n bits divides the operand by 2n


mov dl,80
shr dl,1 ; DL = 40
shr dl,2 ; DL = 10

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 7


SAL and SAR Instructions
• SAL (shift arithmetic left) is identical to SHL.
• SAR (shift arithmetic right) performs a right arithmetic
shift on the destination operand.

CF

An arithmetic shift preserves the number's sign.


mov dl,-80
sar dl,1 ; DL = -40
sar dl,2 ; DL = -10

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 8


Your turn . . .

Indicate the hexadecimal value of AL after each shift:

mov al,6Bh
shr al,1 a. 35h
shl al,3 b. A8h
mov al,8Ch
sar al,1 c. C6h
sar al,3 d. F8h

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 9


ROL Instruction
• ROL (rotate) shifts each bit to the left
• The highest bit is copied into both the Carry flag
and into the lowest bit
• No bits are lost

CF

mov al,11110000b
rol al,1 ; AL = 11100001b

mov dl,3Fh
rol dl,4 ; DL = F3h

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 10


ROR Instruction
• ROR (rotate right) shifts each bit to the right
• The lowest bit is copied into both the Carry flag and
into the highest bit
• No bits are lost

CF

mov al,11110000b
ror al,1 ; AL = 01111000b

mov dl,3Fh
ror dl,4 ; DL = F3h

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 11


Your turn . . .

Indicate the hexadecimal value of AL after each rotation:

mov al,6Bh
ror al,1 a. B5h
rol al,3 b. ADh

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 12


RCL Instruction
• RCL (rotate carry left) shifts each bit to the left
• Copies the Carry flag to the least significant bit
• Copies the most significant bit to the Carry flag
CF

clc ; CF = 0
mov bl,88h ; CF,BL = 0 10001000b
rcl bl,1 ; CF,BL = 1 00010000b
rcl bl,1 ; CF,BL = 0 00100001b

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 13


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
CF

stc ; CF = 1
mov ah,10h ; CF,AH = 1 00010000b
rcr ah,1 ; CF,AH = 0 10001000b

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 14


Your turn . . .

Indicate the hexadecimal value of AL after each rotation:

stc
mov al,6Bh
rcr al,1 a. B5h
rcl al,3 b. AEh

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 15


55 74 67 61 6E 67 65 6E

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 16

You might also like