Computer Organization Multiplication and Division Feb 2005 Reading: 3.4-3.5 Portions of these slides are derived from: Textbook figures © 1998 Morgan Kaufmann.

Slides:



Advertisements
Similar presentations
Multiplication and Division
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:
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3: IT Students.
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.
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE VLSI Circuit Design Lecture 24 - Subsystem.
361 div.1 Computer Architecture ECE 361 Lecture 7: ALU Design : Division.
Arithmetic IV CPSC 321 Andreas Klappenecker. Any Questions?
L10 – Multiplication Division 1 Comp 411 – Fall /19/2009 Binary Multipliers ×
Integer Multiplication and Division ICS 233 Computer Architecture and Assembly Language Dr. Aiman El-Maleh College of Computer Sciences and Engineering.
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.
CPSC 321 Computer Architecture ALU Design – Integer Addition, Multiplication & Division Copyright 2002 David H. Albonesi and the University of Rochester.
ECE 232 L9.Mult.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 9 Computer Arithmetic.
Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College.
1 Bits are just bits (no inherent meaning) — conventions define relationship between bits and numbers Binary numbers (base 2)
Chapter 6-2 Multiplier Multiplier Next Lecture Divider
Copyright 1995 by Coherence LTD., all rights reserved (Revised: Oct 97 by Rafi Lohev, Oct 99 by Yair Wiseman, Sep 04 Oren Kapah) IBM י ב מ 10-1 The ALU.
Lec 13Systems Architecture1 Systems Architecture Lecture 13: Integer Multiplication and Division Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan.
07/19/2005 Arithmetic / Logic Unit – ALU Design Presentation F CSE : Introduction to Computer Architecture Slides by Gojko Babić.
Computer Arithmetic II Instructor: Mozafar Bag-Mohammadi Spring 2006 University of Ilam.
1  1998 Morgan Kaufmann Publishers Arithmetic Where we've been: –Performance (seconds, cycles, instructions) –Abstractions: Instruction Set Architecture.
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department.
Multiplication of signed-operands
Lecture 6: Multiply, Shift, and Divide
Computer Arithmetic II Instructor: Mozafar Bag-Mohammadi Ilam University.
Chapter 3 Arithmetic for Computers (Integers). Florida A & M University - Department of Computer and Information Sciences Arithmetic for Computers Operations.
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
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,
Orange Coast College Business Division Computer Science Department CS 116- Computer Architecture Arithmetic: Part II.
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 and Division COE 301 Computer Organization Dr. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University.
Integer Multiplication and Division ICS 233 Computer Architecture & Assembly Language Prof. Muhamed Mudawar College of Computer Sciences and Engineering.
Computer Architecture & Operations I
Computer Architecture & Operations I
Integer Multiplication and Division
MIPS mul/div instructions
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
Lecture 8: Addition, Multiplication & Division
Lecture 8: Addition, Multiplication & Division
CSCE 350 Computer Architecture
Topic 3c Integer Multiply and Divide
Systems Architecture I
CDA 3101 Summer 2007 Introduction to Computer Organization
Computer Arithmetic Multiplication, Floating Point
Reading: Study Chapter (including Booth coding)
October 15 Chapter 4 – Multiplication/Division Go to the State Fair!
October 5 Register to vote! Go to the State Fair! (15-24 October)
Presentation transcript:

Computer Organization Multiplication and Division Feb 2005 Reading: Portions of these slides are derived from: Textbook figures © 1998 Morgan Kaufmann Publishers all rights reserved Tod Amon's COD2e Slides © 1998 Morgan Kaufmann Publishers all rights reserved Dave Patterson’s CS 152 Slides - Fall 1997 © UCB Rob Rutenbar’s Slides - Fall 1999 CMU other sources as noted

Feb 2005Multiplication and Division2 Outline - Multiplication and Division  Multiplication  Review: Shift & Add Multiplication   Review: Booth’s Algorithm  Combinational Multiplication  MIPS Multiplication Instructions  Division  Summary

