Lecture 8: Addition, Multiplication & Division

Slides:



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

CMPE 325 Computer Architecture II
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE Computer Organization Lecture 8 - Multiplication.
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.
1 Lecture 9: Floating Point Today’s topics:  Division  IEEE 754 representations  FP arithmetic Reminder: assignment 4 will be posted later today.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
Lecture 9 Sept 28 Chapter 3 Arithmetic for Computers.
Computer Organization Multiplication and Division Feb 2005 Reading: Portions of these slides are derived from: Textbook figures © 1998 Morgan Kaufmann.
1 Lecture 8: Binary Multiplication & Division Today’s topics:  Addition/Subtraction  Multiplication  Division Reminder: get started early on assignment.
1 Lecture 7: Computer Arithmetic Today’s topics:  Chapter 2 wrap-up  Numerical representations  Addition and subtraction Reminder: Assignment 3 will.
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)
Lecture Objectives: 1)Explain the relationship between addition and subtraction with twos complement numbering systems 2)Explain the concept of numeric.
Lecture 12: Computer Arithmetic Today’s topic –Numerical representations –Addition / Subtraction –Multiplication / Division 1.
Arithmetic for Computers
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
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.
Data Representation – Binary Numbers
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
Lec 13Systems Architecture1 Systems Architecture Lecture 13: Integer Multiplication and Division Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan.
Lecture 9: Floating Point
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.
Lecture 11: Chapter 2 Today’s topic –Numerical representations Reminders –Homework 3 posted, due 9/29/2014 –Homework 4 posted, due 10/6/2014 –Midterm 1.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
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
1 Lecture 7: MARS, Computer Arithmetic Today’s topics:  MARS intro  Numerical representations  Addition and subtraction.
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, Division Arithmetic shift Twice the number of places MIPS multiply unit. mult, multu Significant bits Mfhi, mflo, div, divu Arithmetic.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic6: Logic, Multiply and Divide Operations José Nelson Amaral.
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.
1 Lecture 5Multiplication and Division ECE 0142 Computer Organization.
Representing Positive and Negative Numbers
Based on slides from D. Patterson and www-inst.eecs.berkeley.edu/~cs152/ COM 249 – Computer Organization and Assembly Language Chapter 3 Arithmetic For.
Computer System Design Lecture 3
Computer Architecture & Operations I
Integer Multiplication, Division Arithmetic shift
Computer Architecture & Operations I
Integer Multiplication and Division
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
Lecture 7: MARS, Computer Arithmetic
MIPS mul/div instructions
Lecture 9: Floating Point
CS 314 Computer Organization Fall Chapter 3: Arithmetic for Computers
Morgan Kaufmann Publishers
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
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
Systems Architecture I
CDA 3101 Summer 2007 Introduction to Computer Organization
Computer Organization and Design
Digital Logic & Design Lecture 02.
Computer Architecture
Binary to Decimal Conversion
Morgan Kaufmann Publishers Arithmetic for Computers
COMS 361 Computer Organization
1 Lecture 5Multiplication and Division ECE 0142 Computer Organization.
Presentation transcript:

Lecture 8: Addition, Multiplication & Division Today’s topics: Signed/Unsigned Addition Multiplication Division

2’s Complement Why is this representation favorable? 0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0001two = 1ten … 0111 1111 1111 1111 1111 1111 1111 1111two = 231-1 1000 0000 0000 0000 0000 0000 0000 0000two = -231 1000 0000 0000 0000 0000 0000 0000 0001two = -(231 – 1) 1000 0000 0000 0000 0000 0000 0000 0010two = -(231 – 2) 1111 1111 1111 1111 1111 1111 1111 1110two = -2 1111 1111 1111 1111 1111 1111 1111 1111two = -1 Why is this representation favorable? Consider the sum of 1 and -2 …. we get -1 Consider the sum of 2 and -1 …. we get +1 This format can directly undergo addition without any conversions! Each number represents the quantity x31 -231 + x30 230 + x29 229 + … + x1 21 + x0 20

2’s Complement 0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0001two = 1ten … 0111 1111 1111 1111 1111 1111 1111 1111two = 231-1 1000 0000 0000 0000 0000 0000 0000 0000two = -231 1000 0000 0000 0000 0000 0000 0000 0001two = -(231 – 1) 1000 0000 0000 0000 0000 0000 0000 0010two = -(231 – 2) 1111 1111 1111 1111 1111 1111 1111 1110two = -2 1111 1111 1111 1111 1111 1111 1111 1111two = -1 Note that the sum of a number x and its inverted representation x’ always equals a string of 1s (-1). x + x’ = -1 x’ + 1 = -x … hence, can compute the negative of a number by -x = x’ + 1 inverting all bits and adding 1 Similarly, the sum of x and –x gives us all zeroes, with a carry of 1 In reality, x + (-x) = 2n … hence the name 2’s complement

Example Compute the 32-bit 2’s complement representations for the following decimal numbers: 5, -5, -6

Example Compute the 32-bit 2’s complement representations for the following decimal numbers: 5, -5, -6 5: 0000 0000 0000 0000 0000 0000 0000 0101 -5: 1111 1111 1111 1111 1111 1111 1111 1011 -6: 1111 1111 1111 1111 1111 1111 1111 1010 Given -5, verify that negating and adding 1 yields the number 5

