Integer Multiplication and Division

Slides:



Advertisements
Similar presentations
Arithmetic for Computers
Advertisements

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.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
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.
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.
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
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.
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,
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
Integer Multiplication and Division COE 301 Computer Organization Dr. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University.
Integer Operations Computer Organization and Assembly Language: Module 5.
By Wannarat Computer System Design Lecture 3 Wannarat Suntiamorntut.
Integer Multiplication and Division ICS 233 Computer Architecture & Assembly Language Prof. Muhamed Mudawar College of Computer Sciences and Engineering.
1 Lecture 5Multiplication and Division ECE 0142 Computer Organization.
Computer System Design Lecture 3
More Binary Arithmetic - Multiplication
Computer Architecture & Operations I
Data Representation ICS 233
Computer Architecture & Operations I
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 Arithmetic for Computers
CS 314 Computer Organization Fall Chapter 3: Arithmetic for Computers
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
Lecture 8: Addition, Multiplication & Division
Topic 3c Integer Multiply and Divide
CDA 3101 Summer 2007 Introduction to Computer Organization
Part III The Arithmetic/Logic Unit
Computer Architecture
Computer Architecture EECS 361 Lecture 6: ALU Design
Morgan Kaufmann Publishers Arithmetic for Computers
MIPS Arithmetic and Logic Instructions
MIPS Arithmetic and Logic Instructions
1 Lecture 5Multiplication and Division ECE 0142 Computer Organization.
Presentation transcript:

Integer Multiplication and Division ICS 233 Computer Architecture and Assembly Language Dr. Aiman El-Maleh College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals

Outline Unsigned Multiplication Signed Multiplication Faster Multiplication Unsigned Division Signed Division Multiplication and Division in MIPS

Unsigned Multiplication Paper and Pencil Example: Multiplicand 11002 = 12 Multiplier × 11012 = 13 1100 0000 Product 100111002 = 156 m-bit multiplicand × n-bit multiplier = (m+n)-bit product Accomplished via shifting and addition Binary multiplication is easy 0 × multiplicand = 0 1 × multiplicand = multiplicand

Version 1 of Multiplication Hardware Initialize Product = 0 Multiplicand is zero extended Start Multiplier[0]? 1a. Product = Product + Multiplicand 2. Shift the Multiplicand Left 1 bit 3. Shift the Multiplier Right 1 bit 32nd Repetition? Done = 0 = 1 No Yes Multiplicand shift left 64 bits 64-bit ALU add 64 bits Product write Control 64 bits shift right Multiplier 32 bits Multiplier[0]

Multiplication Example (Version 1) Consider: 11002 × 11012 , Product = 100111002 4-bit multiplicand and multiplier are used in this example Multiplicand is zero extended because it is unsigned 2 00001100 00000000 1101 Initialize 1 3 4 Multiplicand Product Multiplier Iteration 00001100 Multiplier[0] = 1 => ADD + 00011000 0110 SLL Multiplicand and SRL Multiplier 00001100 Multiplier[0] = 0 => Do Nothing 00111100 Multiplier[0] = 1 => ADD + 0011 00110000 SLL Multiplicand and SRL Multiplier 10011100 Multiplier[0] = 1 => ADD + 0001 01100000 SLL Multiplicand and SRL Multiplier 0000 11000000 SLL Multiplicand and SRL Multiplier

Observation on Version 1 of Multiply Hardware in version 1 can be optimized Rather than shifting the multiplicand to the left Instead, shift the product to the right Has the same net effect and produces the same results Reduce Hardware Multiplicand register can be reduced to 32 bits only We can also reduce the adder size to 32 bits One cycle per iteration Shifting and addition can be done simultaneously

Version 2 of Multiplication Hardware Product = HI and LO registers, HI=0 Product is shifted right Reduced 32-bit Multiplicand & Adder Start = 1 Multiplier[0]? = 0 Multiplicand 32 bits 32 bits HI = HI + Multiplicand 32-bit ALU add 33 bits Shift Product = (HI,LO) Right 1 bit Shift Multiplier Right 1 bit 32 bits shift right carry HI LO Control write 64 bits 32nd Repetition? No shift right Multiplier Yes Multiplier[0] Done 32 bits

Refined Version of Multiply Hardware Eliminate Multiplier Register Initialize LO = Multiplier, HI=0 Product = HI and LO registers Start LO=Multiplier, HI=0 = 1 LO[0]? = 0 32-bit ALU Control 64 bits 32 bits write add LO[0] Multiplicand shift right 33 bits HI LO carry HI = HI + Multiplicand Shift Product = (HI,LO) Right 1 bit 32nd Repetition? No Yes Done

