+ All Categories
Home > Documents > Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Date post: 17-Dec-2015
Category:
Upload: hilda-wood
View: 218 times
Download: 2 times
Share this document with a friend
Popular Tags:
30
Assembly Language Lecture 9 Multiplication and Division Instructions 1
Transcript
Page 1: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Assembly Language

Lecture 9

Multiplication and Division Instructions

1

Page 2: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Signed vs. Unsigned Multiplication

Signed multiplication and unsigned multiplication lead to different results. Consider two bytes:

11111111 and 00000001Signed multiplication of these two numbers gives: 11111111 (-1) * 00000001 (1) = 1111111111111111 (-1)Unsigned multiplication of these two numbers gives:11111111 (255) * 00000001 (1) = 0000000011111111 (255)

REMEMBER: If you multiply an 8-bit number by an 8-bit number, you will get a

16-bit number! If you multiply a 16-bit number by a 16-bit number, you will get a

32-bit number!

2

Page 3: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Signed vs. Unsigned Multiplication

• Thus there is one instruction to multiply signed (IMUL)and another instruction to multiply unsigned (MUL).

• These instructions execute one way if you're working with bytes and they execute in a different way if you're working with words.

3

Page 4: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

MUL and IMUL InstructionsUnsigned Multiplication

◦ Syntax: MUL sourceSigned Multiplication

◦ IMUL sourceByte Form

◦ The multiplier is contained in the source field and the multiplicand is assumed to be in AL. The 16-bit product will be in AX. The source may be a byte register or a memory byte. It cannot be a constant.

Word Form◦ The multiplier is contained in the source field and the multiplicand is

assumed to be in AX. The 32 bit product will be split over two registers. The most significant 16 bits will be in DX and the least significant bits will be in AX (This is referred to as DX:AX). The source may be a word register or a memory word. It cannot be a constant.

4

Page 5: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

MUL and IMUL Instructions

Effect of MUL/IMUL on the status flags:

• SF,ZF,AF,PF : undefined• CF / OF : If the product is too big to fit in the lower half of the

destination (AL for byte multiplication and AX for word multiplication), the CF/OF will be set to 1.

If the upper half of the result on a MUL is a zero or the upper half of the result on an IMUL is the sign extension of the lower half, the CF/OF will be set to 0.

5

Page 6: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Examples Example 1

Before the instruction executes:DX:???? AX: 0002 BX: 0010 (16) CF/OF:?

IMUL BX multiplies the contents of AX by BX placing the 32 bit answer in DX:AX

After the instruction executes:DX:0000 AX: 0020 (32) BX: 0010 CF/OF:0 (Result fits in AX)

Example 2Before the instruction executes:DX:???? AX: 0003 BX: 7000 (28672) CF/OF:?

IMUL BX multiplies the contents of AX by BX

placing the 32 bit answer in DX:AX After the instruction executes:

DX:0001 AX: 5000 BX: 7000 CF/OF:1 (Result [86016] does not fit in AX) 6

Page 7: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Examples

Example 3Before:

AL: 30 (48) BL: 02 CF/OF:?IMUL BL multiplies the contents of AL by BL placing the 16 bit answer in AX

After:AX: 0060 (96) BL: 02 CF/OF:0 (Result fits in AL)

Example 4Before:

AL: 80 (-128) BL: 02 CF/OF:?IMUL BL multiplies the contents of AL by BL placing the 16 bit answer in AX

After:AX: FF00(-256) BL: 02 CF/OF:1 (High 8 bits are not the signed extension of the lower half)

7

Page 8: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Examples (cont’d)

• Example 5Before: AL: 80 (-128) BL: FF (-1) CF/OF:?

IMUL BL multiplies the contents of AL by BL placing

the 16 bit answer in AXAfter: AX: 0080 (128) BL: FF CF/OF:1

(High 8 bits are not the signed sense signed extension of the lower half)

• Example 6Before: AL: 70 (112) BL: 02 CF/OF:?MUL BL multiplies the contents of AL by BL placing

the 16 bit answer in AX

After: AX: 00E0 (224) BL: 02 CF/OF:0 (High 8 bits are zero)

8

Page 9: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Examples (cont’d)

• Example 7Before:

