Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiplication and Division instructions Dr.Hadi AL Saadi.

Similar presentations


Presentation on theme: "Multiplication and Division instructions Dr.Hadi AL Saadi."— Presentation transcript:

1 Multiplication and Division instructions Dr.Hadi AL Saadi

2 Multiplication and Division Instructions Multiplication  More complicated than add / sub »Produces double-length results –E.g. Multiplying two 8 bit numbers produces a 16-bit result »Cannot use a single multiply instruction for signed and unsigned numbers –add and sub instructions work both on signed and unsigned numbers –For multiplication, we need separate instructions mul for unsigned numbers imul for signed numbers Implied operands:

3 Arithmetic Instructions (cont’d) Unsigned multiplication mul source »Depending on the source operand size, the location of the other source operand and destination are selected

4 Multiplication Instructions  Example mov AL,10 mov DL,25 mul DL produces 250D in AX register (result fits in AL) The imul instruction can use the same syntax »Also supports other formats  Example mov DL,0FFH ; DL = -1 mov AL,0BEH ; AL = -66 imul DL produces 66D in AX register (again, result fits in AL)

5 Division instruction  Even more complicated than multiplication »Produces two results –Quotient –Remainder »In multiplication, using a double-length register, there will not be any overflow –In division, divide overflow is possible  Pentium provides a special software interrupt when a divide overflow occurs  Two instructions as in multiplication div source for unsigned numbers idiv source for signed numbers

6 Dividend is twice the size of the divisor Dividend is assumed to be in  AX (8-bit divisor)  DX:AX (16-bit divisor)  EDX:EAX (32-bit divisor) Division instruction Default Operands:

7 Division instruction

8 Example mov AX,251 mov CL,12 div CL produces 20D in AL and 11D as remainder in AH Example sub DX,DX ; clear DX mov AX,141BH ; AX = 5147D mov CX,012CH ; CX = 300D div CX produces 17D in AX and 47D as remainder in DX Division instruction

9 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)

10 Signed division requires some help »We extended an unsigned 16 bit number to 32 bits by placing zeros in the upper 16 bits »This will not work for signed numbers –To extend signed numbers, you have to copy the sign bit into those upper bit positions  Pentium provides three instructions in aiding sign extension »All three take no operands cbw converts byte to word (extends AL into AH) cwd converts word to doubleword (extends AX into DX) cdq converts doubleword to quadword (extends EAX into EDX) Division instruction

11 IDIV examples Example: 32-bit division of –48 by 5 mov eax,-48 cdq ; extend EAX into EDX mov ebx,5 idiv ebx ; EAX = -9, EDX = -3 Example: 16-bit division of –48 by 5 mov ax,-48 cwd; extend AX into DX mov bx,5 idiv bx; AX = -9, DX = -3

12 Example: 8-bit division of –48 by 5 mov al,-48 cbw ; extend AL into AH mov bl,5 idiv bl ; AL = -9, AH = -3

13 Example mov AL,-95 cbw ; AH = FFH mov CL,12 idiv CL produces  7D in AL and  11D as remainder in AH Example mov AX,-5147 cwd ; DX := FFFFH mov CX,300 idiv CX produces  17D in AX and  47D as remainder in DX

14 Example : write a code to compute the (8!) (Factorial) Fact = 1 For ( I = 8 ; I < 1 ; i--) fact *= i Mov CX,8 ; Cx is a counter Mov AX,1 ; Multiplication instruction uses DX:AX register pairs Mov DX,0 ; dx used to hold our factorial product ; initially set it to 1 Myloop: Mul CX ; DX:AX=AX*CX Dec CX Cmp CX,0 jne myloop

15 Example: var4 = (var1 + var2) * var3 mov eax,var1 add eax,var2 mul var3 jo TooBig; check for overflow mov var4,eax; save product Example: eax = (-var1 * var2) + var3 mov eax,var1 neg eax mul var2 jo TooBig; check for overflow add eax,var3

16 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

17 Example: var4 = (var1 * -5) / (-var2 % var3); mov eax,var2; begin right side neg eax cdq ; sign-extend dividend idiv var3 ; EDX = remainder mov ebx,edx ; EBX = right side mov eax,-5 ; begin left side imul var1 ; EDX:EAX = left side idiv ebx ; final division mov var4,eax ; quotient


Download ppt "Multiplication and Division instructions Dr.Hadi AL Saadi."

Similar presentations


Ads by Google