Multiply Example (Refined Version) Consider: 11002 × 11012 , Product = 100111002 4-bit multiplicand and multiplier are used in this example 4-bit adder produces a 5-bit sum (with carry) Iteration Multiplicand Carry Product = HI, LO Initialize (LO = Multiplier, HI=0) 1 1 0 0 0 0 0 0 1 1 0 1 1 1 0 0 1 1 0 1 LO[0] = 1 => ADD + 1 1 1 0 0 Shift Right Product = (HI, LO) 0 1 1 0 0 1 1 0 2 LO[0] = 0 => Do Nothing 1 1 0 0 Shift Right Product = (HI, LO) 0 0 1 1 0 0 1 1 1 1 1 1 0 0 1 1 LO[0] = 1 => ADD + 3 1 1 0 0 Shift Right Product = (HI, LO) 0 1 1 1 1 0 0 1 1 0 0 1 1 1 0 0 1 LO[0] = 1 => ADD + 4 1 1 0 0 Shift Right Product = (HI, LO) 1 0 0 1 1 1 0 0

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

Signed Multiplication So far, we have dealt with unsigned integer multiplication Version 1 of Signed Multiplication 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 Refined Version: Use the refined version of 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 11002 = -4 Multiplier × 01012 = +5 11111100 111100 Product 111011002 = -20 Case 2: Negative Multiplier Multiplier × 11012 = -3 00100 (2's complement of 1100) Product 000011002 = +12 Sign-extension Sign-extension

Signed Multiplication Hardware Similar to Unsigned Multiplier ALU produces a 33-bit result Multiplicand and HI are sign-extended Sign is the sign of the result = 0 Start LO[0]? First 31 iterations: HI = HI + Multiplicand Last iteration: HI = HI – Multiplicand 32nd Repetition? Done = 1 No Yes LO=Multiplier, HI=0 Shift Right Product = (HI, LO) 1 bit 33-bit ALU Control 64 bits 32 bits write add, sub LO[0] Multiplicand shift right 33 bits HI LO sign

Signed Multiplication Example Consider: 11002 (-4) × 11012 (-3), Product = 000011002 Multiplicand and HI are sign-extended before addition Last iteration: add 2's complement of Multiplicand Iteration Multiplicand Sign Product = HI, LO Initialize (LO = Multiplier) 1 1 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 1 1 0 1 LO[0] = 1 => ADD + 1 1 1 0 0 Shift Product = (HI, LO) right 1 bit 1 1 1 0 0 1 1 0 2 LO[0] = 0 => Do Nothing 1 1 0 0 Shift Product = (HI, LO) right 1 bit 1 1 1 1 0 0 1 1 1 1 0 1 1 0 0 1 1 LO[0] = 1 => ADD + 3 1 1 0 0 Shift Product = (HI, LO) right 1 bit 1 1 0 1 1 0 0 1 0 1 0 0 0 0 0 1 1 0 0 1 LO[0] = 1 => SUB (ADD 2's compl) + 4 Shift Product = (HI, LO) right 1 bit 0 0 0 0 1 1 0 0

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

Faster Multiplication Hardware 32-bit adder for each bit of the multiplier 31 adders are needed for a 32-bit 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 Least significant bit is a product bit Upper 32 bits go to next adder Array multiplier can be optimized Carry save adders reduce delays Pipelining further improves the speed 32-bit A B1 32 bits B0 B2 1 33 bits B3 B31 1 bit P1 P2 P3 P31 P63..32 . . . . . P0

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 ripple-carry adder This is why the carry save adder is much faster than ripple-carry A carry save adder has 3 inputs and produces two outputs It adds 3 numbers and produces partial sum and carry bits Ripple Carry Adder + a0 b0 s0 a1 b1 s1 a31 b31 s31 . . . cout cin Carry Save Adder s'31 c'31 c31 s'1 c'1 c1 s'0 c'0 c0

Consider Adding: S = A + B + C + D Ripple Carry Adder A B C S D Carry Save Adder Ripple Carry Adder A B C D S

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

Unsigned Division (Paper & Pencil) = 19 Quotient Divisor 10112 110110012 = 217 Dividend -1011 10 101 1010 10100 1001 10011 10002 = 8 Remainder 1 1 12 Try to see how big a number can be subtracted, creating a digit of the quotient on each attempt Dividend = Quotient × Divisor + Remainder 217 = 19 × 11 + 8 Binary division is accomplished via shifting and subtraction

First Division Algorithm & Hardware Initialize: Remainder = Dividend (0-extended) Load Upper 32 bits of Divisor Quotient = 0 Start Difference? 2. Remainder = Difference Set least significant bit of Quotient 32nd Repetition? Done < 0 ≥ 0 No Yes Shift the Divisor Right 1 bit Shift the Quotient Left 1 bit Difference = Remainder – Divisor Divisor 64-bit ALU Control Quotient Remainder 64 bits write shift right sub shift left 32 bits Difference sign set lsb

