CPS 104 Computer Organization and Programming

Similar documents
Homework 3. Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) CSCI 402: Computer Architectures

Lecture 8: Addition, Multiplication & Division

COMP 303 Computer Architecture Lecture 6

9 Multiplication and Division

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

Divide: Paper & Pencil

Tailoring the 32-Bit ALU to MIPS

Integer Multiplication and Division

Chapter 3: Arithmetic for Computers

EECS150 - Digital Design Lecture 13 - Combinational Logic & Arithmetic Circuits Part 3

Outline. EEL-4713 Computer Architecture Multipliers and shifters. Deriving requirements of ALU. MIPS arithmetic instructions

Signed Multiplication Multiply the positives Negate result if signs of operand are different

361 div.1. Computer Architecture EECS 361 Lecture 7: ALU Design : Division

More complicated than addition. Let's look at 3 versions based on grade school algorithm (multiplicand) More time and more area

COMPUTER ARITHMETIC (Part 1)

ECE260: Fundamentals of Computer Engineering

Chapter 3 Arithmetic for Computers (Part 2)

EEC 483 Computer Organization

Number Systems and Computer Arithmetic

Review of Last lecture. Review ALU Design. Designing a Multiplier Shifter Design Review. Booth s algorithm. Today s Outline

Module 2: Computer Arithmetic

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: The MIPS ISA (P&H ) Consulting hours. Milestone #1 (due 1/26)

MIPS Integer ALU Requirements

EE260: Logic Design, Spring n Integer multiplication. n Booth s algorithm. n Integer division. n Restoring, non-restoring

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

Chapter 5: Computer Arithmetic. In this chapter you will learn about:

Learning Objectives. Binary over Decimal. In this chapter you will learn about:

Integer Arithmetic. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Integer Multiplication. Back to Arithmetic. Integer Multiplication. Example (Fig 4.25)

Arithmetic for Computers

Chapter 3 Arithmetic for Computers. ELEC 5200/ From P-H slides

Fast Arithmetic. Philipp Koehn. 19 October 2016

CS/COE 0447 Example Problems for Exam 2 Spring 2011

CPE300: Digital System Architecture and Design

Integer Multiplication and Division

Chapter 5: Computer Arithmetic

carry in carry 1101 carry carry

Number Systems and Their Representations

Chapter 4 Section 2 Operations on Decimals

ECE331: Hardware Organization and Design

Chapter 5 : Computer Arithmetic

Lecture 13: (Integer Multiplication and Division) FLOATING POINT NUMBERS

Introduction to Computers and Programming. Numeric Values

CSE 141 Computer Architecture Summer Session Lecture 3 ALU Part 2 Single Cycle CPU Part 1. Pramod V. Argade

CS 5803 Introduction to High Performance Computer Architecture: Arithmetic Logic Unit. A.R. Hurson 323 CS Building, Missouri S&T

Chapter 3. Arithmetic Text: P&H rev

Review from last time. CS152 Computer Architecture and Engineering Lecture 6. Verilog (finish) Multiply, Divide, Shift

Arithmetic Logic Unit

Today s Outline. CS152 Computer Architecture and Engineering Lecture 5. VHDL, Multiply, Shift

UNIT-III COMPUTER ARTHIMETIC

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

COMPUTER ORGANIZATION AND. Edition. The Hardware/Software Interface. Chapter 3. Arithmetic for Computers

Organisasi Sistem Komputer

Review: MIPS Organization

Iterative Division Techniques COMPUTER ARITHMETIC: Lecture Notes # 6. University of Illinois at Chicago

COMPUTER ORGANIZATION AND DESIGN

CS/COE0447: Computer Organization

CS/COE0447: Computer Organization

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

9/6/2011. Multiplication. Binary Multipliers The key trick of multiplication is memorizing a digit-to-digit table Everything else was just adding

Chapter 10 - Computer Arithmetic

University of Illinois at Chicago. Lecture Notes # 13

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

Computer Arithmetic Ch 8

Computer Arithmetic Ch 8

ECE331: Hardware Organization and Design

PROBLEM OF ERRORS IN CALCULATION : A NEW CONCEPT OF DECIMAL

Thomas Polzer Institut für Technische Informatik

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

Computer Architecture. Chapter 3: Arithmetic for Computers

Computer Science 61C Spring Friedland and Weaver. Instruction Encoding

CHW 261: Logic Design

xx.yyyy Lecture #11 Floating Point II Summary (single precision): Precision and Accuracy Fractional Powers of 2 Representation of Fractions

Arithmetic Processing

Logic Design: Part 2

