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

Similar documents
Lecture 8: Addition, Multiplication & Division

Arithmetic for Computers

Number Systems and Their Representations

Integer Multiplication and Division

COMP 303 Computer Architecture Lecture 6

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

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

Fast Arithmetic. Philipp Koehn. 19 October 2016

CPS 104 Computer Organization and Programming

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 3: Arithmetic for Computers

Tailoring the 32-Bit ALU to MIPS

MIPS Integer ALU Requirements

ECE260: Fundamentals of Computer Engineering

CS/COE0447: Computer Organization

CS/COE0447: Computer Organization

Divide: Paper & Pencil

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

Integer Arithmetic. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

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

Chapter Two MIPS Arithmetic

Chapter 3 Arithmetic for Computers

Number Systems and Computer Arithmetic

EEC 483 Computer Organization

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

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

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

Chapter Three. Arithmetic

Review: MIPS Organization

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

Module 2: Computer Arithmetic

ECE331: Hardware Organization and Design

Lecture 14: Recap. Today s topics: Recap for mid-term No electronic devices in midterm

Thomas Polzer Institut für Technische Informatik

Chapter 3 Arithmetic for Computers

CPE 335 Computer Organization. MIPS Arithmetic Part I. Content from Chapter 3 and Appendix B

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

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

Integer Multiplication and Division

Computer Architecture. Chapter 3: Arithmetic for Computers

COMPUTER ARITHMETIC (Part 1)

Computer Architecture Set Four. Arithmetic

COMPUTER ORGANIZATION AND DESIGN

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

carry in carry 1101 carry carry

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Q1: /30 Q2: /25 Q3: /45. Total: /100

CENG3420 L05: Arithmetic and Logic Unit

We are quite familiar with adding two numbers in decimal

MIPS Instruction Set

CENG 3420 Lecture 05: Arithmetic and Logic Unit

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

9 Multiplication and Division

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

F. Appendix 6 MIPS Instruction Reference

Assembly Programming

MIPS Instruction Reference

Reduced Instruction Set Computer (RISC)

Introduction to Computers and Programming. Numeric Values

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

CHW 261: Logic Design

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

ECE331: Hardware Organization and Design

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

Reduced Instruction Set Computer (RISC)

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam One 4 February Your Name (please print clearly)

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

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

5DV118 Computer Organization and Architecture Umeå University Department of Computing Science Stephen J. Hegner. Topic 3: Arithmetic

M2 Instruction Set Architecture

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

Instruction Set Architecture of. MIPS Processor. MIPS Processor. MIPS Registers (continued) MIPS Registers

MIPS Instruction Format

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

Chapter 3. Arithmetic Text: P&H rev

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

Chapter 3 Arithmetic for Computers (Part 2)

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

Number System. Introduction. Decimal Numbers

Chapter 5: Computer Arithmetic

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

ECE331: Hardware Organization and Design

ECE 15B Computer Organization Spring 2010

HW2 solutions You did this for Lab sbn temp, temp,.+1 # temp = 0; sbn temp, b,.+1 # temp = -b; sbn a, temp,.+1 # a = a (-b) = a + b;

Review 1/2 MIPS assembly language instructions mapped to numbers in 3 formats. CS61C Negative Numbers and Logical Operations R I J.

Computer Architecture. The Language of the Machine

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

Week 7: Assignment Solutions

Cache Organizations for Multi-cores

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 22 September Your Name (please print clearly) Signed.

COMP2611: Computer Organization. Data Representation

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

Arithmetic for Computers. Hwansoo Han

Organisasi Sistem Komputer

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

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

MIPS Reference Guide

Transcription:

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

Overview Homework 4 is due tonight Verify your uploaded file before the deadline This lecture Number representations and operations

Binary Representation The binary number 11011000 00010101 00101110 11100111 Most significant bit The number quantity (decimal) Least significant bit 1 x 2 31 + 1 x 2 30 + 0 x 2 29 + + 1 x 2 0 = 3625266919 A 32-bit word can represent 2 32 numbers between 0 and 2 32-1 (4,294,967,295) Represent only positive numbers Also known as the unsigned representation

Negative Numbers The binary number Sign bit 11011000 00010101 00101110 11100111 Sign-magnitude representation 1. Quantify the magnitude (31 bits) 1 x 2 30 + 0 x 2 29 + + 1 x 2 0 = 1477783271 2. Determine the sign based in the sign bit -1477783271 Example: 3-bit sing-magnitude How many numbers How to do arithmetic