Division Example (Version 1) Consider: 11102 / 00112 (4-bit dividend & divisor) Quotient = 01002 and Remainder = 00102 8-bit registers for Remainder and Divisor (8-bit ALU) Iteration Remainder Divisor Difference Quotient Initialize 00001110 00110000 0000 1 0000 1: SRL, SLL, Difference 00001110 00011000 11110110 2: Diff < 0 => Do Nothing 2 0000 1: SRL, SLL, Difference 00001110 00001100 00000010 2: Rem = Diff, set lsb Quotient 00000010 0001 3 0010 1: SRL, SLL, Difference 00000010 00000110 11111100 2: Diff < 0 => Do Nothing 4 0100 1: SRL, SLL, Difference 00000010 00000011 11111111 2: Diff < 0 => Do Nothing

Observations on Version 1 of Divide Version 1 of Division hardware can be optimized Instead of shifting divisor right, Shift the remainder register left Has the same net effect and produces the same results Reduce Hardware: Divisor register can be reduced to 32 bits (instead of 64 bits) ALU can be reduced to 32 bits (instead of 64 bits) Remainder and Quotient registers can be combined

Refined Division Hardware Observation: Shifting remainder left does the same as shifting the divisor right Initialize: Quotient = Dividend, Remainder = 0 Start Difference? 2. Remainder = Difference Set least significant bit of Quotient 32nd Repetition? Done < 0 ≥ 0 No Yes Shift (Remainder, Quotient) Left Difference = Remainder – Divisor shift left Divisor 32-bit ALU Quotient 32 bits write sub Difference sign set lsb Remainder Control

Division Example (Refined Version) Same Example: 11102 / 00112 (4-bit dividend & divisor) Quotient = 01002 and Remainder = 00102 4-bit registers for Remainder and Divisor (4-bit ALU) Iteration Remainder Quotient Divisor Difference Initialize 0 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 0 1: SLL, Difference 0 0 0 1 1 1 0 0 0 0 1 1 2: Diff < 0 => Do Nothing 2 0 0 0 0 1: SLL, Difference 0 0 1 1 1 0 0 0 2: Rem = Diff, set lsb Quotient 1 0 0 1 0 0 0 0 3 1 1 1 0 1: SLL, Difference 0 0 0 1 0 0 1 0 0 0 1 1 2: Diff < 0 => Do Nothing 4 1 1 1 1 1: SLL, Difference 0 0 1 0 0 1 0 0 0 0 1 1 2: Diff < 0 => Do Nothing

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

Signed 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 Division Examples Positive Dividend and Positive Divisor Example: +17 / +3 Quotient = +5 Remainder = +2 Positive Dividend and Negative Divisor Example: +17 / –3 Quotient = –5 Remainder = +2 Negative Dividend and Positive Divisor Example: –17 / +3 Quotient = –5 Remainder = –2 Negative Dividend and Negative Divisor Example: –17 / –3 Quotient = +5 Remainder = –2 The following equation must always hold: Dividend = Quotient × Divisor + Remainder

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

Multiplication in MIPS Two 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 LO = low-order 32-bit Result of multiplication is always in HI & LO 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 HI LO $1 . $31

Division in MIPS Two 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 to HI/LO from MIPS registers mthi Rs (move to HI from Rs) mtlo Rs (move to LO from Rs) Multiply Divide $0 HI LO $1 . $31

Integer Multiply/Divide Instructions Meaning Format mult Rs, Rt Hi, Lo = Rs × Rt op6 = 0 Rs5 Rt5 0x18 multu Rs, Rt 0x19 div Rs, Rt Hi, Lo = Rs / Rt 0x1a divu Rs, Rt 0x1b mfhi Rd Rd = Hi Rd5 0x10 mflo Rd Rd = Lo 0x12 mthi Rs Hi = Rs 0x11 mtlo Rs Lo = Rs 0x13 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/10 quotient = 203 remainder = 7 char = '7' Divide 203/10 quotient = 20 remainder = 3 char = '3' Divide 20/10 quotient = 2 remainder = 0 char = '0' Divide 2/10 quotient = 0 remainder = 2 char = '2'

Integer to String Procedure #-------------------------------------------------------- # int2str: Converts an unsigned integer into a string # Parameters: $a0 = integer to be converted # $a1 = string pointer (can store 10 digits) int2str: move $t0, $a0 # $t0 = dividend = integer value li $t1, 10 # $t1 = divisor = 10 addiu $a1, $a1, 10 # start at end of string sb $zero, 0($a1) # store a NULL byte convert: divu $t0, $t1 # LO = quotient, HI = remainder mflo $t0 # $t0 = quotient mfhi $t2 # $t2 = remainder ori $t2, $t2, 0x30 # convert digit to a character addiu $a1, $a1, -1 # point to previous char sb $t2, 0($a1) # store digit character bnez $t0, convert # loop if quotient is not 0 jr $ra