Computer Arithmetic. Appendix A Fall 2003 Lec.03-58

Adding Binary Integers. Part 4. Negative Binary. Integers. Adding Base 10 Numbers. Adding Binary Example = 10. Arithmetic Logic Unit

Review: MULTIPLY HARDWARE Version 1. ECE4680 Computer Organization & Architecture. Divide, Floating Point, Pentium Bug

Computer Organisation CS303

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

(+A) + ( B) + (A B) (B A) + (A B) ( A) + (+ B) (A B) + (B A) + (A B) (+ A) (+ B) + (A - B) (B A) + (A B) ( A) ( B) (A B) + (B A) + (A B)

Week 7: Assignment Solutions

IEEE Standard 754 for Binary Floating-Point Arithmetic.

Arithmetic for Computers. Hwansoo Han

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

BINARY SYSTEM. Binary system is used in digital systems because it is:

Arithmetic and Bitwise Operations on Binary Data

CS 64 Week 1 Lecture 1. Kyle Dewey

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Introduction to Digital Logic Missouri S&T University CPE 2210 Multipliers/Dividers

CS61C : Machine Structures

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Lecture 6: Signed Numbers & Arithmetic Circuits. BCD (Binary Coded Decimal) Points Addressed in this Lecture

Chapter 3 Arithmetic for Computers

CS101 Lecture 04: Binary Arithmetic

Chapter 3 Arithmetic for Computers

Finite State Machine with Datapath

Topics. 6.1 Number Systems and Radix Conversion 6.2 Fixed-Point Arithmetic 6.3 Seminumeric Aspects of ALU Design 6.4 Floating-Point Arithmetic

ECE 3120 Computer Systems Arithmetic Programming

Transcription:

CPS 104 Computer Organization and Programming Lecture 9: Integer Arithmetic. Robert Wagner CPS104 IMD.1 RW Fall 2000

Overview of Today s Lecture: Integer Multiplication and Division. Read Appendix B CPS104 IMD.2 RW Fall 2000

Integer Multiplication Product = Multiplicand x Multiplier Example: 0011 x 0101 Multiplicand 0 0 1 1 Multiplier 0 1 0 1 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 Product 0 0 0 1 1 1 1 CPS104 IMD.3 RW Fall 2000

Multiplication Algorithm #1 From Right-Left: If multiplier digit = 1: add (shifted) copy of multiplicand to result. If multiplier digit = 0: add 0 to result. (do nothing) 32 steps when multiplier is 32-bit number. Example: 3 10 x 5 10 or 0011 2 x 0101 2 Product = 00001111 2 Product has twice as many bits as operands CPS104 IMD.4 RW Fall 2000

Multiplication Algorithm #1 Start Multiplier0 = 1 1. Test Multiplier0 Multiplier0 = 0 1a. Add multiplicand to product and place the result in Product register 2. Shift the Multiplicand register left 1 bit 3. Shift the Multiplier register right 1 bit 32nd repetition? No: < 32 repetitions Yes: 32 repetitions Done Figure Copyright Morgan Kaufmann CPS104 IMD.5 RW Fall 2000

Multiplication Hardware #1 Multiplicand starts in right half of register Multiplicand 64 bits Shift left 64-bit ALU Multiplier Shift right 32 bits Product 64 bits Write Control test Figure Copyright Morgan Kaufmann CPS104 IMD.6 RW Fall 2000

Multiplication Hardware #2 Shift Multiplicand Left works same as Shift Product Right Only need 32 bits for multiplicand Multiplicand 32 bits 32-bit ALU Multiplier Shift right 32 bits Product 64 bits Shift right Write Control test Possible to combine multiplier and product registers Keep multiplier in low order 32 bits of product register Figure Copyright Morgan Kaufmann CPS104 IMD.7 RW Fall 2000

Multiplication Algorithm #2 Start Multiplier0 = 1 1. Test Multiplier0 Multiplier0 = 0 1a. Add multiplicand to the left half of the product and place the result in the left half of the Product register 2. Shift the Product register right 1 bit 3. Shift the Multiplier register right 1 bit 32nd repetition? No: < 32 repetitions Yes: 32 repetitions Done Figure Copyright Morgan Kaufmann CPS104 IMD.8 RW Fall 2000

Signed Multiplication II Overflow can occur during the addition step Solution: Make the product register one bit longer Recall that the high-order bit of the multiplier has NEGATIVE weight If the high-order bit of the multiplier is 1, SUBTRACT the multiplicand from the product, instead of adding it CPS104 IMD.9 RW Fall 2000

