0% found this document useful (0 votes)
10 views35 pages

12 Arithmetic FINAL

Uploaded by

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

12 Arithmetic FINAL

Uploaded by

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

Integer Arithmetic

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:

• An arithmetic shift fills the newly created bit position


with a copy of the number’s sign bit: The lowest bit is
shifted into the Carry flag:
Logical vs arithmetic shifts

• 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

Shifting left n bits multiplies the operand by 2n


For example, 5*4 = 5 * 22 = 20
mov dl,5
shl dl,2 ; DL =5x4= 20
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.

Application of SHR: Fast Division


Shifting right n bits divides the operand by 2n
mov dl,80 ;DL= 80d =0101 0000b
shr dl,1 ; DL = 40 =0010 1000b
shr dl,2 ; DL = 40/4 =10d =0000 1010b
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.

An arithmetic shift preserves the number's


sign.mov dl,-80 ;DL = -80 = 1011 0000b
sar dl,1 ; DL = -40 = 1101 1000b
sar dl,2 ; DL = -10 = 1110 1100b
ROL instruction
• ROL (rotate left) 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

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 . . .

Indicate the hexadecimal value of AL after


each rotation:

mov al,6Bh
ror al,1 a.
rol al,3 b.
Your turn . . .

Indicate the hexadecimal value of AL after


each rotation:

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:

EAX * 36 mov eax,123


= EAX * (32 + 4) mov ebx,eax
= (EAX * 32)+(EAX * shl eax,5
4)
shl ebx,2
add eax,ebx
Your turn . . .

Multiply AX by 26, using shifting and addition


instructions. Hint: 26 = 16 + 8 + 2.
mov ax,2 ; test value
Your turn . . .

Multiply AX by 26, using shifting and addition


instructions. Hint: 26 = 16 + 8 + 2.
mov ax,2 ; test value

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

12345h * 1000h, using 32-bit operands:


mov eax,12345h
mov ebx,1000h
mul ebx ; EDX:EAX=0000000012345000h, CF=0
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

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

EDX = 00000012h, EAX = 87650000h, CF = 1


DIV instruction
• The DIV (unsigned divide) instruction performs
8-bit, 16-bit, and 32-bit division on unsigned
integers
• A single operand is supplied (register or
memory operand), which is assumed to be the
divisor
• Instruction formats:
DIV r/m8 Default Operands:
DIV r/m16
DIV r/m32
DIV examples

Divide 8003h by 100h, using 16-bit operands:


mov dx,0 ; clear dividend, high
mov ax,8003h ; dividend, low
mov cx,100h ; divisor
div cx ; AX = 0080h, DX = 3

Same division, using 32-bit operands:

mov edx,0 ; clear dividend, high


mov eax,8003h ; dividend, low
mov ecx,100h ; divisor
div ecx ; EAX=00000080h,DX= 3
Divide overflow
• Divide overflow happens when the quotient is
too large to fit into the destination.
mov ax, 1000h
mov bl, 10h
div bl
It causes a CPU interrupt and halts the program.
(divided by zero cause similar results)

To prevent division by zero, test the divisor


before dividing:
e.g. Je (jump if equal) etc.
Implementing arithmetic expressions
• Some good reasons to learn how to implement
expressions:
– Learn how compilers do it
– Test your understanding of MUL & DIV
– Check for 32-bit overflow
Implementing arithmetic expressions
Example: eax = (-var1 * var2) + var3
mov eax,var1
neg eax
mul var2
jo TooBig ; check for overflow
add eax,var3

Example: var4 = (var1 * 5) / (var2 – 3)


mov eax,var1 ; left side
mov ebx,5
mul ebx ; EDX:EAX = product
mov ebx,var2 ; right side
sub ebx,3
div ebx ; final division
mov var4,eax
Your turn . . .
Implement the following expression using
signed 32-bit integers:
eax = (ebx * 20) / ecx
Your turn . . .
Implement the following expression using
signed 32-bit integers:
eax = (ebx * 20) / ecx
mov eax,20
mul ebx
div ecx

You might also like