Feb 2005Multiplication and Division3 Multiplication  Basic algorithm analogous to decimal multiplication  Break multiplier into digits  Multiply one digit at a time; shift multiplicand to form partial products  Create product as sum of partial products  n bit multiplicand X m bit multiplier = (n+m) bit product Multiplicand 0110 (6) Multiplier X 0011 (3) Product (18) Partial Products

Feb 2005Multiplication and Division4 Multiplier Hardware  Sequential  Combinational

Feb 2005Multiplication and Division5 Sequential Multiplier - First Version Multiplicand (64 bits) Shift Left Multiplier (32 bits) Shift Right Product (64 bits) Write Control 64-bit ALU LSB  Multiplicand shifts left  Multiplier shifts right  Sample LSB of multiplier to decide whether to add

Feb 2005Multiplication and Division6 Algorithm - 1st Cut Multiplier START DONE 1. Test Multiplier0 LSB 1a. Add Multiplicand to Product Place result in Product 2. Shift Multiplicand left 1 bit 2. Shift Multiplier right 1 bit 32nd Repitition? Multiplier0=1Multiplier0=0

Feb 2005Multiplication and Division7 Animation - 1st Cut Multiplier Multiplier Product (64 bits) Write Control 64-bit ALU LSB Multiplicand  Multiplicand shifts left  Multiplier shifts right  Sample LSB of multiplier to decide whether to add

Feb 2005Multiplication and Division8 -9x   117

Feb 2005Multiplication and Division9 Sequential Multiplier - 2nd Version  Observation: we’re only adding 32 bits at a time  Clever idea: Why not...  Hold the multiplicand still and…  Shift the product right! Multiplicand (32 bits) Multiplier (32 bits) Shift Right Product (64 bits) Write Control 32-bit ALU LSB Shift Right LHPROD (32 bits) RHPROD (32 bits)

Feb 2005Multiplication and Division10 Algorithm - 2nd Version Multiplier START DONE 1. Test Multiplier0 1a. Add MCND to left half of Product Place result in left half of Product 2. Shift Product right 1 bit 2. Shift Multiplier right 1 bit 32nd Repitition? Multiplier0=1Multiplier0=0 No: <32 Repititions Yes: 32 Repititions

Feb 2005Multiplication and Division11 Multiplicand (32 bits) Product (64 bits) Write Control 32-bit ALU LSB Shift Right Sequential Multiplier - 3nd Version  Observation: we can store the multiplier and product in the same register!  As multiplier shifts out….  Product shifts in LHPROD (32 bits) MPY (initial) (32 bits) MP/RHPROD (32 bits)

Feb 2005Multiplication and Division12 Algorithm - 3rd Version Multiplier START DONE 1. Test PROD0 1a. Add MCND to left half of PROD Place result in left half of PROD 2. Shift PROD right 1 bit 0. LOAD MPY in right half of PROD 32nd Repitition? Product0=1Product0=0 No: <32 Repititions Yes: 32 Repititions

Feb 2005Multiplication and Division13 Outline - Multiplication and Division  Multiplication  Review: Shift & Add Multiplication  Review: Booth’s Algorithm   Combinational Multiplication  MIPS Multiplication Instructions  Division  Summary

Feb 2005Multiplication and Division14 Signed Multiplication with Booth’s Algorithm  Originally proposed to reduce addition steps  Bonus: works for two’s complement numbers  Uses shifting, addition, and subtraction

Feb 2005Multiplication and Division15 Booth’s Algorithm  Observation: if we can both add and subtract, there are multiple ways to create a product  Example: multiply 2 ten by 6 ten (0010 two X 0110 two )  Product = (2 X 2) + (2 X 4) OR  Product = (2 X -2) + (2 X 8) 0010 X shift shift + add shift X shift shift + subtract 0000 shift shift + add Regular Algorithm Booth’s Algorithm

Feb 2005Multiplication and Division16 Booth’s Algorithm Continued  Question:  How do we know when to subtract?  When do we know when to add?  Answer: look for “runs of 1s” in multiplier  Example:  Working from Right to Left, any “run of 1’s” is equal to: - value of first digit that’s one +value of first digit that’s zero  Example : First run: = 3 Second run: = 112 Total: = 115

