CS.305 Computer Architecture Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005, and from slides kindly made available by Dr Mary.

Slides:



Advertisements
Similar presentations
Computer Organization MIPS Arithmetic – Part II
Advertisements

The MIPS 32 1)Project 1 Discussion? 1)HW 2 Discussion? 2)We want to get some feel for programming in an assembly language - MIPS 32 We want to fully understand.
CSE431 Chapter 3.1Irwin, PSU, 2008 CSE 431 Computer Architecture Fall 2008 Chapter 3: Arithmetic for Computers Mary Jane Irwin ( )
Chapter Three.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Arithmetic in Computers Chapter 4 Arithmetic in Computers2 Outline Data representation integers Unsigned integers Signed integers Floating-points.
1 Chapter Three Last revision: 4/17/ Arithmetic Where we've been: –Performance (seconds, cycles, instructions) –Abstractions: Instruction Set Architecture.
Chapter 3 Arithmetic for Computers. Exam 1 CSCE
Arithmetic for Computers
CSCE 212 Chapter 3: Arithmetic for Computers Instructor: Jason D. Bakos.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
1  2004 Morgan Kaufmann Publishers Chapter Three.
CSE431 L03 MIPS Arithmetic Review.1Irwin, PSU, 2005 CSE 431 Computer Architecture Fall 2005 Lecture 03: MIPS Arithmetic Review Mary Jane Irwin (
Computer Systems Organization: Lecture 3
1 Chapter 4: Arithmetic Where we've been: –Performance (seconds, cycles, instructions) –Abstractions: Instruction Set Architecture Assembly Language and.
Arithmetic I CPSC 321 Andreas Klappenecker. Administrative Issues Office hours of TA Praveen Bhojwani: M 1:00pm-3:00pm.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
1  1998 Morgan Kaufmann Publishers Chapter Four Arithmetic for Computers.
Chapter 3 Arithmetic for Computers. Arithmetic Where we've been: Abstractions: Instruction Set Architecture Assembly Language and Machine Language What's.
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 Bits are just bits (no inherent meaning) — conventions define relationship between bits and numbers Binary numbers (base 2)
Computer Arithmetic Nizamettin AYDIN
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
Computing Systems Basic arithmetic for computers.
ECE232: Hardware Organization and Design
CS35101 Computer Architecture Spring 2006 Week 8 P Durand ( [Adapted from MJI ( [Adapted from Dave Patterson’s.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
1 EGRE 426 Fall 08 Chapter Three. 2 Arithmetic What's up ahead: –Implementing the Architecture 32 operation result a b ALU.
1  1998 Morgan Kaufmann Publishers Arithmetic Where we've been: –Performance (seconds, cycles, instructions) –Abstractions: Instruction Set Architecture.
Computer Arithmetic II Instructor: Mozafar Bag-Mohammadi Ilam University.
Computer Architecture Chapter 3 Instructions: Arithmetic for Computer Yu-Lun Kuo 郭育倫 Department of Computer Science and Information Engineering Tunghai.
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.
Restoring Unsigned Integer Division
CPE 232 MIPS Arithmetic1 CPE 232 Computer Organization MIPS Arithmetic – Part I Dr. Gheith Abandah [Adapted from the slides of Professor Mary Irwin (
1 ELEN 033 Lecture 4 Chapter 4 of Text (COD2E) Chapters 3 and 4 of Goodman and Miller book.
EI 209 Chapter 3.1CSE, 2015 EI 209 Computer Organization Fall 2015 Chapter 3: Arithmetic for Computers Haojin Zhu ( )
CS 224 Computer Organization Spring 2011 Arithmetic for Computers With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture slide contents.
순천향대학교 정보기술공학부 이 상 정 1 3. Arithmetic for Computers.
Prof. Hsien-Hsin Sean Lee
Computer Arthmetic Chapter Four P&H. Data Representation Why do we not encode numbers as strings of ASCII digits inside computers? What is overflow when.
9/23/2004Comp 120 Fall September Chapter 4 – Arithmetic and its implementation Assignments 5,6 and 7 posted to the class web page.
1 CPTR 220 Computer Organization Computer Architecture Assembly Programming.
CSE 340 Computer Architecture Spring 2016 MIPS Arithmetic Review.
Based on slides from D. Patterson and www-inst.eecs.berkeley.edu/~cs152/ COM 249 – Computer Organization and Assembly Language Chapter 3 Arithmetic For.
William Stallings Computer Organization and Architecture 8th Edition
Mary Jane Irwin ( ) [Adapted from Computer Organization and Design,
Integer Multiplication and Division
Arithmetic for Computers
Chapter Three : Arithmetic for Computers
XU, Qiang 徐強 [Adapted from UC Berkeley’s D. Patterson’s and
CS 314 Computer Organization Fall Chapter 3: Arithmetic for Computers
Morgan Kaufmann Publishers
CDA 3101 Summer 2007 Introduction to Computer Organization
ECE232: Hardware Organization and Design
Computer Arithmetic Multiplication, Floating Point
ECEG-3202 Computer Architecture and Organization
Chapter 3 Arithmetic for Computers
CSC3050 – Computer Architecture
Morgan Kaufmann Publishers Arithmetic for Computers
Chapter 3 Arithmetic for Computers
Number Representation
Presentation transcript:

CS.305 Computer Architecture Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005, and from slides kindly made available by Dr Mary Jane Irwin, Penn State University. MIPS Arithmetic

CS305_04/2 Review: MIPS Organization Processor Memory 32 bits 2 30 words read/write addr read data write data word address (binary) 0…0000 0…0100 0…1000 0…1100 1…1100 Register File src1 addr src2 addr dst addr write data 32 bits src1 data src2 data 32 registers ($zero - $ra) 325 PC ALU byte address (big Endian) Fetch PC = PC+4 Decode Exec Add 4 branch offset 32 55

MIPS ArithmeticCS305_04/3  32-bit signed numbers (2’s complement): two = 0 ten two = + 1 ten two = + 2,147,483,646 ten two = + 2,147,483,647 ten two = – 2,147,483,648 ten two = – 2,147,483,647 ten two = – 2 ten two = – 1 ten MIPS Number Representations maxint minint  Converting <32-bit values into 32-bit values  copy the most significant bit (the sign bit) into the “empty” bits > >  sign extend versus zero extend ( lb vs. lbu ) MSB LSB

MIPS ArithmeticCS305_04/4 Recap: Unsigned Integers  Since, for example, memory addresses as numeric values will always be positive they will, in MIPS, represent the range of values: 0 to  Whereas, signed values represent the range: to +( )  Programming languages reflect this distinction. C/C++ for example has the fundamental types integer ( int ) and unsigned integer ( unsigned int ).  Comparison instructions, for example, must cope with both forms of integer. Unsigned versions of slt and slti are, unsurprisingly: set on less than unsigned ( sltu ) set on less than immediate unsigned ( sltiu )

MIPS ArithmeticCS305_04/5 MIPS Arithmetic Logic Unit (ALU)  Must support the Arithmetic/Logic operations of the ISA: add, addi, addiu, addu sub, subu, neg mult, multu, div, divu sqrt and, andi, nor, or, ori, xor, xori beq, bne, slt, slti, sltiu, sltu 32 m (operation) result A B ALU 4 zeroovf 1 1  With special handling for:  sign extend – addi, addiu, slti, sltiu  zero extend – andi, ori, xori, lbu  no overflow detected – addu, addiu, subu, multu, divu, slt, sltu, slti, sltiu

MIPS ArithmeticCS305_04/6 Review: 2’s Complement Binary Represent'n 2’sc binarydecimal = -( ) = -2 3 = 1010 complement all the bits 1011 and add a 1  Note: negate and invert are different!  Negate

MIPS ArithmeticCS305_04/7 Review: A Full Adder 1-bit Full Adder A B S carry_in carry_out S = A  B  carry_in (odd parity function) carry_out = A&B | A&carry_in | B&carry_in (majority function) ABcarry_incarry_outS  How can we use it to build a 32-bit adder?  How can we modify it to build an adder/subtractor?

MIPS ArithmeticCS305_04/8 A 32-bit Ripple Carry Adder/Subtractor A 0111  0111 B  + 1-bit FA S0S0 c 0 =carry_in c1c1 1-bit FA S1S1 c2c2 S2S2 c3c3 c 32 =carry_out 1-bit FA S 31 c A0A0 A1A1 A2A2 A 31 B0B0 B1B1 B2B2 B 31 add/sub B0B0 control (0=add,1=sub) B 0 if control = 0, !B 0 if control =  Remember 2’s complement is just  complement all the bits  Add a 1 to the least significant bit

MIPS ArithmeticCS305_04/9 Overflow Detection – –4 –  Overflow: the result is too large to represent in 32 bits  Overflow occurs when  adding two positives yields a negative  or, adding two negatives gives a positive  or, subtract a negative from a positive gives a negative  or, subtract a positive from a negative gives a positive  Detecting overflow by:  Carry into MSB xor Carry out of MSB

MIPS ArithmeticCS305_04/10 Overflow Conditions  Overflow conditions for addition and subtraction are summarised in the table below: OperationOperand AOperand BResult A + B+ve -ve A + B-ve +ve A - B+ve-ve A - B-ve+ve Overflow conditions for addition and subtraction

MIPS ArithmeticCS305_04/11 Overflow Handling  Q: What happens when an overflow occurs?  Answer(s):  Even though a processor like MIPS detects overflow with an exception (an internal interrupt), a programming language - e.g. C - may choose to ignore it.  Other languages like Ada, Fortran, and Lisp require that the program be notified - usually by means of an exception handler.

MIPS ArithmeticCS305_04/12 …Overflow Handling  Q: Does unsigned integer arithmetic overflow?  The simple answer is "yes, but it doesn't matter!". The reason is that unsigned integers are commonly used for memory addresses and they are deemed to have a finite limit.  The computer designer must therefore provide a way to ignore overflow in some cases and to recognise it in others.  The MIPS solution is to have two kinds of arithmetic instructions to cater for the two choices:  add ( add ), add immediate ( addi ), and subtract ( sub ) cause exceptions on overflow, and  add unsigned ( addu ), add immediate unsigned ( addiu ), and subtract unsigned ( subu ) do not cause exceptions on overflow.

MIPS ArithmeticCS305_04/13  Need to support the logic operation ( and,nor,or,xor )  Bit wise operations (no carry operation involved)  Need a logic gate for each function, mux to choose the output  Need to support the set-on-less-than instruction ( slt )  Use subtraction to determine if (a – b) < 0 (implies a < b)  Copy the sign bit into the low order bit of the result, set remaining result bits to 0  Need to support test for equality ( bne, beq )  Again use subtraction: (a - b) = 0 implies a = b  Additional logic to “nor” all result bits together  Immediates are sign extended outside the ALU with wiring (i.e., no logic needed) Tailoring the ALU to the MIPS ISA

MIPS ArithmeticCS305_04/14 Logical Shift Operations  Also need operations to pack and unpack 8-bit characters into 32-bit words  Shifts move all the bits in a word left or right sll $t2,$s0,8#$t2 = $s0 > 8 bits  Notice that a 5-bit shamt field is enough to shift a 32-bit value 2 5 – 1 or 31 bit positions  Such shifts are logical because they fill with zeros  Notice that the register operands are in fact: s … $rd,$rt, … #$rd = $rt … R-formatop0rtrdshamtfunct

MIPS ArithmeticCS305_04/15 Arithmetic Shift Operations  An arithmetic shift ( sra ) maintain the arithmetic correctness of the shifted value (i.e., a number shifted right one bit should be ½ of its original value; a number shifted left should be 2 times its original value)  so sra uses the most significant bit (sign bit) as the bit shifted in sra $t2,$s0,8#$t2 = $s0 >> 8 bits  note that there is no need for a sla when using two’s complement number representation  The shift operation is implemented by hardware separate from the ALU  using a barrel shifter (which would takes lots of gates in discrete logic, but is pretty easy to implement in VLSI)

MIPS ArithmeticCS305_04/16 Multiply  Binary multiplication is just a bunch of right shifts and adds multiplicand multiplier partial product array double precision product n 2n n can be formed in parallel and added in parallel for faster multiplication

MIPS ArithmeticCS305_04/17  Multiply produces a double precision product mult $s0, $s1 # hi||lo = $s0 * $s1 MIPS Multiply Instruction  Low-order word of the product is left in processor register lo and the high-order word is left in register hi  Instructions mfhi rd and mflo rd are provided to move the product to (user accessible) registers in the register file  Multiplies are done by fast, dedicated hardware and are much more complex (and slower) than adders  Hardware dividers are even more complex and even slower; ditto for hardware square root R-formatoprsrt00funct

MIPS ArithmeticCS305_04/18 Division  Division is just a bunch of quotient digit guesses and left shifts and subtracts dividend divisor partial remainder array quotient n n remainder n

MIPS ArithmeticCS305_04/19  Divide generates the reminder in hi and the quotient in lo div $s0, $s1 # lo = $s0 / $s1 # hi = $s0 mod $s1 MIPS Divide Instruction  Instructions mfhi rd and mflo rd are provided to move the quotient and reminder to (user accessible) registers in the register file  As with multiply, divide ignores overflow so software must determine if the quotient is too large. Software must also check the divisor to avoid division by 0. R-formatoprsrt00funct

MIPS ArithmeticCS305_04/20 Representing Big (and Small) Numbers  Q: How would we encode the approx. age of the earth? 4,600,000,000 or 4.6 x 10 9 or the weight in kg of one a.m.u. (atomic mass unit)? or 1.6 x There is no way we can encode either in a 32-bit integer.  A: Floating point representation (-1) sign x F x 2 E  Still have to fit everything in 32 bits (single precision)  The base (2, not 10) is hardwired in the design of the FPALU  More bits in the fraction (F) or the exponent (E) is a trade-off between precision (accuracy of the number) and range (size of the number) sEF 1-bit sign 8-bit Exponent 23-bit Fraction

MIPS ArithmeticCS305_04/21 IEEE 754 FP Standard Encoding  Most (all?) computers these days conform to the IEEE 754 FP standard (-1) sign x (1+F) x 2 E-bias  Formats for both single and double precision  F is stored in normalized form where the msb in the fraction is 1 (so there is no need to store it!) – called the hidden bit  To simplify sorting FP numbers, E comes before F in the word and E is represented in excess (biased) notation Single PrecisionDouble PrecisionObject Represented E (8)F (23)E (11)F (52) 0000true zero (0) 0nonzero0 ± denormalized number ± 1-254anything± anything± floating point number ± 2550± 20470± infinity 255nonzero2047nonzeronot a number (NaN)

MIPS ArithmeticCS305_04/22 Floating Point Addition  Addition (and subtraction) (  F1  2 E1 ) + (  F2  2 E2 ) =  F3  2 E3  Step 1: Restore the hidden bit in F1 and in F2  Step 1: Align fractions by right shifting F2 by E1 - E2 positions (assuming E1  E2) keeping track of (three of) the bits shifted out in a round bit, a guard bit, and a sticky bit  Step 2: Add the resulting F2 to F1 to form F3  Step 3: Normalize F3 (so it is in the form 1.XXXXX …) If F1 and F2 have the same sign  F3  [1,4)  1 bit right shift F3 and increment E3 If F1 and F2 have different signs  F3 may require many left shifts each time decrementing E3  Step 4: Round F3 and possibly normalize F3 again  Step 5: Rehide the most significant bit of F3 before storing the result

MIPS ArithmeticCS305_04/23 MIPS Floating Point Instructions  MIPS has a separate Floating Point Register File ( $f0, $f1, …, $f31 ) (whose registers are used in pairs for double precision values) with special instructions to load to and store from them: lwcl $f1,54($s2)#$f1 = Memory[$s2+54] swcl $f1,58($s4)#Memory[$s4+58] = $f1  And supports IEEE 754 single: add.s $f2,$f4,$f6#$f2 = $f4 + $f6 and double precision operations: add.d $f2,$f4,$f6#$f2||$f3 = … # … $f4||$f5 + $f6||$f7 Similarly for: sub.s, sub.d, mul.s, mul.d, div.s, div.d

MIPS ArithmeticCS305_04/24 …MIPS Floating Point Instructions  And floating point single precision comparison operations c.x.s $f2,$f4#if($f2 < $f4) cond=1; else cond=0 where x may be eq, neq, lt, le, gt, ge  And branch operations bclt 25#if(cond==1) go to PC+4+(25<<2) bclf 25#if(cond==0) go to PC+4+(25<<2)  And double precision comparison operations c.x.d $f2,$f4#if($f2||$f3 < $f4||$f5) cond=1; else cond=0

MIPS ArithmeticCS305_04/25 Fallacies and Pitfalls  Fallacy: Floating-point addition is associative; i.e., x+(y+z) = (x+y)+z  Problems occur when adding two large numbers of opposite signs plus a small number.  Fallacy: …a right shift is the same as an integer division by a power of 2.  This may be true for unsigned integers but it is not for signed integers. Arithmetic right shifts that preserves the sign bit still produce incorrect results. Consider a 2-bt shift right of -5 (-5/4) that results in (-2): two two  Pitfall: The MIPS instruction add immediate unsigned ( addiu ) sign extends its 16-bit immediate field.  Despite its name is used to add constants to signed integers when we don’t care about overflow. I.e. for C programs compiled for MIPS addiu is the 2nd most common instruction used (12%) [see Fig 3.26, P&H]  As MIPS does not have a subtract immediate and negative numbers need sign extension this design decision was made.