Signed / Unsigned The hardware recognizes two formats: unsigned (corresponding to the C declaration unsigned int) -- all numbers are positive, a 1 in the most significant bit just means it is a really large number signed (C declaration is signed int or just int) -- numbers can be +/- , a 1 in the MSB means the number is negative This distinction enables us to represent twice as many numbers when we’re sure that we don’t need negatives

MIPS Instructions Consider a comparison instruction: slt $t0, $t1, $zero and $t1 contains the 32-bit number 1111 01…01 What gets stored in $t0?

MIPS Instructions Consider a comparison instruction: slt $t0, $t1, $zero and $t1 contains the 32-bit number 1111 01…01 What gets stored in $t0? The result depends on whether $t1 is a signed or unsigned number – the compiler/programmer must track this and accordingly use either slt or sltu slt $t0, $t1, $zero stores 1 in $t0 sltu $t0, $t1, $zero stores 0 in $t0

Sign Extension Occasionally, 16-bit signed numbers must be converted into 32-bit signed numbers – for example, when doing an add with an immediate operand The conversion is simple: take the most significant bit and use it to fill up the additional bits on the left – known as sign extension So 210 goes from 0000 0000 0000 0010 to 0000 0000 0000 0000 0000 0000 0000 0010 and -210 goes from 1111 1111 1111 1110 to 1111 1111 1111 1111 1111 1111 1111 1110

Alternative Representations The following two (intuitive) representations were discarded because they required additional conversion steps before arithmetic could be performed on the numbers sign-and-magnitude: the most significant bit represents +/- and the remaining bits express the magnitude one’s complement: -x is represented by inverting all the bits of x Both representations above suffer from two zeroes

Addition and Subtraction Addition is similar to decimal arithmetic For subtraction, simply add the negative number – hence, subtract A-B involves negating B’s bits, adding 1 and A Source: H&P textbook

Overflows For an unsigned number, overflow happens when the last carry (1) cannot be accommodated For a signed number, overflow happens when the most significant bit is not the same as every bit to its left when the sum of two positive numbers is a negative result when the sum of two negative numbers is a positive result The sum of a positive and negative number will never overflow MIPS allows addu and subu instructions that work with unsigned integers and never flag an overflow – to detect the overflow, other instructions will have to be executed

Multiplication Example Multiplicand 1000ten Multiplier x 1001ten --------------- 1000 0000 ---------------- Product 1001000ten In every step multiplicand is shifted next bit of multiplier is examined (also a shifting step) if this bit is 1, shifted multiplicand is added to the product

HW Algorithm 1 In every step multiplicand is shifted Source: H&P textbook In every step multiplicand is shifted next bit of multiplier is examined (also a shifting step) if this bit is 1, shifted multiplicand is added to the product

HW Algorithm 2 32-bit ALU and multiplicand is untouched Source: H&P textbook 32-bit ALU and multiplicand is untouched the sum keeps shifting right at every step, number of bits in product + multiplier = 64, hence, they share a single 64-bit register

Notes The previous algorithm also works for signed numbers (negative numbers in 2’s complement form) We can also convert negative numbers to positive, multiply the magnitudes, and convert to negative if signs disagree The product of two 32-bit numbers can be a 64-bit number -- hence, in MIPS, the product is saved in two 32-bit registers

MIPS Instructions mult $s2, $s3 computes the product and stores it in two “internal” registers that can be referred to as hi and lo mfhi $s0 moves the value in hi into $s0 mflo $s1 moves the value in lo into $s1 Similarly for multu

Fast Algorithm The previous algorithm requires a clock to ensure that the earlier addition has completed before shifting This algorithm can quickly set up most inputs – it then has to wait for the result of each add to propagate down – faster because no clock is involved -- Note: high transistor cost Source: H&P textbook

Division 1001ten Quotient Divisor 1000ten | 1001010ten Dividend -1000 10ten Remainder At every step, shift divisor right and compare it with current dividend if divisor is larger, shift 0 as the next bit of the quotient if divisor is smaller, subtract to get new dividend and shift 1 as the next bit of the quotient

Division 1001ten Quotient Divisor 1000ten | 1001010ten Dividend 0001001010 0001001010 0000001010 0000001010 100000000000  0001000000 00001000000000001000 Quo: 0 000001 0000010 000001001 At every step, shift divisor right and compare it with current dividend if divisor is larger, shift 0 as the next bit of the quotient if divisor is smaller, subtract to get new dividend and shift 1 as the next bit of the quotient

Divide Example Divide 7ten (0000 0111two) by 2ten (0010two) Iter Step Quot Divisor Remainder Initial values 1 2 3 4 5

Divide Example Divide 7ten (0000 0111two) by 2ten (0010two) Iter Step Quot Divisor Remainder Initial values 0000 0010 0000 0000 0111 1 Rem = Rem – Div Rem < 0  +Div, shift 0 into Q Shift Div right 0001 0000 1110 0111 2 Same steps as 1 0000 1000 1111 0111 3 0000 0100 4 Rem >= 0  shift 1 into Q 0001 0000 0010 0000 0011 5 Same steps as 4 0011 0000 0001

Title Bullet