Multiplication and Division

Slides:



Advertisements
Similar presentations
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,
Advertisements

CMPE 325 Computer Architecture II
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE Computer Organization Lecture 8 - Multiplication.
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.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
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.
Addition Subtraction Multiplication Division
Dividing Polynomials.
Binary Arithmetic Math For Computers.
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.
Computer Arithmetic Nizamettin AYDIN
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department.
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.
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
Division Check for 0 divisor Long division approach – If divisor ≤ dividend bits 1 bit in quotient, subtract – Otherwise 0 bit in quotient, bring down.
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.
Integer Multiplication and Division ICS 233 Computer Architecture & Assembly Language Prof. Muhamed Mudawar College of Computer Sciences and Engineering.
William Stallings Computer Organization and Architecture 8th Edition
More Binary Arithmetic - Multiplication
LONG DIVISION CAN BE EASY!.
Computer Architecture & Operations I
Lecture on LONG DIVISION
Multiplication and Division basics
Computer Architecture & Operations I
Integer Multiplication and Division
MIPS mul/div instructions
Morgan Kaufmann Publishers Arithmetic for Computers
Dividing Polynomials.
Data Representation in Computer Systems
COMPUTING FUNDAMENTALS
Morgan Kaufmann Publishers
Binary Addition and Subtraction
CS/COE 0447 (term 2181) Jarrett Billingsley
Morgan Kaufmann Publishers
Lecture 8: Binary Multiplication & Division
Multiplication & Division
Warm-up: Do you remember how to do long division? Try this: (without a calculator!)
LONG DIVISION CAN BE EASY!.
CDA 3101 Summer 2007 Introduction to Computer Organization
Dividing Polynomials.
Lecture 8: Addition, Multiplication & Division
Topic 3: Data Binary Arithmetic.
Addition, Signed Numbers, and Overflow
Lecture 8: Addition, Multiplication & Division
Division Page 87 – 119.
Decimal Operations.
Topic 3c Integer Multiply and Divide
Topic 3: Data Signed Binary.
CS/COE 0447 Jarrett Billingsley
Fundamentals of Data Representation
Bitwise Operations and Bitfields
ECEG-3202 Computer Architecture and Organization
Addition Subtraction Multiplication Division
Fundamentals of Data Representation
Reading: Study Chapter (including Booth coding)
FSMs, Multiplication, and Division
Arithmetic and Decisions
Chapter 8 Computer Arithmetic
Montek Singh Mon, Mar 28, 2011 Lecture 11
Long Division by Michael Hedderig
Presentation transcript:

Multiplication and Division CS/COE 0447 Jarrett Billingsley

Class announcements NNNOOOOOOOOO!!!!!!!! CS447

Multiplication CS447

Monkeys, ladders, bananas, and hoses here's where most people would teach you Booth's Algorithm there are a few problems with it: it's really complicated it's really confusing most importantly, literally no one uses it anymore and we haven't for decades ever heard the (fake?) story of the monkey-ladder experiment? as far as I can tell, Booth's Algorithm is a waste of time used to torture architecture students and nothing more - scientists put a group of 3 monkeys in a room with a ladder. at the top of the ladder was a banana. - whenever any monkey tried to climb the ladder, the scientists sprayed them with a hose. - eventually all the monkeys learned not to climb the ladder. - then they replaced one monkey with a new one. new monkey tries to climb the ladder, but other 2 monkeys pull him down. - eventually the new monkey learns not to climb the ladder from the other 2 monkeys. - then they replaced another of the original monkeys… same thing. - then they replaced the last original monkey… same thing. - now they had three monkeys who had never been sprayed but nonetheless refused to go up the ladder. - "it's just how we've always done it." - "listen to your elders." - "do you really want to take the risk?" CS447

Multiplication by repeated addition in A × B, the product (answer) is "A copies of B, added together" 6 × 3 = 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 how many additions would it take to calculate 500,000,000 × 2? CS447

A B P 1 ✕ 1 Back to grade school remember your multiplication tables? binary is so much easier if we list 0 too, the product logic looks awfully familiar… ✕ 1 2 3 4 5 6 7 8 9 10 12 14 16 18 15 21 24 27 20 28 32 36 25 30 35 40 45 42 48 54 49 56 63 64 72 81 ✕ 1 A B P 1 - it's an AND! (hey, we don't call it "logical product" for nothing.) CS447

Just like you remember you know how to multiply, riiiight? 1011 = × 0101 = 1011 0000 0000___ 11 × 5 Multiplicand Multiplier these are partial products. how many additions are we doing? 55 00 000 wait, what operation are we doing here...? 110111 - for an n-bit multiplier, we have n partial products, and therefore O(n) additions. - each partial product is shifted left one more place than the one above it. - you do this in decimal too! each row of the partial product is multiplied by 10 (shifted left one place). CS447

78×54 = 70×50 + 70×4 + 8×50 + 8×4 Wait, why does this work? what are we actually doing with this technique? remember how positional numbers are really polynomials? FOIL… 78×54 = 70×50 + 70×4 + 8×50 + 8×4 we're eliminating many addition steps by grouping them together. we're eliminating many addition steps by grouping them together. we group them together by powers of the base. - we're also "mashing together" some of the inner terms in each partial product. - so even though this FOILing gives us 3 terms, we only have 2 partial products, since the middle term is split across 2 partial products. CS447

