Integer Multiplication and Division COE 301 Computer Organization Dr. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University.

Slides:



Advertisements
Similar presentations
Chapter 3 Arithmetic for Computers. Exam 1 CSCE
Advertisements

Arithmetic for Computers
Datorteknik IntegerMulDiv bild 1 MIPS mul/div instructions Multiply: mult $2,$3Hi, Lo = $2 x $3;64-bit signed product Multiply unsigned: multu$2,$3Hi,
CMPE 325 Computer Architecture II
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE Computer Organization Lecture 8 - Multiplication.
Division CPSC 321 Computer Architecture Andreas Klappenecker.
Lecture 15: Computer Arithmetic Today’s topic –Division 1.
CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, ©
Lecture Objectives: 1)Perform binary division of two numbers. 2)Define dividend, divisor, quotient, and remainder. 3)Explain how division is accomplished.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Integer Multiplication and DivisionICS 233 – KFUPM © Muhamed Mudawar slide 1 Multiplicand and HI are sign-extended  Sign is the sign of the result Signed.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
361 div.1 Computer Architecture ECE 361 Lecture 7: ALU Design : Division.
Lecture 9 Sept 28 Chapter 3 Arithmetic for Computers.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 4: Arithmetic / Data Transfer Instructions Partially adapted from Computer Organization.
Arithmetic IV CPSC 321 Andreas Klappenecker. Any Questions?
Integer Multiplication and Division ICS 233 Computer Architecture and Assembly Language Dr. Aiman El-Maleh College of Computer Sciences and Engineering.
Computer Organization Multiplication and Division Feb 2005 Reading: Portions of these slides are derived from: Textbook figures © 1998 Morgan Kaufmann.
Chap 3.3~3.5 Construction an Arithmetic Logic Unit (ALU) Jen-Chang Liu, Spring 2006.
Integer Multiplication and Division
1 Lecture 8: Binary Multiplication & Division Today’s topics:  Addition/Subtraction  Multiplication  Division Reminder: get started early on assignment.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
COE 308: Computer Architecture (T041) Dr. Marwan Abu-Amara Integer & Floating-Point Arithmetic (Appendix A, Computer Architecture: A Quantitative Approach,
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
ECE 232 L9.Mult.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 9 Computer Arithmetic.
Lecture Objectives: 1)Explain the relationship between addition and subtraction with twos complement numbering systems 2)Explain the concept of numeric.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Lec 13Systems Architecture1 Systems Architecture Lecture 13: Integer Multiplication and Division Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan.
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department.
July 2005Computer Architecture, The Arithmetic/Logic UnitSlide 1 Part III The Arithmetic/Logic Unit.
Lecture 6: Multiply, Shift, and Divide
Chapter 3 Arithmetic for Computers (Integers). Florida A & M University - Department of Computer and Information Sciences Arithmetic for Computers Operations.
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number.
Cs 152 l6 Multiply 1 DAP Fa 97 © U.C.B. ECE Computer Architecture Lecture Notes Multiply, Shift, Divide Shantanu Dutt Univ. of Illinois at.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
05/03/2009CA&O Lecture 8,9,10 By Engr. Umbreen sabir1 Computer Arithmetic Computer Engineering Department.
Integer Multiplication and Division
Lecture notes Reading: Section 3.4, 3.5, 3.6 Multiplication
Integer Multiplication and Division ICS 233 Computer Architecture and Assembly Language Dr. Aiman El-Maleh College of Computer Sciences and Engineering.
Csci 136 Computer Architecture II – Multiplication and Division
Mohamed Younis CMCS 411, Computer Architecture 1 CMSC Computer Architecture Lecture 11 Performing Division March 5,
EI 209 Chapter 3.1CSE, 2015 EI 209 Computer Organization Fall 2015 Chapter 3: Arithmetic for Computers Haojin Zhu ( )
Division Check for 0 divisor Long division approach – If divisor ≤ dividend bits 1 bit in quotient, subtract – Otherwise 0 bit in quotient, bring down.
CDA 3101 Spring 2016 Introduction to Computer Organization
Division Quotient Divisor Dividend – – Remainder.
Integer Multiplication, Division Arithmetic shift Twice the number of places MIPS multiply unit. mult, multu Significant bits Mfhi, mflo, div, divu Arithmetic.
Integer Operations Computer Organization and Assembly Language: Module 5.
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
Integer Multiplication and Division ICS 233 Computer Architecture & Assembly Language Prof. Muhamed Mudawar College of Computer Sciences and Engineering.
More Binary Arithmetic - Multiplication
Computer Architecture & Operations I
Computer Architecture & Operations I
Integer Multiplication and Division
MIPS mul/div instructions
Single Bit ALU 3 R e s u l t O p r a i o n 1 C y I B v b 2 L S f w d O
Morgan Kaufmann Publishers
Morgan Kaufmann Publishers
Lecture 8: Binary Multiplication & Division
Multiplication & Division
CDA 3101 Summer 2007 Introduction to Computer Organization
CDA 3101 Spring 2016 Introduction to Computer Organization
CS352H: Computer Systems Architecture
Lecture 8: Addition, Multiplication & Division
King Fahd University of Petroleum and Minerals
Lecture 8: Addition, Multiplication & Division
Topic 3c Integer Multiply and Divide
CDA 3101 Summer 2007 Introduction to Computer Organization
Morgan Kaufmann Publishers Arithmetic for Computers
MIPS Arithmetic and Logic Instructions
MIPS Arithmetic and Logic Instructions
Presentation transcript:

