Presentation is loading. Please wait.

Presentation is loading. Please wait.

MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or EAX. MUL r/m8 MUL r/m16 MUL r/m32.

Similar presentations


Presentation on theme: "MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or EAX. MUL r/m8 MUL r/m16 MUL r/m32."— Presentation transcript:

1 MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or EAX. MUL r/m8 MUL r/m16 MUL r/m32

2 MUL Instruction Note that the product is stored in a register (or group of registers) twice the size of the operands. The operand can be a register or a memory operand

3 MUL Instruction MultiplicandMultiplierProduct ALr/m8AX r/m16DX:AX EAXr/m32EDX:EAX

4 MUL Examples Moval, 5h Movbl, 10h Mulbl; AX = 0050h, CF = 0 (no overflow - the Carry flag is 0 because the upper half of AX is zero)

5 MUL Examples.data Val1WORD2000h Val2WORD0100h.code Movax, val1 Mulval2;DX:AX = 00200000h, CF = 1 (Carry flag is 1 because DX is not equal to zero)

6 IMUL Instruction (Signed Multiply) Has the same syntax and uses the same operands as the MUL instruction except that it preserves the sign of the product.

7 IMUL Instruction IMUL sets the Carry and Overflow flags if the high-order product is not a sign extension of the low-order product. Moval, 48 Movbl, 4 Imul bl;AX = 00C0h, OF = 1 AH is not a sign extension of AL, so the Overflow flag is set.

8 IMUL Instruction Mul al, -4 Movbl, 4 Imulbl; AX = FFF0h, OF = 0 AH is a sign extension of AL (the signed result fits within AL), so the Overflow flag is clear.

9 DIV Instruction (Unsigned Divide) Performs 8-, 16-, and 32-bit division on unsigned integers. DIV r/m8 DIV r/m16 DIV r/m32

10 DIV Instruction DividendDivisorQuotientRemainder AXr/m8ALAH DX:AXr/m16AXDX EDX:EAXr/m32EAXEDX

11 DIV Instruction dividendEDXEAX (quotient) ___________________________= divisor r/m32 EDX(remainder)

12 DIV Examples 8-bit Unsigned Division Movax,0083h;dividend Movbl, 2h;divisor Divbl; AL = 41h, AH = 01h Quotient is 41h, remainder is 1

13 DIV Examples Movdx,0;clear dividend, high Movax, 8003h;dividend, low Movcx, 100h;divisor Divcx;ax = 0080h, dx = 0003h Quotient = 80h, remainder = 3

14 Signed Integer Division CBW – convert Byte to word – Extends the sign bit of AL into the AH register.data BytevalSBYTE-65.code moval,byteval; AL = 9Bh cbw; AX = FF9Bh

15 Sign Extension Instructions CBW –-Convert byte to word CWD –Convert word to double CDQ –-Convert double to quadword

16 IDIV Instruction (Signed Division) Performs signed integer division, using the same operands as the DIV instruction The dividend must be sign-extended into the high order register before IDIV executes.

17 IDIV Examples.data BytevalSBYTE-48.code moval, byteval;dividend cbw;extend AL into AH movbl, 5;divisor idivbl;AL = -9, AH = -3

18 Divide Overflow If the quotient is too large to fit into the destination operand, a divide overflow results. This causes a CPU interrupt, and the current program halts. Movax, 1000h Movbl, 10h Divbl;AL cannot hold 100h

19 DIV Overflow Can use 16-bit divisor to reduce the possibility of divide overflow. Movax, 1000h Movdx, 0;clear DX Movbx, 10h Divbx;AX = 0100h

20 Dividing by Zero Put a check of the divisor to compare to zero. If divisor is zero, jump to an error return and skip the code with the divide.

21 Implementing Arithmetic Expressions Implement Var4 = (Var1 + Var2) * Var3 Moveax,var1 Addeax, var2 Mulvar3;EAX = EAX*Var3 Movvar4, eax

22 Extended Addition How do you add 128-bit integers in C? How do you add 128-bit integers in Assembly? ADC (Add with Carry) –Adds both a source operand and the contents of the carry flag to a destination operand. Movdl,0 Moval, 0FFh Addal, 0FFh;AL = FEh ADCdl,0;DL = 1

23 Subtract with Borrow SBB –Subtracts both a source operand and the value of the carry flag from a destination operand. Movedx, 1;upper half Moveax, 0;lower half Subeax,1;subtract 1 Sbbedx,0;subtract upper half

24 Adding ASCII Digits ASCII values can be added digit by digit. It is slow, but allows processing of large numbers. The high 4 bits of an unpacked decimal integer are always zero – they are always 0011 with ASCII

25 Necessary adjustments AAA (ASCII Adjust after Addition) Adjust the binary result of an ADD or ADC instruction. Movah,0 Moval, ‘8’;AX = 0038h Addal, ‘2’;AX = 006Ah Aaa;AX = 0100h Orax, 3030h;AX = 3130h = ’10’

26 Related Instructions AAS –ASCII Adjust after Subtraction AAM –ASCII Adjust after Multiplication AAD –ASCII Adjust after Division

27 Packed Decimal Numbers DAA Instruction –Decimal Adjust after Addtion DAS Instruction –Decimal Adjsut after Subtraction


Download ppt "MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or EAX. MUL r/m8 MUL r/m16 MUL r/m32."

Similar presentations


Ads by Google