Multiplication Examples (4 bit operands) 00111 2 * 0111 2 11001 2 * 1001 2 00111 2 * 1001 2 11001 2 * 0111 2 00000 0111 00000 1001 00000 1001 00000 0111 00111 0111 11001 1001 00111 1001 11001 0111 00011 1011 11100 1100 00011 1100 11100 1011 01010 1011 11110 0110 00001 1110 10101 1011 00101 0101 11111 0011 00000 1111 11010 1101 01100 0101 subtract subtract 10011 1101 00110 0010 00110 0011 11001 1111 11001 1110 00011 0001 00011 0001 11100 1111 11100 1111 = 3*16+1-7*-7 = 49 -(00011 0001) -7*7 = -49 CPS104 IMD.10 RW Fall 2000

Booth Encoding Observation: Can write number as difference of two numbers. In particular: Can replace a string of 1s with initial subtract when we see a 1, and then an add when we see the bit AFTER the last 1 Example 1: 7 10 7 10 = -1 10 + 8 10 0111 2 = -0001 2 + 1000 2 Example 2: 110 10 = 01101110 2 110 10 = (-2 10 + 16 10 ) + (-32 10 + 128 10 ) 01101110 2 = (-00000010 2 + 00010000 2 ) + (-00100000 2 + 10000000 2 ) Works for signed numbers as well! CPS104 IMD.11 RW Fall 2000

Booth s Algorithm Similar to previous multiply algorithm. (Current, Previous) bits of Multiplier: 0,0: middle of string of 0s; do nothing 0,1: end of a string of 1s; add multiplicand 1,0; start of string of 1s; subtract multiplicand 1,1: middle of string of 1s; do nothing Shift Product/Multiplier right 1 bit (as before) CPS104 IMD.12 RW Fall 2000

Booth Multiplication Examples (4 bit operands) 0111 2 * 0111 2 1001 2 * 1001 2 0111 2 * 1001 2 1001 2 * 0111 2 0000 0111 0-0000 1001 0-0000 1001 0-0000 0111 0-1001 0111 0111 1001 1001 1001 0111 0111 1100 1011 1 x 0011 1100 1 + 1100 1100 1 + 0011 1011 1 x 1110 0101 1 x 1100 1100 0011 1100 0001 1101 1 x 1111 0010 1 + 1110 0110 0 x 0001 1110 0 x 0000 1110 1 + 0110 0010 1111 0011 0-0000 1111 0-1001 1110 0011 0001 0110 0011 1100 1111 1100 1111 00110 001 = 3*16+1 -(0011 0001) -7*7 = -49-7*-7 = 49 CPS104 IMD.13 RW Fall 2000

Signed Multiplication Convert negative numbers to positive and remember the original signs. In 2s-complement, can multiply directly using a 1-bit longer product register Sign extend when shifting Subtract instead of adding on last step Or, use Booth s algorithm Sign extend when shifting CPS104 IMD.14 RW Fall 2000

Integer Division Dividend = Quotient x Divisor + Remainder Example: 1,001,010 ten / 1000 ten 1 0 0 1 ten Quotient Divisor 1000 ten 1 0 0 1 0 1 0 ten Dividend -1 0 0 0 1 0 1 0 1 1 0 1 0-1 0 0 0 1 0 ten Remainder Compare shifted divisor and remainder, subtract only if divisor<=remainder CPS104 IMD.15 RW Fall 2000

Division Hardware #1 Dividend starts in Remainder register Divisor starts in left half of divisor register Comparison requires extra hardware, not shown, or extra steps Divisor Shift right 64 bits 64-bit ALU Quotient Shift left 32 bits Remainder 64 bits Write Control test Figure Copyright Morgan Kaufmann CPS104 IMD.16 RW Fall 2000

Division (contd.) Similar to multiplication Shift remainder left instead of shifting divisor right Combine quotient register with right half of remainder register Signed Division Remember the signs and negate quotient if different. Make sign of remainder match the dividend Same hardware can be used for both multiply and divide. Need 64-bit register that can shift left and right ALU that adds or subtracts Optimizations possible CPS104 IMD.17 RW Fall 2000

Summary Both multiplication and division are MULTISTEP algorithms Multiplication takes some 32 steps, each about 1 cycle Division may take twice as long Some optimizations are possible Multiplication in about 9 cycles (32-bit operands) Division in about 32 Each algorithm uses one 64-bit register, usually split into a HI 32-bit part, and a LO 32-bit part MIPS uses MFHI and MFLO operations to read these Both operations (and MOD) are SLOW compared to ADD CPS104 IMD.18 RW Fall 2000