Integer Multiplication and Division COE 301 Computer Organization Dr. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals

Presentation Outline  Unsigned Integer Multiplication  Signed Integer Multiplication  Faster Integer Multiplication  Integer Division  Integer Multiplication and Division in MIPS

 Paper and Pencil Example: Multiplicand = 12 Multiplier × = Product = 156  m-bit multiplicand × n-bit multiplier = (m+n)-bit product  Accomplished via shifting and addition  Consumes more time and more chip area than addition Unsigned Integer Multiplication Binary multiplication is easy 0 × multiplicand = 0 1 × multiplicand = multiplicand

Sequential Unsigned Multiplication  Initialize Product = 0  Check each bit of the Multiplier  If Multiplier bit = 1 then Product = Product + Multiplicand  Rather than shifting the multiplicand to the left Instead, Shift the Product to the Right Has the same net effect and produces the same result Minimizes the hardware resources  One cycle per iteration (for each bit of the Multiplier)  Addition and shifting can be done simultaneously

 Initialize HI = 0  Initialize LO = Multiplier  Final Product = HI and LO registers  Repeat for each bit of Multiplier Sequential Multiplication Hardware = 0 Start LO[0]? HI = HI + Multiplicand 32 nd Repetition? Done = 1 No Yes HI = 0, LO=Multiplier Shift (Carry, HI, LO) Right 1 bit 32-bit ALU Control 64 bits 32 bits write add LO[0] Multiplicand shift right 32 bits HILO 32 bits carry

Sequential Multiplier Example  Consider: × , Product =  4-bit multiplicand and multiplier are used in this example  4-bit adder produces a 5-bit sum (with carry) Shift Right (Carry, HI, LO) by 1 bit LO[0] = 0 => Do Nothing Shift Right (Carry, HI, LO) by 1 bit Shift Right (Carry, HI, LO) by 1 bit Shift Right (Carry, HI, LO) by 1 bit Initialize (HI = 0, LO = Multiplier) MultiplicandProduct = HI, LOCarryIteration LO[0] = 1 => ADD LO[0] = 1 => ADD LO[0] = 1 => ADD +

Next...  Unsigned Integer Multiplication  Signed Integer Multiplication  Faster Integer Multiplication  Integer Division  Integer Multiplication and Division in MIPS

Signed Integer Multiplication  So far, we have dealt with unsigned integer multiplication  First Attempt:  Convert multiplier and multiplicand into positive numbers  If negative then obtain the 2's complement and remember the sign  Perform unsigned multiplication  Compute the sign of the product  If product sign < 0 then obtain the 2's complement of the product  Better Version:  Use the unsigned multiplication hardware  When shifting right, extend the sign of the product  If multiplier is negative, the last step should be a subtract

Signed Multiplication (Pencil & Paper)  Case 1: Positive Multiplier Multiplicand = -4 Multiplier × = Product = -20  Case 2: Negative Multiplier Multiplicand = -4 Multiplier × = (2's complement of 1100) Product = +12 Sign-extension

 ALU produces 32-bit result + Sign bit  Check for overflow  No overflow  Extend sign-bit of result  Overflow  Invert sign bit Sequential Signed Multiplier = 0 Start LO[0]? First 31 iterations: HI = HI + Multiplicand Last iteration: HI = HI – Multiplicand 32 nd Repetition? Done = 1 No Yes HI = 0, LO = Multiplier Shift Right (Sign, HI, LO) 1 bit 32-bit ALU Control 64 bits 32 bits write add, sub LO[0] Multiplicand shift right 32 bits HILO 32 bits sign

Signed Multiplication Example  Consider: (-4) × (-3), Product =  Check for overflow: No overflow  Extend sign bit  Last iteration: add 2's complement of Multiplicand Shift (Sign, HI, LO) right 1 bit LO[0] = 0 => Do Nothing Shift (Sign, HI, LO) right 1 bit Shift (Sign, HI, LO) right 1 bit Shift (Sign, HI, LO) right 1 bit Initialize (HI = 0, LO = Multiplier) MultiplicandProduct = HI, LOSignIteration LO[0] = 1 => ADD LO[0] = 1 => ADD LO[0] = 1 => SUB (ADD 2's compl) +