Grade school algorithm, formalized for(n bits in multiplier) { if((multiplier & 1) != 0) product += multiplicand multiplicand <<= 1 multiplier >>= 1 } Multiplicand Multiplier Product 1011 0101 +1011 10110 010 1011 + 0 101100 01 1011 shifting the multiplier right is just like going right-to-left through the digits. shifting the multiplicand left is what we do in the partial product rows. how long does this take? +101100 1011000 110111 + 0 - "multiplier & 1" is checking if the LSB is 1. (it masks off everything except the LSB.) - the code inside the loop is O(1), so the loop is O(n) where n is the number of bits in the multiplier. 10110000 110111 CS447

How many bits? 99 × 99 9801 when we added two n-digit/bit numbers, how many digits/bits was the sum? how about for multiplication? when you multiply two n-digit/bit numbers, the product will be at most 2n digits/bits so if we multiply two 32-bit numbers… we could get a 64-bit result! AAAA! if we just ignored those extra 32 bits, or crashed, we'd be losing a lot of info. so we have to store it. 9999 × 9999 99980001 1111 × 1111 11100001 - when adding two n-bit numbers, we could get an n+1-bit result. - technically if you multiply an m-bit number by an n-bit result then the product can be (m+n) bits, but if m = n (commonly), then it's 2n. CS447

How (and why) MIPS does it MIPS has two more 32-bit registers, HI and LO. if you do this: mult t0, a0 then HI = upper 32 bits of the product and LO = lower 32 bits to actually get the product, we use these: mfhi t0 # move From HI (t0 = HI) mflo t1 # move From LO (t1 = LO) the mul pseudo-op does a mult followed by an mflo MIPS does this for 2 reasons: multiplication can take longer than addition we'd otherwise have to change two different registers at once if you wanted to check for 32-bit multiplication overflow, how could you do it? - you could use mfhi to get the upper 32 bits and see if it's nonzero. - if all the upper bits are 0, then the result fits comfortably in 32 bits. - if all the upper bits are 1, then the result is negative… and may be either 32 or 33 bits… so more checks… ugh… CS447

Signed multiplication CS447

Grade school (but like, 6th, instead of 3rd) if you multiply two signed numbers, what's the rule? Product Sign A B P 3 5 15 -5 -15 -3 A B S + - if the signs of the operands differ, the output is negative. - you may or may not recognize this logic as exclusive OR. CS447

long prod = unsigned_mult(abs(A), abs(B)); if(sgn(A) == sgn(B)) Don't repeat yourself we already have an algorithm to multiply unsigned numbers multiplying signed numbers is exactly the same (except for the signs) so why not use what we already made? long prod = unsigned_mult(abs(A), abs(B)); if(sgn(A) == sgn(B)) return prod; else return –prod; - abs(x) = {-x if x < 0; x otherwise} - sgn(x) = {-1 if x < 0; 1 if x > 0; 0 otherwise} CS447

Division Like multiplication, but… not CS447

If multiplication is repeated addition… …is division repeated subtraction? yes. it is. in A ÷ B, the quotient (answer) is "how many times can you subtract B from A until you can't anymore?" 20 ÷ 3 = 6 R 2 1 2 3 what about these lil guys? 4 5 6 how many subtractions would it take to calculate 1,000,000,000 ÷ 2? CS447

That's not what you learned in school, was it you learned something a tiiiiiny bit more complicated 5 4 R51 4÷77? 4209 77 42÷77? -385 =77×5 9 420÷77! oh, that's like 77×4 35 uhh…how many times? like, 5-ish? guess and check -308 =77×4 51 wtf? CS447

What's going on? division is multiplication backwards. it's like parallel parking. 77 × we're finding the partial products that add up to a total product 5 4 division goes left-to-right because we find the partial products from biggest to smallest 308 3850 and to make it even more interesting, there might be a remainder 4158 and then there's division by 0. +R51 multiplication is multiplying polynomials. division is factoring polynomials. 4209 CS447

Another way of looking at it (animated) let's say we want to do… idk, 696÷4 40 40 40 4 first we ask how many 400s fit 1 400 4 40 7 then how many 40s 4 4 then how many 4s 40 4 so we're saving time by doing groups of subtractions from biggest to smallest 40 40 - could we go smallest to biggest? - how would you tell how many 4s before you knew how many 400s and 40s? - (YOU CAN'T) CS447

Thanks, tiny multiplication table. Thable. at least multiplication in binary is easy, which simplifies division 1 1 R1 1100≤1? 1100≤10? 1100≤100? 1001001 1100 1100≤1001? -1100 1 1100≤10010! 110 1100≤1100! -1100 in binary, each step becomes a yes-no choice: does the divisor fit into the remainder? 1100≤1? CS447

Divisor? Dividend? Remainder? the divisor divides the dividend the dividend is the number that is being divided 1001001 1100 -1100 1 the remainder is the number we're trying to fit the divisor into 110 this is the new remainder. but really, when we subtract something, we are making the remainder smaller. it starts off as the dividend… - the remainder isn't just a number you get at the end. it is the number that you start off with and chop parts off of. - it's the uncovered (white) part of the graph paper. - the remainder went from 1001001 to 11001, and the next step will reduce it to 1. - we usually don't drop more than 1 digit at a time… just to save some writing. but they are there. CS447

Finding partial products, biggest to smallest (animated) essentially we're starting with the divisor shifted all the way left, and sliding it right 1 1 R1 1100 1 1001001 11001 1100000000 so let's ALGORITHM-IZE IT CS447

noitacilpitluM let's do 11 ÷ 5. Divisor Remainder Quotient divisor <<= n (bits in dividend) remainder = dividend quotient = 0 for(n bits in dividend) { divisor >>= 1 if(divisor > remainder) { append 0 to quotient } else { remainder -= divisor append 1 to quotient } 1010000 1011 101000 1011 10100 1011 1010 -1010 1 101 1 10 1 10 - 11 ÷ 5 is indeed 2R1. CS447