Presentation is loading. Please wait.

Presentation is loading. Please wait.

Morgan Kaufmann Publishers Computer Organization and Assembly Language

Similar presentations


Presentation on theme: "Morgan Kaufmann Publishers Computer Organization and Assembly Language"— Presentation transcript:

1 Morgan Kaufmann Publishers Computer Organization and Assembly Language
November 15, 2018 CS 206 D Computer Organization and Assembly Language Chapter 1 — Computer Abstractions and Technology

2 Lecture 7 Multiplication and Division Instructions

3 Chapter Outline Signed vs. Un signe Multiplication
MUL and IMUL Instructions Signed and Unsigned Division CWD Instruction CBW Instruction DIV and IDIV Examples

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

5 MUL and IMUL Instructions
Unsigned Multiplication MUL source Signed Multiplication IMUL source Byte Form The multiplier is contained in the source field 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. Source cannot be a constant. Word Form 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. Source cannot be a constant

6 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 or 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.

7 Examples Example 1 Example 2 Before the instruction executes:
DX:???? AX: 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: CF/OF:0 (Result fits in AX) Example 2 DX:???? AX: 0003 BX: 7000 (28672) CF/OF:? IMUL BX multiplies the contents of AX by BX DX:0001 AX: BX: CF/OF:1 (Result [86016] does not fit in AX)

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

9 Examples (cont’d) Example 5 Example 6
Before: AL: 80 (-128) BL: FF (-1) CF/OF:? IMUL BL 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: CF/OF:? MUL BL multiplies the contents of AL by BL placing After: AX: 00E0 (224) BL: CF/OF:0 (High 8 bits are zero)

10 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 AX,13 ;move constant to reg IMUL A ;assume A*13 in AX MOV ANSWER,AX ;ANSWER=13*A MOV AL,B IMUL C ;assume B*C in AX ADD ANSWER,AX ;ANSWER=13*A+B*C MOV BX,D ;Put D in register so you can ;subtract it from ANSWER SUB ANSWER,BX ;ANSWER=13*A+B*C-D

11 Solution 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. ; compute N! ; input : CX = N ;output: AX = N! MOV AX, ; AX holds product TOP: MUL CX ; product = product X term LOOP TOP

12 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 : All status flags are undefined after a divide has taken place !!! 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.

13 Signed and Unsigned Division
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! 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!

14 Signed and Unsigned Division
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". 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 as follow: QUOTIENT REMAINDER +/+ = /+ = + +/- = /- = + -/+ = /+ = - -/- = /- = -

15 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. Place the dividend in AL 2. 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

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

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

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

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

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

21 DIV and IDIV Examples Example 3 Before 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

22 DIV and IDIV Examples Example 4 MOV AX,9 XOR DX,DX
Thus, before the instruction executes: DX:0000 AX: 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: BX: FFFE Example 5: Divide by 7 MOV AX, ; AX gets dividend CWD ;Extend sign to DX MOV BX, ;BX has divisor IDIV BX ;AX gets quotient, DX has remainder

23 DIV, IDIV, MUL and IMUL Examples

24 DIV, IDIV, MUL and IMUL Examples

25 Problem Translate into ASSMBLER: 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 AX,130 ;move constant to reg CWD ;expand to DX:AX IDIV A ;assume QUOTIENT 130/A in AX MOV ANSWER,AX ;ANSWER=130/A MOV AL,B CBW ;expand to AX IDIV C ;assume quotient B/C in AX CBW ;convert byte quotient in AL to a word ADD ANSWER,AX ;ANSWER=130/A + B/C MOV AX,D MOV BL, ;put constant in reg IDIV BL ;assume quotient D/7 in AL CBW ;expand to AX so you can SUB from NSWER SUB ANSWER,AX ;ANSWER=130/A + B/C - D/7


Download ppt "Morgan Kaufmann Publishers Computer Organization and Assembly Language"

Similar presentations


Ads by Google