Multiplication and Division Instructions

Slides:



Advertisements
Similar presentations
Introduction to Computer Engineering by Richard E. Haskell Multiplication and Division Instructions Module M16.4 Section 10.4.
Advertisements

NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
1 x86’s instruction sets. 2 Instruction Set Classification  Transfer Move  Arithmetic Add / Subtract Mul / Div, etc.  Control Jump Call / Return, etc.
Department of Computer Science and Software Engineering
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
Assembly Language Lecture 9
1 Multiplication, Division, and Numerical Conversions Chapter 6.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
1 Lecture 7 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Shift and Rotate Instructions
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Assembly Language for Intel-Based Computers
CE302 Outline Multiplication Division Program Segment Prefix Command Line Parameters.
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.
Assembly Language – Lab 5
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
ASCII and BCD Arithmetic Chapter 11 S. Dandamudi.
Multiplication and Division Instructions & the 0Ah function.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Integer Arithmetic Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24 with slides by Kip Irvine.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Chapter 7: Integer Arithmetic. 2 Chapter Overview Shift and Rotate Instructions Shift and Rotate Applications Multiplication and Division Instructions.
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
The Assemble, Unassemble commands of the debugger: U Command for converting machine code language source Equivalent machine code instructions Equivalent.
Introduction Arithmetic instructions are used to perform arithmetic operation such as Addition Subtraction Multiplication Division These operations can.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Multiplication and Division instructions Dr.Hadi AL Saadi.
Data Transfers, Addressing, and Arithmetic
Assembly Language for Intel-Based Computers, 5th Edition
ICS312 SET 7 Flags.
Basic Assembly Language
Multiplication and Division Instructions
Assembly Language Programming Part 2
CS-401 Computer Architecture & Assembly Language Programming
Processor Processor characterized by register set (state variables)
4.2 Arithmetic Instructions
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
Symbolic Instruction and Addressing
X86’s instruction sets.
Introduction to Assembly Language
Chapter 4: Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Data Transfers, Addressing, and Arithmetic
Microprocessor and Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Shift & Rotate Instructions)
Multiplication and Division Instructions
Symbolic Instruction and Addressing
Shift & Rotate Instructions)
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
EECE.3170 Microprocessor Systems Design I
Chapter 5 Arithmetic and Logic Instructions
Chapter 6 –Symbolic Instruction and Addressing
Multiplication and Division Instructions
Chapter 8: Instruction Set 8086 CPU Architecture
Division instruction.
Computer Architecture and System Programming Laboratory
Presentation transcript:

Multiplication and Division Instructions Lecture 8

Signed vs. Unsigned Multiplication Signed multiplication and unsigned multiplication lead to different results. Consider two bytes: 11111111 and 00000001 Signed 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!

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.

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.

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.

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

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: 02 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)

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

Examples (cont’d) Example 7 Before: 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)

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

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.

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

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 !!!

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!

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

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 +/+ = + +/+ = + +/- = - +/- = + -/+ = - -/+ = - -/- = + -/- = -

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

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)

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)

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)

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)

IDIV BX divides the contents of DX:AX by BX placing DIV and IDIV Examples Example 1 MOV AX,9 CWD Thus, 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

IDIV BX divides the contents of DX:AX by BX placing DIV and IDIV Examples Example 2 MOV AX,9 CWD 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

IDIV BL divides the contents of AX by BL placing the 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

DIV BX divides the contents of DX:AX by BX placing DIV and IDIV Examples Example 4 MOV AX,9 XOR 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

DIV BL divides the contents of AX by BL placing the 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 7FFFh) will not fit into AL

CODE : 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

CODE : 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 ;AL has dividend CBW ;Extend sign to AH MOV BL, -7 ;BL has divisor IDIV BL ;AL has quotient, AH has remainder

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