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

Similar documents
Arithmetic for Computers

Lecture 8: Addition, Multiplication & Division

Number Systems and Their Representations

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

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

Thomas Polzer Institut für Technische Informatik

ECE260: Fundamentals of Computer Engineering

Integer Arithmetic. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Integer Multiplication and Division

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

Fast Arithmetic. Philipp Koehn. 19 October 2016

Integer Multiplication and Division

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

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

Tailoring the 32-Bit ALU to MIPS

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

COMPUTER ARITHMETIC (Part 1)

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

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

CPS 104 Computer Organization and Programming

ECE331: Hardware Organization and Design

MIPS Integer ALU Requirements

Arithmetic for Computers. Hwansoo Han

Chapter 5: Computer Arithmetic

Chapter 3 Arithmetic for Computers (Part 2)

Chapter 3. Arithmetic Text: P&H rev

We are quite familiar with adding two numbers in decimal

ECE331: Hardware Organization and Design

CS/COE0447: Computer Organization

CS/COE0447: Computer Organization

Review: MIPS Organization

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

COMPUTER ORGANIZATION AND DESIGN

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

Computer Science 61C Spring Friedland and Weaver. Instruction Encoding

COMP 303 Computer Architecture Lecture 6

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

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

Assembly Programming

Module 2: Computer Arithmetic

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

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

EEC 483 Computer Organization

Computer Architecture. Chapter 3: Arithmetic for Computers

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

carry in carry 1101 carry carry

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

ECE331: Hardware Organization and Design

Boolean Algebra. Chapter 3. Boolean Algebra. Chapter 3 Arithmetic for Computers 1. Fundamental Boolean Operations. Arithmetic for Computers

Sparse Notes on an MIPS Processor s Architecture and its Assembly Language

Chapter Two MIPS Arithmetic

ECE260: Fundamentals of Computer Engineering

Number Systems. Both numbers are positive

Chapter 10 - Computer Arithmetic

Divide: Paper & Pencil

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

Chapter 3. Arithmetic for Computers

F. Appendix 6 MIPS Instruction Reference

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

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

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

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

ECE260: Fundamentals of Computer Engineering

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

Chapter 3: Arithmetic for Computers

COE 202: Digital Logic Design Number Systems Part 2. Dr. Ahmad Almulhem ahmadsm AT kfupm Phone: Office:

Chapter 3. Arithmetic for Computers

CHW 261: Logic Design

Chapter 2: Number Systems

Chapter Three. Arithmetic

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

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

MIPS Instruction Reference

TDT4255 Computer Design. Lecture 4. Magnus Jahre

Number Systems and Computer Arithmetic

MIPS History. ISA MIPS Registers

Arithmetic. Chapter 3 Computer Organization and Design

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

Chapter 5 : Computer Arithmetic

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

CS/COE 0447 Example Problems for Exam 2 Spring 2011

Arithmetic Operations on Binary Numbers

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

COMP2611: Computer Organization. Data Representation

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

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

EE 109 Unit 6 Binary Arithmetic

Internal Data Representation

Arithmetic Logic Unit

Chapter 2. Data Representation in Computer Systems

Chapter 3 Arithmetic for Computers

MIPS Instruction Set

Chapter 4 Section 2 Operations on Decimals

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

Organisasi Sistem Komputer

Computer Architecture Set Four. Arithmetic

CENG3420 L05: Arithmetic and Logic Unit

CO212 Lecture 10: Arithmetic & Logical Unit

Week 7: Assignment Solutions

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

Transcription:

Page < 1 > Math in MIPS Adding and Subtracting Numbers Adding two binary numbers together is very similar to the method used with decimal numbers, except simpler. When you add two binary numbers together, you have to carry if the value is greater than 1 (the values would be 10 or 11). This is the same as when two decimal numbers are added and you have to carry if the value is greater than 9 (the values between 10 and 19). Examples of adding decimal and binary numbers are presented below: Carry bit 1 1 1 1 111 11 10010 + 00111 10011 + 11101 50070 + 00982 11001 10 51052 Carry digit 1 1 111 30096 + 89904 12 Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal. Borrow bit 1 1 1 1 1 0-0 1 0 0 1 10 1 0 0 1 1-0 0 1 1 1-1 1 1 10 1 0 1 0 1 1-0 1 0 1 0 0 9 0 5 6 Borrow bit 9 9 1 1 1 0-6 1 0 0 113 6 0 4 2 3-0 0 9 5 7-7 9 7 10 3-1 9 2 8 0 Two s Complement Two s complement is the representation used in most modern microprocessors for positive and negative numbers (non-floating point). It makes addition and subtraction easy to perform because of it s generally easy to convert from positive to negative numbers and visa versa, as well as add positive and negative numbers together. In fact, subtraction is done by converting the subtrahend to a negative number and then adding them together. There are two ways of coming up with a two s complement number from a negative binary number. Notice that the end result looks positive, although it can be used as though it represented a negative number. Something similar can also be done using decimal numbers. - Invert 0 1001 - + 1 01010 1010-0470 Invert 99999529-470 + 1 09999530 99999530

Page < 2 > Inverting the numbers is done by subtracting the largest single digit value to each digit in the number and taking the absolute value of it. Thus, for binary 0, we have 0 1 = 1, and for binary 1, we have 1 1 = 0. The same can be done for decimal. For decimal 4, we have 4 9 = 5, and for decimal 7, we have 7 9 = 3. The simplest method of obtaining the two s complement is by inverting the negative value and then adding one to it. To go back the other way, you reverse the process by subtracting one and then inverting the value. Another way to find the two s complement value is through understanding that when you subtract a value from 0, yet you have only a limited number of digits (8 in the above example), so the value will loop backwards as though you subtracted from a value with a 1 in the unobtainable position (9 in the above example). Whenever the computer interprets the value, you can think of it as looking at the value either subtracted by the value with the 1 in the unobtainable position, or subtracting one and inverting the digits. Either way, we can get from a truly negative value to a positive two s complement value quickly. One way to visualize the equivalence of adding two s complement value to each other to get the correct positive or negative equivalent two s complement value is by using the value with the 1 in the unobtainable position. (positive result) - (negative result) - - - 0-0 1010-0 } Two's complement + 1010 0-0 -0-11101011 -0 - } Two's complement + 11101011 0001 Shifting Digits Shifting digits in numbers is fairly straightforward. All you do is increase or decrease the position of the digits in the number. For example, if we had a binary point in our MIPS numbers, then shifting would work as follows: Theoretical Shifting 00110110.000 Unshifted 0011010. Shift left 3 00110.10 Shift right 3 Actual MIPS Logical Shifting 00110110 Unshifted 1010 Shift left 3 Shift right 3

Page < 3 > Because MIPS binary integers have a limited number of digits, when using logical shifting, the digits disappear when they go beyond the largest or smallest digit positions (as shown above). When shifting to the right, there are actually two types of shifts, logical and arithmetic. Logical right shift replaces the most significant bits with 0s. Arithmetic right shift must preserve the sign of the number, so if the number is negative, it replaces the most significant bits with 1s (called sign extension), and if the number is positive, it replaces the most significant bits with 0s. Multiplying Numbers Multiplying positive binary numbers together is extremely simple using the techniques used with decimal numbers. For 1 in the multiplier, you copy the multiplicand shifted over the correct number of digits. All these numbers you add together to get the final product. x 1001 + 100 Multiplicand Multiplier Product Normal Example x + 1101 Largest Number Example Notice how the largest possible value obtained by multiplying the two largest 4 bit numbers is no more than 8 bits. This is true no matter how many bits are used. So any product of two 32 bit values must need at most 64 bits to hold its value. This fact is used in the MIPS multiply instruction which I ll describe later. Another fact about multiplying numbers is how it doesn t seem to work as well with negative 2 s complement values as does addition. If the values we multiplied together were the 2 s complement value of -1, then the value would be 1, not -31! You should note however that if we had sign extended all of the values we multiplied together then we would have arrived at the correct answer as shown (also notice that we only add up the significant bits): Sign Extention Bits x1001 000 00 1 Multiplicand Multiplier -8 x -7 56 000 00 +0 11 Product x 111 11 1 111 11 +1 111-1 x -1 1 x0101 000 11 0 000 00 +0 11101-8 x 5-40 Out of Bounds Bits