AL: A0 (160) BL: 02 CF/OF:?MUL BL multiplies the contents of

AL by BL placing the 16 bit answer

in AX After:

AX: 0140 (320) BX: 02 CF/OF:1 (High 8 bits are not zero)

9

Page 10: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Problem 1Translate into ASSMBLER the following high-level assignment

statement: ANSWER = 13 * A + B * C - Dwhere ANSWER, A, and D are words in memory and B and C are bytes in memory. Assume no overflow and use signed multiplication.

MOV AX,13 ;move constant to regIMUL A ;assume A*13 in AX

MOV ANSWER,AX ;ANSWER=13*AMOV AL,BIMUL C ;assume B*C in AXADD ANSWER,AX ;ANSWER=13*A+B*CMOV BX,D ;Put D in register so you

can ;subtract it from ANSWERSUB ANSWER,BX ;ANSWER=13*A+B*C-D

10

Page 11: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Problem 2

Write a procedure to calculate N! Remember N! = 1*2*3*...* N-1 * NFor example, 4! = 1*2*3*4 = 24

0! and 1! are defined to be 1 and the factorial for a negative number is undefined and should be returned as 0.

11

Page 12: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

; compute N!; input : CX = N;output: AX = N! MOV AX,1 ; AX holds productTOP: MUL CX ; product = product X term LOOP TOP

Solution

12

Page 13: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Signed and Unsigned Division

• When any kind of division is performed, two results are obtained - the quotient and the remainder.

• The effect of the division instructions on the flags register is that all status flags are undefined after a divide has taken place !!!

13

Page 14: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Signed and Unsigned Division

• Unsigned Division Instruction– Syntax: DIV divisor

• Signed Division Instruction– Syntax: IDIV divisor

• These instructions divide 8 (or 16) bits into 16 (or 32) bits. The quotient and remainder will be the same size as the divisor.

• Byte Form– The divisor is an 8-bit register or memory byte. The 16-bit

dividend is assumed to be in AX. After the division the 8 bit quotient is in AL and the 8-bit remainder is in AH. The divisor cannot be a constant!

14

Page 15: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Signed and Unsigned Division • Word Form

– The divisor is 16-bit register or memory word. The 32 bit dividend is assumed to be in DX:AX. After the division, the 16 bit quotient is in AX and the 16-bit remainder is in DX. The divisor cannot be a constant!

• The divide instruction does not set the status flags to any particular settings.

• If the quotient will not fit into AL or AX, the system will display "DIVIDE OVERFLOW".

15

Page 16: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Signed and Unsigned Division

• The sign of the quotient will be determined by the laws of algebra. The sign of the remainder will be the sign of the original dividend.

That is:

QUOTIENT REMAINDER

+/+ = + +/+ = + +/- = - +/- = + -/+ = - -/+ = - -/- = + -/- = -

16

Page 17: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Signed and Unsigned Division To set to divide one word by another word1. Place the dividend in AX2. For DIV, clear DX (XOR DX,DX will clear DX)3. For IDIV, extend the sign of the value in AX to DX by issuing the CWD

instruction (Convert a Word to a Doubleword).

To set up to divide one byte by another byte1. Place the dividend in AL2. For DIV, clear AH (XOR AH,AH will clear AH)3. For IDIV, extend the sign of the value in AL to AH by issuing

the CBW instructions (Convert a Byte to a Word).

17

Page 18: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

CWD Instruction

• Converts the signed 16-bit number in AX into a signed 32-bit number in DX:AX

• NOTE: Use this instruction only with signed arithmetic!!• Syntax: CWD• If bit 15 of AX is a 1, the instruction will place FFFF in DX.• If bit 15 of AX is a 0, the instruction will place 0000 in DX.• No flags are affected by this instruction.• EXAMPLES:• Before:

DX:???? AX:7000 (28672)CWD converts the signed 16-bit number

in AX to a 32-bit signed number in DX:AX

• After:DX:0000 AX:7000 (28672)

18

Page 19: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

CWD Instruction

• Example 2:Before the instruction executes:

DX:???? AX:8000 (-32768)CWD converts the signed 16-bit number

in AX to a 32-bit signed number in DX:AX

After the instruction executes:DX:FFFF AX:8000 (-32768)

19

