Lecture 7 Multiplication and Division Instructions 1 Signed

  • Slides: 30
Download presentation
Lecture 7 Multiplication and Division Instructions 1

Lecture 7 Multiplication and Division Instructions 1

Signed vs. Unsigned Multiplication Signed multiplication and unsigned multiplication lead to different results. Consider

Signed vs. Unsigned Multiplication Signed multiplication and unsigned multiplication lead to different results. Consider two bytes: 1111 and 00000001 Signed multiplication of these two numbers gives: 1111 (-1) * 00000001 (1) = 11111111 (-1) Unsigned multiplication of these two numbers gives: 1111 (255) * 00000001 (1) = 00001111 (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

Signed vs. Unsigned Multiplication • Thus there is one instruction to multiply signed (IMUL)and

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

MUL and IMUL Instructions Unsigned Multiplication ◦ Syntax: MUL source Signed Multiplication ◦ IMUL

MUL and IMUL Instructions Unsigned Multiplication ◦ Syntax: MUL source Signed Multiplication ◦ IMUL source Byte 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

MUL and IMUL Instructions Effect of MUL/IMUL on the status flags: • SF, ZF,

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

Examples Example 1 Before the instruction executes: DX: ? ? AX: 0002 IMUL BX:

Examples Example 1 Before the instruction executes: DX: ? ? AX: 0002 IMUL BX: 0010 (16) CF/OF: ? 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 2 Before the instruction executes: DX: ? ? AX: 0003 BX: 7000 (28672) IMUL BX CF/OF: ? 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

Examples Example 3 Before: AL: 30 (48) BL: 02 CF/OF: ? IMUL BL multiplies

Examples Example 3 Before: 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 4 Before: AL: 80 (-128) IMUL BL BL: 02 CF/OF: ? multiplies the contents of AL by BL placing the 16 bit answer in AX After: AX: FF 00(-256) BL: 02 CF/OF: 1 (High 8 bits are not the signed extension of the lower half) 7

Examples (cont’d) • Example 5 Before: AL: 80 (-128) IMUL BL BL: FF (-1)

Examples (cont’d) • Example 5 Before: AL: 80 (-128) IMUL BL BL: FF (-1) CF/OF: ? multiplies the contents of AL by BL placing the 16 bit answer in AX After: AX: 0080 (128) BL: FF CF/OF: 1 (High 8 bits are not the signed sense signed extension of the lower half) • Example 6 Before: 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: 00 E 0 (224) BL: 02 CF/OF: 0 (High 8 bits are zero) 8

Examples (cont’d) • Example 7 Before: AL: A 0 (160) MUL BL BL: 02

Examples (cont’d) • Example 7 Before: AL: A 0 (160) MUL BL BL: 02 CF/OF: ? 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

Problem 1 Translate into ASSMBLER the following high-level assignment statement: ANSWER = 13 *

Problem 1 Translate into ASSMBLER the following high-level assignment statement: ANSWER = 13 * A + B * C - D 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 IMUL ADD MOV SUB AX, 13 A ANSWER, AX AL, B C ANSWER, AX BX, D ANSWER, BX ; move constant to reg ; assume A*13 in AX ; ANSWER=13*A ; assume B*C in AX ; ANSWER=13*A+B*C ; Put D in register so you can ; subtract it from ANSWER ; ANSWER=13*A+B*C-D 10

Problem 2 Write a procedure to calculate N! Remember N! = 1*2*3*. . .

Problem 2 Write a procedure to calculate N! Remember N! = 1*2*3*. . . * N-1 * N For 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

Solution ; compute N! ; input : CX = N ; output: AX =

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

Signed and Unsigned Division • When any kind of division is performed, two results

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

Signed and Unsigned Division • Unsigned Division Instruction – Syntax: DIV divisor • Signed

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

Signed and Unsigned Division • Word Form – The divisor is 16 -bit register

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

Signed and Unsigned Division • The sign of the quotient will be determined by

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

Signed and Unsigned Division To set to divide one word by another word 1.

Signed and Unsigned Division To set to divide one word by another word 1. Place the dividend in AX 2. 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 byte 1. 2. 3. Place the dividend in AL For DIV, clear AH (XOR AH, AH will clear AH) For IDIV, extend the sign of the value in AL to AH by issuing the CBW instructions (Convert a Byte to a Word). 17

CWD Instruction • Converts the signed 16 -bit number in AX into a signed

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

CWD Instruction • Example 2: Before the instruction executes: DX: ? ? AX: 8000

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

CBW Instruction • This instruction converts the signed 8 -bit number in AL into

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: After: AX: ? ? 40 (64) CBW converts the signed 8 -bit number in AL to a 16 -bit signed number in AX AX: 0040 (64) 20

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

CBW Instruction • Example 2 • Before: AX: ? ? FC (-4) CBW • After: converts the signed 8 -bit number in AL to a 16 -bit signed number in AX AX: FFFC (-4) 21

DIV and IDIV Examples • Example 1 MOV AX, 9 CWD Thus, before the

DIV and IDIV Examples • Example 1 MOV AX, 9 CWD Thus, before the instruction executes: DX: 0000 AX: 0009 IDIV BX BX: 0002 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

DIV and IDIV Examples • Example 2 MOV AX, 9 CWD Thus, before the

DIV and IDIV Examples • Example 2 MOV AX, 9 CWD Thus, before the instruction executes: DX: 0000 AX: 0009 IDIV BX BX: FFFE (-2) 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

DIV and IDIV Examples • Example 3 Before the instruction executes: AX: 0 A

DIV and IDIV Examples • Example 3 Before the instruction executes: AX: 0 A 00 (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 500 h) will not fit into AL 24

DIV and IDIV Examples • Example 4 MOV AX, 9 XOR DX, DX Thus,

DIV and IDIV Examples • Example 4 MOV AX, 9 XOR DX, DX Thus, before the instruction executes: DX: 0000 AX: 0009 DIV BX BX: FFFE (65534) 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

DIV and IDIV Examples • Example 5 Before the instruction executes: AX: FFFF (65535)

DIV and IDIV Examples • Example 5 Before 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 7 FFFh) will not fit into AL 26

