12 Arithmetic FINAL
12 Arithmetic FINAL
Reference
Section 7.2, 7.3
& 7.4 of kip
Irvine book
with slides by Kip Irvine
Chapter 7 Integer Arithmetic Overview
• Shift and Rotate Instructions
• Shift and Rotate Applications
• Multiplication and Division Instructions
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
Logical vs arithmetic shifts
• A logical shift fills the newly created bit position with
zero: The lowest bit is shifted into the Carry flag:
• Arithmetic shift preserve sign bit, whereas Logical shift can not preserve sign bit.
• Arithmetic shift perform multiplication and division operation, whereas Logical shift perform
only multiplication operation.
• Arithmetic shift is used for signed interpretation, whereas Logical shift is used for unsigned
interpretation.
https://www.ahirlabs.com/difference/arithmetic-shift-and-logical-shift/
SHL instruction
• The SHL (shift left) instruction performs a logical left
shift on the destination operand, filling the lowest bit
with 0.
• Operand types:
SHL destination,count
SHL reg,imm8
SHL mem,imm8
SHL reg,CL
SHL mem,CL
Formats shown above also apply to the SHR, SAL, SAR, ROR, ROL,
RCR, and RCL instructions.
Application of SHL: Fast multiplication
Shifting left 1 bit multiplies a number by 2
mov dl,5
shl dl,1
mov al,11110000b
rol al,1 ; AL = 11100001b
mov dl,3Fh
rol dl,4 ; DL = F3h
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
mov al,11110000b
ror al,1 ; AL = 01111000b
mov dl,3Fh
ror dl,4 ; DL = F3h
When rotating a multibyte integer by 4 bits, the effect is to rotate each hexadecimal digit
one position to the right or left.
Your turn . . .
Indicate the hexadecimal value of AL after
each shift:
mov al,6Bh
shr al,1 a.
shl al,3 b.
mov al,8Ch
sar al,1 c.
sar al,3 d.
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
C6h
sar al,1 c. F8h
sar al,3 d.
Your turn . . .
mov al,6Bh
ror al,1 a.
rol al,3 b.
Your turn . . .
mov al,6Bh
ror al,1 a.
B5h
rol al,3 b.
ADh
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
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
stc ; CF = 1
mov ah,10h ; AH = 00010000 CF=1
rcr ah,1 ; AH = 10001000 CF=0
Your turn . . .
Indicate the hexadecimal value of AL after
each rotation:
stc
mov al,6Bh
rcr al,1 a.
rcl al,3 b.
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
Shift and rotate applications
• Binary Multiplication
•
Binary multiplication
• We already know that SHL performs unsigned
multiplication efficiently when the multiplier is a power
of 2.
• Shifting an unsigned integer n bits to the left multiplies it
by 2n
• Factor any binary number into powers of 2.
For example, to multiply EAX * 36, factor 36 into 32 + 4 (as 25 + 22)and use the
distributive property of multiplication to carry out the operation:
mov dx,ax
shl dx,4 ; AX * 16
push dx ; save for later
mov dx,ax
shl dx,3 ; AX * 8
shl ax,1 ; AX * 2
add ax,dx ; AX * 10
pop dx ; recall AX * 16
add ax,dx ; AX * 26
Multiplication and division instructions
• MUL Instruction
• DIV Instruction
• Implementing Arithmetic Expressions
MUL instruction
• The MUL (unsigned multiply) instruction multiplies an
8-, 16-, or 32-bit operand by either AL, AX, or EAX.
• The instruction formats are:
MUL r/m8
MUL r/m16
MUL r/m32
Implied operands:
MUL examples
100h * 2000h, using 16-bit operands:
.data The Carry flag indicates
val1 WORD 2000h whether or not the upper
val2 WORD 100h half of the product
.code contains significant digits.
mov ax,val1
mul val2 ; DX:AX=00200000h, CF=1
mov eax,00128765h
mov ecx,10000h
mul ecx
Your turn . . .
What will be the hexadecimal values of (E)DX, (E)AX,
and the Carry flag after the following instructions
execute?
mov ax,1234h
mov bx,100h
mul bx
DX = 0012h, AX = 3400h, CF = 1
mov eax,00128765h
mov ecx,10000h
mul ecx