Arithmetic for Computers

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.
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.
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.
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
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
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.
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
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
Computing Systems Basic arithmetic for computers.
COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 5 Unsigned integer and floating point.
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.
Lecture 9: Floating Point
Computer Architecture Chapter 3 Instructions: Arithmetic for Computer Yu-Lun Kuo 郭育倫 Department of Computer Science and Information Engineering Tunghai.
CS.305 Computer Architecture Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005, and from slides kindly made available by Dr Mary.
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 ( )
Integer Operations Computer Organization and Assembly Language: Module 5.
CSE 340 Computer Architecture Spring 2016 MIPS Arithmetic Review.
Chapter 9 Computer Arithmetic
William Stallings Computer Organization and Architecture 8th Edition
Mary Jane Irwin ( ) [Adapted from Computer Organization and Design,
/ Computer Architecture and Design
Morgan Kaufmann Publishers Arithmetic for Computers
Integer Multiplication and Division
Computer Architecture & Operations I
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
CS/COE 0447 (term 2181) Jarrett Billingsley
James Gosling Sun Fellow Java Inventor
William Stallings Computer Organization and Architecture 7th Edition
CSCE 350 Computer Architecture
ECE232: Hardware Organization and Design
Computer Arithmetic Multiplication, Floating Point
ECEG-3202 Computer Architecture and Organization
Review.
Computer Architecture
Addition & Subtraction
Chapter 8 Computer Arithmetic
Chapter 3 Arithmetic for Computers
Morgan Kaufmann Publishers Arithmetic for Computers
Chapter 3 Arithmetic for Computers
Number Representation
Presentation transcript:

Arithmetic for Computers Chapter 3 Arithmetic for Computers

Chapter 3 Table of Contents Signed and Unsigned Numbers Addition and Subtraction Multiplication Division Floating Point Real Stuff: Floating Point in the IA-32

MIPS Organization So Far Processor Memory Register File 1…1100 32 registers ($zero - $ra) 32 bits src1 addr src2 addr dst addr write data src1 data src2 data 5 32 32 read / write address 5 5 32 32 32 32 230 words read data branch offset 32 32 PC Add 32 32 Add 32 32 4 0…1100 write data Fetch PC = PC+4 Decode Exec 32 0…1000 32 4 5 6 7 0…0100 32 ALU 1 2 3 0…0000 32 word address (binary) 32 bits 32 byte address (big Endian)

MIPS ISA So Far Category Instr Op Code Example Meaning Arithmetic (R & I format) add 0 and 32 add $s1,$s2,$s3 $s1 = $s2 + $s3 subtract 0 and 34 sub $s1,$s2,$s3 $s1 = $s2 - $s3 add immediate 8 addi $s1, $s2, 6 $s1 = $s2 + 6 or immediate 13 ori $s1, $s2, 6 $s1 = $s2 v 6 Data Transfer (I format) load word 35 lw $s1,24($s2) $s1 = Memory($s2+24) store word 43 sw $s1,24($s2) Memory($s2+24) = $s1 load byte 32 lb $s1,25($s2) $s1 = Memory($s2+25) store byte 40 sb $s1,25($s2) Memory($s2+25) = $s1 load upper imm 15 lui $s1, 6 $s1 = 6 * 216 Cond. Branch (I & R format) br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L set on less than 0 and 42 slt $s1,$s2,$s3 if ($s2<$s3) $s1=1 else $s1=0 set on less than immediate 10 slti $s1,$s2,6 if ($s2<6) $s1=1 else $s1=0 Uncond. Jump (J & R format) jump 2 j 2500 go to 10000 jump register 0 and 8 jr $t1 go to $t1 jump and link 3 jal 2500 go to 10000; $ra=PC+4

MIPS Number Representations 32-bit signed numbers (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 1110two = + 2,147,483,646ten 0111 1111 1111 1111 1111 1111 1111 1111two = + 2,147,483,647ten 1000 0000 0000 0000 0000 0000 0000 0000two = – 2,147,483,648ten 1000 0000 0000 0000 0000 0000 0000 0001two = – 2,147,483,647ten ... 1111 1111 1111 1111 1111 1111 1111 1110two = – 2ten 1111 1111 1111 1111 1111 1111 1111 1111two = – 1ten MSB LSB maxint minint Converting <32-bit values into 32-bit values copy the most significant bit (the sign bit) into the “empty” bits 0010 -> 0000 0010 1010 -> 1111 1010 sign extend versus zero extend ( lb vs. lbu )

2’s Complement Binary Representation 2’sc binary decimal 1000 -8 1001 -7 1010 -6 1011 -5 1100 -4 1101 -3 1110 -2 1111 -1 0000 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 -23 = Negate -(23 - 1) = 1011 and add a 1 1010 complement all the bits Note: negate and invert are different! 23 - 1 =

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

A 32-bit Ripple Carry Adder/Subtractor add/sub 1-bit FA S0 c0=carry_in c1 S1 c2 S2 c3 c32=carry_out S31 c31 . . . A0 A1 A2 A31 Remember 2’s complement is just complement all the bits add a 1 in the least significant bit B0 control (0=add,1=sub) B0 if control = 0, !B0 if control = 1 A 0111  0111 B - 0110  + 1001 1 0001 1 0001

Overflow Detection 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 On your own: Prove you can detect overflow by: Carry into MSB xor Carry out of MSB, ex for 4 bit signed numbers 1 1 1 1 1 1 1 + 7 3 1 + –4 – 5 – 6 1 1 7

Tailoring the ALU to the MIPS ISA 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)

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 srl $t2, $s0, 8 #$t2 = $s0 >> 8 bits op rs rt rd shamt funct Notice that a 5-bit shamt field is enough to shift a 32-bit value 25 – 1 or 31 bit positions Such shifts are “logical shifts” because they fill with zeros

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 note that there is no need for a sla when using two’s complement number representation sra $t2, $s0, 8 #$t2 = $s0 >> 8 bits 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)

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

MIPS Multiply Instruction Multiply produces a double precision product mult $s0, $s1 # hi||lo = $s0 * $s1 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 op rs rt rd shamt funct 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

Division Division is just a bunch of quotient digit guesses and left shifts and subtracts n n quotient dividend divisor partial remainder array remainder n

MIPS Divide Instruction Divide generates the reminder in hi and the quotient in lo div $s0, $s1 # lo = $s0 / $s1 # hi = $s0 mod $s1 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. op rs rt rd shamt funct

Representing Big (and Small) Numbers What if we want to encode the approx. age of the earth? 4,600,000,000 or 4.6 x 109 or the weight in kg of one a.m.u. (atomic mass unit) 0.0000000000000000000000000166 or 1.6 x 10-27 It is not possible to encode either of the above in a 32-bit integer. Floating point representation: (-1)sign x F x 2E Still have to fit everything in 32 bits (single precision) The base (2, not 10) is hardwired in the design of the FP ALU 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) s E (exponent) F (fraction) 1 bit 8 bits 23 bits

IEEE 754 FP Standard Encoding Most (all?) computers these days conform to the IEEE 754 floating point standard (-1)sign x (1+F) x 2E-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 Precision Double Precision Object Represented E (8) F (23) E (11) F (52) true zero (0) nonzero ± denormalized number ± 1-254 anything ± 1-2046 ± floating point number ± 255 ± 2047 ± infinity 255 2047 not a number (NaN)

Floating Point Addition Addition (and subtraction) ( F1  2E1) + ( F2  2E2) =  F3  2E3 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 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 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 bclf 25 #if(cond==0) go to PC+4+25 And double precision comparison operations c.x.d $f2,$f4 #$f2||$f3 < $f4||$f5 cond=1; else cond=0