DIV and IDIV Examples • Example 6 Divide -1250 by 7 AX: -1250 BX:

DIV and IDIV Examples • Example 6 Divide -1250 by 7 AX: -1250 BX: 7 CODE : MOV AX, -1250 ; AX gets dividend CWD ; Extend sign to DX MOV BX, 7 ; BX has divisor IDIV BX ; AX gets quotient, DX has remainder 27

DIV and IDIV Examples • Example 7 Divide the signed value of the byte

DIV and IDIV Examples • Example 7 Divide the signed value of the byte variable XBYTE by -7 AL: XBYTE BL: -7 CODE : MOV AL, XBYTE CBW MOV BL, -7 IDIV BL ; AL has dividend ; Extend sign to AH ; BL has divisor ; AL has quotient, AH has remainder 28

Problem • Translate into ASSMBLER the following high-level assignment statement: ANSWER = 130 /

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 division MOV CWD IDIV MOV CBW IDIV AX, 130 A ANSWER, AX AL, B C ; move constant to reg ; expand to DX: AX ; assume QUOTIENT 130/A in AX ; ANSWER=130/A ; expand to AX ; assume quotient B/C in AX 29

Problem (cont’d) CBW ; convert byte quotient in AL to a word ADD ANSWER,

Problem (cont’d) CBW ; convert byte quotient in AL to a word ADD ANSWER, AX ; ANSWER=130/A + B/C MOV AX, D MOV BL, 7 ; put constant in reg IDIV BL ; assume quotient D/7 in AL CBW ; expand to AX so you can SUB from ANSWER SUB ANSWER, AX ; ANSWER=130/A + B/C - D/7 30