Feb 2005Multiplication and Division17 Implementing Booth’s Algorithm  Scan multiplier bits from right to left  Recognize the beginning and in of a run looking at only 2 bits at a time  “Current” bit a i  Bit to right of “current” bit a i Beginning Of Run Middle Of Run End Of Run Bit a i Bit a i-1 Explanation 10Begin Run of 1’s 11Middle of Run of 1’s 01End of Run 00Middle of Run of 0’s

Feb 2005Multiplication and Division18 Implementing Booth’s Algorithm  Key idea: test 2 bits of multiplier at once  10 - subtract (beginning of run of 1’s)  01 - add (end of run of 1’s)  00, 11 - do nothing (middle of run of 0’s or 1’s) Multiplicand (32 bits) Product (64 bits) Write Control 32-bit ALU Shift Left ADD/ SUB 2 Bits 1:0 LHPROD (32 bits) MP/RHPROD (32 bits)

Feb 2005Multiplication and Division19 Booth’s Algorithm Example Multiply 4 X X (sub 4 = add -4) (shift after add) (shift without add) (shift without add) (add +4) (shift after add) (sub 4 = add -4) (shift after add) Remember 4 = = = -( ) = -( ) = -36 = 4 X -9! Step 0 Step 1 Step 2 Step 3 Step 4 Step 5 LHProd

Feb 2005Multiplication and Division20 Booth’s Algorithm Example Multiply -9 X X (sub -9 = add 9) (shift after add) (shift without add) (add -9) (shift after add) (add 9) (shift after add) = = 117 Step 0 Step 1 Step 2 Step 3 Step 4 Step 5 LHProd Remember 9 = = 10111

Feb 2005Multiplication and Division21 Booth’s Algorithm Example Multiply 4 X X (sub 4 / add -4) (shift after add) (shift w/ no add) (shift w/ no add) (add +4) (shift after add) (sub 4 / add -4) Remember 4 = = = -( ) = -( ) = -36 = 4 X -9! Drop leftmost & rightmost bit Use 6-bit

Feb 2005Multiplication and Division22 Outline - Multiplication and Division  Multiplication  Review: Shift & Add Multiplication  Review: Booth’s Algorithm  Combinational Multiplication   MIPS Multiplication Instructions  Division  Summary

Feb 2005Multiplication and Division23 Combinational Multipliers  Goal: make multiplication faster  General approach  Use AND gates to generate partial products  Sum partial products with adders

Feb 2005Multiplication and Division24 Array Multiplier X3X3 X2X2 X1X1 X0X0 Y 0. From J. Rabaey, CMOS Digital Integrated Circuits © Prentice-Hall, 1996 X = multiplicand Y = multiplier

Feb 2005Multiplication and Division25 From J. Rabaey, CMOS Digital Integrated Circuits © Prentice-Hall, 1996 Array Multiplier - Critical Paths Critical Path 1 & 2

Feb 2005Multiplication and Division26 Carry-Save Multiplier From J. Rabaey, CMOS Digital Integrated Circuits © Prentice-Hall, 1996 Ripple carry only in final addition!

Feb 2005Multiplication and Division27 Wallace-Tree Multiplier From J. Rabaey, CMOS Digital Integrated Circuits © Prentice-Hall, 1996 Restructure additions to reduce delay

Feb 2005Multiplication and Division28 Outline - Multiplication and Division  Multiplication  Review: Shift & Add Multiplication  Review: Booth’s Algorithm  Combinational Multiplication  MIPS Multiplication Instructions   Division  Summary

Feb 2005Multiplication and Division29 Multiply Instructions in MIPS  MIPS adds new registers for product result:  Hi - upper 32 bits of product  Lo - lower 32 bits of product  MIPS multiply instructions  mult $s0, $s1  multu $s0, $s1  Accessing Hi, Lo registers  mfhi $s1  mflo $s1

Feb 2005Multiplication and Division30 Outline - Multiplication and Division  Multiplication  Review: Shift & Add Multiplication  Review: Booth’s Algorithm  Combinational Multiplication  MIPS Multiplication Instructions  Division  Division Algorithms   MIPS Division Instructions  Summary