Next...  Unsigned Integer Multiplication  Signed Integer Multiplication  Faster Integer Multiplication  Integer Division  Integer Multiplication and Division in MIPS

Faster Integer Multiplier  Uses Multiple Adders (Cost vs. Performance) Can be pipelined

Using Multiple Adders  32-bit adder for each bit of the multiplier  AND multiplicand with each bit of multiplier  Product = accumulated shifted sum  Each adder produces a 33-bit output  Most significant bit is a carry bit  Array multiplier can be optimized  Additions can be done in parallel  Multiple-level tree reduction to produce final product  Carry save adders reduce delays

Carry Save Adders  Used when adding multiple numbers (as in multipliers)  All the bits of a carry-save adder work in parallel  The carry does not propagate as in a carry-propagate adder  This is why a carry-save is faster than a carry-propagate adder  A carry-save adder has 3 inputs and produces two outputs  It adds 3 numbers and produces partial sum and carry bits Carry-Propagate Adder + a0a0 b0b0 s0s0 + a1a1 b1b1 s1s1 + a 31 b 31 s c out c in Carry-Save Adder... + a 31 b 31 s' 31 c' 31 c 31 + a1a1 b1b1 s' 1 c' 1 c1c1 + a0a0 b0b0 s' 0 c' 0 c0c0

Tree Multiplier - 1 of 2  Suppose we want to multiply two numbers A and B  Example on 4-bit numbers: A = a 3 a 2 a 1 a 0 and B = b 3 b 2 b 1 b 0  Step 1: AND (multiply) each bit of A with each bit of B  Requires n 2 AND gates and produces n 2 product bits  Position of a i b j = (i+j). For example, Position of a 2 b 3 = 2+3 = 5 a0b0a0b0 a1b0a1b0 a2b0a2b0 a3b0a3b0 a0b1a0b1 a1b1a1b1 a2b1a2b1 a3b1a3b1 a0b2a0b2 a1b2a1b2 a2b2a2b2 a3b2a3b2 a0b3a0b3 a1b3a1b3 a2b3a2b3 a3b3a3b3 A × B

Tree Multiplier – 2 of 2 Step 2: Use carry save adders to add the partial products  Reduce the partial products to just two numbers Step 3: Add last two numbers using a carry-propagate adder P0P0 + a 2 b 0 a 1 b a 3 b 0 a 2 b 1 a0b0a0b0 + a 1 b 0 a 0 b a 3 b 1 a 2 b 2 + a3b2a3b2 a2b3a2b3 a3b3a3b3 a1b3a1b3 a0b3a0b3 a1b2a1b2 a0b2a0b2 P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P7 Carry Save Adder Carry Propagate Adder

Next...  Unsigned Integer Multiplication  Signed Integer Multiplication  Faster Integer Multiplication  Integer Division  Integer Multiplication and Division in MIPS

Try to see how big a number can be subtracted, creating a digit of the quotient on each attempt = 19 Quotient Divisor = 217 Dividend = 8 Remainder Unsigned Division (Paper & Pencil) Binary division is accomplished via shifting and subtraction Dividend = Quotient × Divisor + Remainder 217 = 19 ×

Sequential Division  Uses two registers: HI and LO  Initialize: HI = Remainder = 0 and LO = Dividend  Shift (HI, LO) LEFT by 1 bit (also Shift Quotient LEFT)  Shift the remainder and dividend registers together LEFT  Has the same net effect of shifting the divisor RIGHT  Compute: Difference = Remainder – Divisor  If (Difference ≥ 0) then  Remainder = Difference  Set Least significant Bit of Quotient  Observation to Reduce Hardware:  LO register can be also used to store the computed Quotient

Sequential Division Hardware  Initialize:  HI = 0, LO = Dividend  Results:  HI = Remainder  LO = Quotient Start Difference? 2.HI = Remainder = Difference Set least significant bit of LO 32 nd Repetition? Done < 0≥ 0 No Yes 1.Shift (HI, LO) Left Difference = HI – Divisor shift left Divisor 32-bit ALU LO 32 bits write sub 32 bits Difference sign set lsb HI 32 bits Control

2: Diff Do Nothing Unsigned Integer Division Example  Example: / (4-bit dividend & divisor)  Result Quotient = and Remainder =  4-bit registers for Remainder and Divisor (4-bit ALU) 2: Rem = Diff, set lsb of LO : Diff Do Nothing Initialize HIDifferenceLOIteration Divisor : Shift Left, Diff = HI - Divisor : Shift Left, Diff = HI - Divisor : Shift Left, Diff = HI - Divisor : Shift Left, Diff = HI - Divisor