Page 20: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

CBW Instruction

• This instruction converts the signed 8-bit number in AL into a signed 16-bit number in AX

• NOTE: Use this instruction only with signed arithmetic!!• Syntax: CBW

If bit 7 of AL is a 1, the instruction will place FF in AH. If bit 7 of AL is a 0, the instruction will place 00 in AH.

No flags are affected by this instruction.

• Examples:Before: AX:??40 (64)

CBW converts the signed 8-bit number in AL to a

16-bit signed number in AX After: AX:0040 (64)

20

Page 21: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

CBW Instruction

• Example 2• Before:AX:??FC (-4)

CBW converts the signed 8-bit number in AL

to a 16-bit signed number in AX

• After: AX:FFFC (-4)

21

Page 22: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

DIV and IDIV Examples

• Example 1

MOV AX,9CWDThus, before the instruction executes:

DX:0000 AX: 0009 BX: 0002

IDIV BX divides the contents of DX:AX by BX placing

the quotient in AX and the remainder in DX

After the instruction executes:DX:0001 AX: 0004 BX: 0002

22

Page 23: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

DIV and IDIV Examples

• Example 2

MOV AX,9CWD

Thus, before the instruction executes:DX:0000 AX: 0009 BX: FFFE (-2)

IDIV BX divides the contents of DX:AX by BX placing

the quotient in AX and the remainder in DX After the instruction executes:

DX:0001 AX: FFFC (-4) BX: FFFE

23

Page 24: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

DIV and IDIV Examples

• Example 3Before the instruction executes:

AX: 0A00 (2560) BL: 02

IDIV BL divides the contents of AX by BL placing the

quotient in AL and the remainder in AH

As the instruction executes, the system will display on the screen: DIVIDE OVERFLOW

because the quotient (1280 or 500h) will not fit into AL

24

Page 25: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

DIV and IDIV Examples

• Example 4MOV AX,9XOR DX,DX

Thus, before the instruction executes:DX:0000 AX: 0009 BX: FFFE (65534)

DIV BX divides the contents of DX:AX by BX placing

the quotient in AX and the remainder in DX

After the instruction executes:

DX:0009 AX: 0000 BX: FFFE

25

Page 26: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

DIV and IDIV Examples

• Example 5Before the instruction executes:

AX: FFFF (65535) BL: 02

DIV BL divides the contents of AX by BL placing the

quotient in AL and the remainder in AH

As the instruction executes, the system will display on the screen:

DIVIDE OVERFLOW

because the quotient (32767 or 7FFFh) will not fit into AL

26

Page 27: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

DIV and IDIV Examples

• Example 6Divide -1250 by 7

AX: -1250 BX: 7

CODE :

MOV AX,-1250 ; AX gets dividendCWD ;Extend sign to DXMOV BX, 7 ;BX has divisorIDIV BX ;AX gets quotient, DX has remainder

27

Page 28: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

DIV and IDIV Examples

• Example 7Divide the signed value of the byte variable XBYTE by -7

AL: XBYTE BL: -7

CODE :

MOV AL, XBYTE ;AL has dividendCBW ;Extend sign to AHMOV BL, -7 ;BL has divisorIDIV BL ;AL has quotient, AH has remainder

28

Page 29: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Problem

• Translate into ASSMBLER the following high-level

assignment statement: ANSWER = 130 / A + B / C - D / 7

where ANSWER, A, and D are words in memory and B and C are bytes in memory. Assume no overflow and use signed multiplication.

MOV AX,130 ;move constant to regCWD ;expand to DX:AX

IDIV A ;assume QUOTIENT 130/A in AX

MOV ANSWER,AX ;ANSWER=130/AMOV AL,BCBW ;expand to AXIDIV C ;assume quotient B/C in AX

29

Page 30: Assembly Language Lecture 9 Multiplication and Division Instructions 1.

Problem (cont’d)

CBW ;convert byte quotient in AL to a word

ADD ANSWER,AX ;ANSWER=130/A + B/CMOV AX,DMOV BL,7 ;put constant in regIDIV BL ;assume quotient D/7 in ALCBW ;expand to AX so you can SUB from ANSWERSUB ANSWER,AX ;ANSWER=130/A + B/C - D/7

30


Recommended