Feb 2005Multiplication and Division31 Division Overview  Grammar school algorithm: long division  Subtract shifted divisor from dividend when it “fits”  Quotient bit: 1 or 0  Question: how can hardware tell “when it fits?” DividendDivisor Remainder Quotient Dividend = Quotient X Divisor + Remainder

Feb 2005Multiplication and Division32 Division Hardware - 1st Version Divisor DIVR (64 bits) Shift R QUOT (32 bits) Shift L Remainder REM (64 bits) Write Control 64-bit ALU Sign bit (REM<0) ADD/ SUB LSB  Shift register moves divisor (DIVR) to right  ALU subtracts DIVR, then restores (adds back) if REM < 0 (i.e. divisor was “too big”)

Feb 2005Multiplication and Division33 Division Algorithm - First Version START: Place Dividend in REM DONE REM ≥ 0? 2a. Shift QUOT left 1 bit; LSB=1 2. Shift DIVR right 1 bit 1. REM = REM - DIVR 33nd Repitition? REM ≥ 0REM < 0 No: <33 Repetitions Yes: 33 Repetitions 2b. REM = REM + DIVR Shift QUOT left 1 bit; LSB=0 Restore

Feb 2005Multiplication and Division34 Divide 1st Version - Observations  We only subtract 32 bits in each iteration  Idea: Instead of shifting divisor to right, shift remainder to left  First step cannot produce a 1 in quotient bit  Switch order to shift first, then subtract  Save 1 iteration

Feb 2005Multiplication and Division35 Divide Hardware - 2nd Version  Divisor Holds Still  Dividend/Remainder Shifts Left  End Result: Remainder in upper half of register QUOT (32 bits) Shift L REM (64 bits) Write Control 32-bit ALU Sign bit (REM<0) ADD/ SUB DIVR (32 bits) Shift L LSB

Feb 2005Multiplication and Division36 Divide Hardware - 3rd Version  Combine quotient with remainder register REM (64 bits) Write Control 32-bit ALU Sign bit (REM<0) ADD/ SUB DIVR (32 bits) Shift L LSB Shift R

Feb 2005Multiplication and Division37 Divide Algorithm - 3rd Version START: Place Dividend in REM DONE (shift LH right 1 bit) REM ≥ 0? 3a.. Shift REM left 1 bit; LSB=1 1. Shift REM left 1 bit 2. LHREM = LHREM - DIVR 32nd Repitition? REM ≥ 0REM < 0 No: <32 Repetitions Yes: 32 Repetitions 3b. LHREM = LHREM + DIVR Shift REM left 1 bit; LSB=0

Feb 2005Multiplication and Division38 Dividing Signed Numbers  Check sign of divisor, dividend  Negate quotient if signs of operands are opposite  Make remainder sign match dividend (if nonzero)  +7   2 =  3 … + 1   7   2 = +3 …  1

Feb 2005Multiplication and Division39 Outline - Multiplication and Division  Multiplication  Review: Shift & Add Multiplication  Review: Booth’s Algorithm  Combinational Multiplication  MIPS Multiplication Instructions  Division  Division Algorithms  MIPS Division Instructions   Summary

Feb 2005Multiplication and Division40 MIPS Divide Instructions  Divide Instructions  div $s2, $s3  divu $s2, $s3  Results in Lo, Hi registers  Hi: remainder  Lo: quotient  Divide pseudoinstructions  div $s3, $s2, $s1# $s3 = $s2 / $s1  divu $s3, $s2, $s1  Software must check for overflow, divide-by-zero

Feb 2005Multiplication and Division41 Outline - Multiplication and Division  Multiplication  Review: Shift & Add Multiplication  Review: Booth’s Algorithm  Combinational Multiplication  MIPS Multiplication Instructions  Division  Division Algorithms  MIPS Division Instructions  Summary 

Feb 2005Multiplication and Division42 Summary - Multiplication and Division  Multiplication  Sequential multipliers - efficient but slow  Combinational multipliers - fast but expensive  Division is more complex and problematic  What about divide by zero?  Restore step needed to undo unwanted subtractions  Nonrestoring division: combine restore w/ next subtract (see Problem 3.29)  Take a Computer Arithmetic course for more details  Coming Up: Floating Point