Signed Integer Division  Simplest way is to remember the signs  Convert the dividend and divisor to positive  Obtain the 2's complement if they are negative  Do the unsigned division  Compute the signs of the quotient and remainder  Quotient sign = Dividend sign XOR Divisor sign  Remainder sign = Dividend sign  Negate the quotient and remainder if their sign is negative  Obtain the 2's complement to convert them to negative

Signed Integer Division Examples 1.Positive Dividend and Positive Divisor  Example: +17 / +3Quotient = +5Remainder = +2 2.Positive Dividend and Negative Divisor  Example: +17 / –3 Quotient = –5Remainder = +2 3.Negative Dividend and Positive Divisor  Example: –17 / +3Quotient = –5Remainder = –2 4.Negative Dividend and Negative Divisor  Example: –17 / –3Quotient = +5Remainder = –2 The following equation must always hold: Dividend = Quotient × Divisor + Remainder

Next...  Unsigned Integer Multiplication  Signed Integer Multiplication  Faster Multiplication  Integer Division  Integer Multiplication and Division in MIPS

Integer Multiplication in MIPS  Multiply instructions  mult $s1,$s2 Signed multiplication  multu $s1,$s2 Unsigned multiplication  32-bit multiplication produces a 64-bit Product  Separate pair of 32-bit registers  HI = high-order 32-bit of product  LO = low-order 32-bit of product  MIPS also has a special mul instruction  mul $s0,$s1,$s2 $s0 = $s1 × $s2  Put low-order 32 bits into destination register  HI & LO are undefined Multiply Divide $0 HILO $1.... $31

Integer Division in MIPS  Divide instructions  div $s1,$s2 Signed division  divu $s1,$s2 Unsigned division  Division produces quotient and remainder  Separate pair of 32-bit registers  HI = 32-bit remainder  LO = 32-bit quotient  If divisor is 0 then result is unpredictable  Moving data from HI/LO to MIPS registers  mfhi Rd (move from HI to Rd)  mflo Rd (move from LO to Rd) Multiply Divide $0 HILO $1.... $31

Integer Multiply/Divide Instructions InstructionMeaningFormat multRs, Rt Hi, Lo = Rs × Rt op 6 = 0Rs 5 Rt 5 000x18 multuRs, Rt Hi, Lo = Rs × Rt op 6 = 0Rs 5 Rt 5 000x19 mulRd, Rs, Rt Rd = Rs × Rt 0x1cRs 5 Rt 5 Rd 5 00x02 divRs, Rt Hi, Lo = Rs / Rt op 6 = 0Rs 5 Rt 5 000x1a divuRs, Rt Hi, Lo = Rs / Rt op 6 = 0Rs 5 Rt 5 000x1b mfhiRdRd = Hiop 6 = 000Rd 5 00x10 mfloRdRd = Loop 6 = 000Rd 5 00x12  Signed arithmetic: mult, div (Rs and Rt are signed)  LO = 32-bit low-order and HI = 32-bit high-order of multiplication  LO = 32-bit quotient and HI = 32-bit remainder of division  Unsigned arithmetic: multu, divu (Rs and Rt are unsigned)  NO arithmetic exception can occur

Integer to String Conversion  Objective: convert an unsigned 32-bit integer to a string  How to obtain the decimal digits of the number?  Divide the number by 10, Remainder = decimal digit (0 to 9)  Convert decimal digit into its ASCII representation ('0' to '9')  Repeat the division until the quotient becomes zero  Digits are computed backwards from least to most significant  Example: convert 2037 to a string  Divide 2037/10quotient = 203remainder = 7char = '7'  Divide 203/10quotient = 20remainder = 3char = '3'  Divide 20/10quotient = 2remainder = 0char = '0'  Divide 2/10quotient = 0remainder = 2char = '2'

Integer to String Procedure # # int2str: Converts an unsigned integer into a string # Input: $a0 = unsigned integer # In/Out: $a1 = address of string buffer (12 bytes) # int2str: move$t0, $a0# $t0 = dividend = unsigned integer li$t1, 10# $t1 = divisor = 10 addiu$a1, $a1, 11# start at end of string buffer sb$zero, 0($a1)# store a NULL byte convert: divu$t0, $t1# LO = quotient, HI = remainder mflo$t0# $t0 = quotient mfhi$t2# $t2 = remainder addiu$t2, $t2, 0x30# convert digit to a character addiu$a1, $a1, -1# point to previous byte sb$t2, 0($a1)# store digit character bnez$t0, convert# loop if quotient is not 0 jr$ra# return to caller