Negative Numbers The binary number Sign bit 11011000 00010101 00101110 11100111 1 s complement: -x is represented by inverting x s bits 1. Invert the bits if the sign bit is set 00100111 11101010 11010001 00011000 2. Quantify the magnitude (31 bits) -1 x (1 x 2 29 + + 0 x 2 0 ) = -669700376 Example: 3-bit 1 s complement How many numbers How to do arithmetic

Negative Numbers The binary number Sign bit 11011000 00010101 00101110 11100111 Sign-magnitude and 1 s complement are not favorable Relatively complex implementation of arithmetic operations A 32-bit word represents 2 32-1 numbers between - 2 31 +1 and +2 31-1 Two different representations for zero

Negative Numbers The binary number Sign bit 11011000 00010101 00101110 11100111 2 s complement representation Give the sign bit a negative weight 1 x -2 31 + 1 x 2 30 + 0 x 2 29 + + 1 x 2 0 = -669700377 Example: 3-bit 2 s complement How many numbers How to do arithmetic

Negative Numbers The binary number Sign bit 11011000 00010101 00101110 11100111 2 s complement representation Give the sign bit a negative weight 1 x -2 31 + 1 x 2 30 + 0 x 2 29 + + 1 x 2 0 = -669700377 A 32-bit word represents 2 32 numbers between -2 31 and +2 31-1. No repeated numbers and simple arithmetic implementation

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

Example: 2 s Complement Compute the 32-bit 2 s complement representations for the following decimal numbers: 5, -5, -6 Given -5, verify that negating and adding 1 yields the number 5 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

Example All 32-bit 2 s complement representations int num = 0; do { num++; } while(num!= 0); 0000 0000 0000 0000 0000 0000 0000 0000 two = 0 ten 0000 0000 0000 0000 0000 0000 0000 0001 two = 1 ten 0111 1111 1111 1111 1111 1111 1111 1111 two = 2 31-1 1000 0000 0000 0000 0000 0000 0000 0000 two = -2 31 1000 0000 0000 0000 0000 0000 0000 0001 two = -(2 31 1) 1000 0000 0000 0000 0000 0000 0000 0010 two = -(2 31 2) 1111 1111 1111 1111 1111 1111 1111 1110 two = -2 1111 1111 1111 1111 1111 1111 1111 1111 two = -1

Signed and Unsigned The hardware recognizes two formats: Unsigned All numbers are positive, a 1 in the most significant bit just means it is a really large number Example: the unsigned int declaration in C/C++ Signed Numbers can be +/-, a 1 in the MSB means the number is negative Example: the signed int or int declaration in C/C++ Why would I need both? To represent twice as many numbers when we re sure that we don t need negatives

Example: MIPS Instructions Example: consider a comparison instruction slt $t0, $t1, $zero and $t1 contains the 32-bit number 11110111 11001010 00010100 00011110 What gets stored in $t0?

Example: MIPS Instructions Example: consider a comparison instruction slt $t0, $t1, $zero and $t1 contains the 32-bit number 11110111 11001010 00010100 00011110 What gets stored in $t0? 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

Recall: Dealing with Characters Instructions are also provided to deal with bytesized and half-word quantities: lb (load-byte), sb, lh, sh Example: loading a byte from memory Is the byte signed or unsigned? $t0: Memory: $gp

Sign Extension Signed 8-/16-bit numbers must be converted into 32-bit signed numbers Example: n addi n addi $s0, $zero, 0x8000 $s0, $zero, 0x4000 Conversion: take the most significant bit and use it to fill up the additional bits on the left 11111111 11111111 10000000 00000000 = -32768 00000000 00000000 01000000 00000000 = 16384

Unsigned Conversion Unsigned 8-/16-bit numbers must be converted into 32-bit signed numbers Example: n addiu $s0, $zero, 0x8000 n addiu $s0, $zero, 0x4000 Conversion: fill up the additional bits on the left with zeroes 00000000 00000000 10000000 00000000 = 32768 00000000 00000000 01000000 00000000 = 16384

Addition and Subtraction Addition is similar to decimal arithmetic For subtraction, simply add the negative number 4-bit example: 6 5 = 6 + (-5) 0 1 1 0 + 1 0 1 1

Overflows Note: machines have limited number of buts for representing each number 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 Instructions Instructions add, addi, sub may cause exceptions on overflow Software needs to handle exceptions 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 1000 ten Multiplier x 1001 ten --------------- 1000 0000 0000 1000 ---------------- Product 1001000 ten 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

Multiplication Algorithm 1 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

Multiplication Algorithm 2 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

Multiplication 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 Source: H&P textbook 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 26

Division 1001 ten Quotient Divisor 1000 ten 1001010 ten Dividend -1000 10 101 1010-1000 10 ten 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 27

Division 1001 ten Quotient Divisor 1000 ten 1001010 ten 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 28

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

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