MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or...

27
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

Transcript of MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or...

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

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

Page 2: 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.

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   

AL AX EAX

x r/m8 x r/m16 x r/m32__________________ __________________ __________________

DX AX EDX EAXAX

Page 3: 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.

MUL Instruction

Multiplicand Multiplier Product

AL r/m8 AX

AX r/m16 DX:AX

EAX r/m32 EDX:EAX

Page 4: 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.

MUL Examples

Mov al, 5h

Mov bl, 10h

Mul bl ; AX = 0050h, CF = 0

(no overflow - the Carry flag is 0 because the upper half of AX is zero)

Page 5: 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.

MUL Examples

.data

Val1 WORD 2000h

Val2 WORD 0100h

.code

Mov ax, val1

Mul val2 ;DX:AX = 00200000h, CF = 1

(Carry flag is 1 because DX is not equal to zero)

Page 6: 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.

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.

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

IMUL Instruction

• IMUL sets the Carry and Overflow flags if the high-order product is not a sign extension of the low-order product.

Mov al, 48

Mov bl, 4

Imul bl ;AX = 00C0h, OF = 1

AH is not a sign extension of AL, so the Overflow flag is set.

Page 8: 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.

IMUL Instruction

Mul al, -4

Mov bl, 4

Imul bl ; AX = FFF0h, OF = 0

AH is a sign extension of AL (the signed result fits within AL), so the Overflow flag is clear.

Page 9: 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.

DIV Instruction(Unsigned Divide)

• Performs 8-, 16-, and 32-bit division on unsigned integers.

DIV r/m8

DIV r/m16

DIV r/m32

Page 10: 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.

DIV Instruction

Dividend Divisor Quotient Remainder

AX r/m8 AL AH

DX:AX r/m16 AX DX

EDX:EAX r/m32 EAX EDX

Page 11: 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.

DIV Instruction

dividend EDX EAX   EAX (quotient)

  ___________________________ =    

divisor   r/m32   EDX (remainder)

Page 12: 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.

DIV Examples8-bit Unsigned Division

Mov ax,0083h ;dividend

Mov bl, 2h ;divisor

Div bl ; AL = 41h, AH = 01h

Quotient is 41h, remainder is 1

Page 13: 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.

DIV Examples

Mov dx,0 ;clear dividend, high

Mov ax, 8003h ;dividend, low

Mov cx, 100h ;divisor

Div cx ;ax = 0080h, dx = 0003h

Quotient = 80h, remainder = 3

Page 14: 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.

Signed Integer Division

• CBW – convert Byte to word– Extends the sign bit of AL into the AH register

.data

Byteval SBYTE -65

.code

mov al,byteval ; AL = 9Bh

cbw ; AX = FF9Bh

Page 15: 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.

Sign Extension Instructions

• CBW– -Convert byte to word

• CWD– Convert word to double

• CDQ– -Convert double to quadword

Page 16: 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.

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.

Page 17: 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.

IDIV Examples

.data

Byteval SBYTE -48

.code

mov al, byteval ;dividend

cbw ;extend AL into AH

mov bl, 5 ;divisor

idiv bl ;AL = -9, AH = -3

Page 18: 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.

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.

Mov ax, 1000h

Mov bl, 10h

Div bl ;AL cannot hold 100h

Page 19: 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.

DIV Overflow

Can use 16-bit divisor to reduce the possibility of divide overflow.

Mov ax, 1000h

Mov dx, 0 ;clear DX

Mov bx, 10h

Div bx ;AX = 0100h

Page 20: 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.

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.

Page 21: 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.

Implementing Arithmetic Expressions

• ImplementVar4 = (Var1 + Var2) * Var3

Mov eax,var1

Add eax, var2

Mul var3 ;EAX = EAX*Var3

Mov var4, eax

Page 22: 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.

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.

Mov dl,0

Mov al, 0FFh

Add al, 0FFh ;AL = FEh

ADC dl,0 ;DL = 1

Page 23: 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.

Subtract with Borrow

• SBB– Subtracts both a source operand and the

value of the carry flag from a destination operand.

Mov edx, 1 ;upper half

Mov eax, 0 ;lower half

Sub eax,1 ;subtract 1

Sbb edx,0 ;subtract upper half

Page 24: 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.

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

Page 25: 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.

Necessary adjustments

• AAA (ASCII Adjust after Addition)• Adjust the binary result of an ADD or ADC

instruction.

Mov ah,0Mov al, ‘8’ ;AX = 0038hAdd al, ‘2’ ;AX = 006AhAaa ;AX = 0100hOr ax, 3030h ;AX = 3130h = ’10’

Page 26: 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.

Related Instructions

• AAS– ASCII Adjust after Subtraction

• AAM– ASCII Adjust after Multiplication

• AAD– ASCII Adjust after Division

Page 27: 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.

Packed Decimal Numbers

• DAA Instruction– Decimal Adjust after Addtion

• DAS Instruction– Decimal Adjsut after Subtraction