Page < 4 > Because the same two numbers can produce different results depending on whether they are signed or unsigned multiplication, it is important to specify the desired type. For this reason, MIPS has both signed and unsigned multiplication. You should also notice the number of additions required to produce the correct answer increased by two times when we decided to handle 2 s complement signed values. A more efficient method of multiplying called Booth s Algorithm can reduce the number of additions needed in the signed case to the same number needed in the unsigned case 1. Dividing Numbers Dividing binary numbers can also be done using the same methods used in decimal division. (Amaze your friends by division using different bases!) It s also significantly easier than the decimal equivalent because you only have to worry about two values, 0 and 1! 1001010 1001 Quotient = Divisor 1001010 - Dividend 1010-10 Remainder To handle signed numbers, the standard method used is to only divide using positive numbers, compare the signs and figure out what sign to make the result. If both signs are equal, the result is positive. If both signs are opposite, the result is negative. Both the Quotient and Remainder always have the same sign. Once again it is to MIPS advantage to make both a signed and unsigned division. Instructions Causing Overflow Certain MIPS instructions, such as the signed add and subtract (both immediate and register types) can cause a system interrupt to be triggered if a condition called overflow occurs. The definition of an overflow is whenever a mathematical function creates an answer that cannot be represented given the limitations of the destination register. For example, if two positive numbers were added together and a negative number resulted because the most significant bit became 1, that would be an overflow. Similarly, if two negative numbers were added together and a postive number resulted because the most significant bit became 0, that would be an overflow. + 10111-128 + -1 = 127 Negative to Positive Flip 0111 +0001 127 + 1 = -128 Positive to Negative Flip Take notice that numbers of opposite signs added together can never cause an overflow. 1 Although the algorithm is fun to learn about, I don t have the time to go over it in any detail. It s covered in Computer Organization & Design: The Hardware/Software Interface by Patterson and Hennessy on pages 259 through 263.

Page < 5 > MIPS Math Instructions The following are the MIPS instructions for addition, subtraction, multiplication, and division. Notice that the multiplication result is a 64 bit number loaded into two special registers called Hi and Lo. The division result is two 32 bit numbers loaded into the same two special registers Hi and Lo. These values can be loaded into general purpose registers using the instructions mfhi (move from Hi) and mflo (move from Lo). add $dest, $src1, $src2 # $dest = $src1 + $src2; /* overflow */ sub $dest, $src1, $src2 # $dest = $src1 - $src2; /* overflow */ addi $dest, $src, number # $dest = $src + number; /* sign extended overflow */ addu $dest, $src1, $src2 # $dest = $src1 + $src2; /* subu $dest, $src1, $src2 # $dest = $src1 - $src2; /* addiu $dest, $src, number # $dest = $src + number; /* sign extended mult $src1, $src2 multu $src1, $src2 # (Hi:Lo) = $src1 * $src2; /* signed # (Hi:Lo) = $src1 * $src2; /* unsigned div $src1, $src2 # Lo = $src1 / $src2 /* signed */ # Hi = $src1 % $src2 /* divu $src1, $src2 # Lo = $src1 / $src2 /* unsigned */ # Hi = $src1 % $src2 /* mfhi $dest mflo $dest # $dest = Hi # $dest = Lo An important feature of addiu that you should be aware of is that the 16 bit immediate value is sign extended. This means the instruction can act as an unsigned addition and subtraction. The only real feature that makes this appear unsigned is that the overflow is ignored as though the source register s value is unsigned.