Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Review: MIPS Organization

Processor
src1 addr src2 addr dst addr write data 5 32 32 bits branch offset 32 PC 4 32 Add 32 Add 32 32 ALU 32 32 32 src1 32 data

Memory
11100

5 5

Register File 32 registers ($zero - $ra)

src2 32 data

read/write addr
32

230 words

read data
32

Fetch PC = PC+4 Exec

write data
32 32 4 0 5 1 6 2 7 3

Decode

32 bits byte address (big Endian)

01100 01000 00100 00000 word address (binary)

CSE 338 Computer Organization CHAPTER 3

Chapter 3 Arithmetic for Computers


Bits (no inherent meaning) conventions define relationship between bits and numbers Binary numbers (base 2) 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001... decimal: 0...2n-1 Of course it gets more complicated: numbers are finite (overflow) fractions and real numbers negative numbers e.g., no MIPS subi instruction; addi can add a negative number How do we represent negative numbers? i.e., which bit patterns will represent which numbers?

CSE 338 Computer Organization CHAPTER 3

Possible Representations
Sign Magnitude: One's Complement Two's Complement

000 = +0 001 = +1 010 = +2 011 = +3 100 = -0 101 = -1 110 = -2 111 = -3

000 = +0 001 = +1 010 = +2 011 = +3 100 = -3 101 = -2 110 = -1 111 = -0

000 = +0 001 = +1 010 = +2 011 = +3 100 = -4 101 = -3 110 = -2 111 = -1

Issues: balance, number of zeros, ease of operations Which one is best?

CSE 338 Computer Organization CHAPTER 3

MIPS Number Representations


32-bit signed numbers (2s complement):
0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0001two = + 1ten ... 0111 0111 1000 1000 ... 1111 1111 0000 0000 1111 1111 0000 0000 1111 1111 0000 0000 1111 1111 0000 0000 1111 1111 0000 0000 1111 1111 0000 0000 1110two 1111two 0000two 0001two = = = = + + 2,147,483,646ten 2,147,483,647ten 2,147,483,648ten 2,147,483,647ten

maxint

1111 1111 1111 1111 1111 1111 1111 1110two = 2ten 1111 1111 1111 1111 1111 1111 1111 1111two = 1ten

MSB LSB

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)

CSE 338 Computer Organization CHAPTER 3

Two's Complement Operations


Negating a two's complement number: invert all bits and add 1 remember: negate and invert are quite different! Converting n bit numbers into numbers with more than n bits: MIPS 16 bit immediate gets converted to 32 bits for arithmetic copy the most significant bit (the sign bit) into the other bits 0010 -> 0000 0010 1010 -> 1111 1010

"sign extension" (lbu vs. lb)

CSE 338 Computer Organization CHAPTER 3

Review: A Full Adder


carry_in
A 0 0 B 0 0 1 1 0 0 1 1 carry_in 0 1 0 1 0 1 0 1 carry_out 0 0 0 1 0 1 1 1 S 0 1 1 0 1 0 0 1

A B

1-bit Full Adder carry_out

0 1 1 1 1

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?
CSE 338 Computer Organization CHAPTER 3

A 32-bit Ripple Carry Adder/Subtractor


Remember 2s complement is just
complement all the bits
control (0=add,1=sub) B0 B0 if control = 0, !B0 if control = 1

add/sub A0 B0 A1 B1 A2

c0=carry_in 1-bit FA c1 1-bit FA c2 1-bit FA c3 ... S0

S1

S2

add a 1 in the least significant bit A 0111 B - 0110 0001 0111 + 1001 1 1 0001

B2

c31 A31 B31 1-bit FA S31

c32=carry_out

CSE 338 Computer Organization CHAPTER 3

Addition & Subtraction


Just like in primary school (carry/borrow 1s) 0111 0111 + 0110 - 0110 Two's complement operations easy subtraction using addition of negative numbers 0111 + 1010 Overflow (result too large for finite computer word): e.g., adding two n-bit numbers does not yield an n-bit number 0111 note that overflow term is somewhat misleading, + 0001 1000 it does not mean a carry overflowed
CSE 338 Computer Organization CHAPTER 3

0110 - 0101

Detecting Overflow
No overflow when adding a positive and a negative number No overflow when signs are the same for subtraction Overflow occurs when the value affects the sign: overflow when adding two positives yields a negative or, adding two negatives gives a positive or, subtract a negative from a positive and get a negative or, subtract a positive from a negative and get a positive

CSE 338 Computer Organization CHAPTER 3

Effects of Overflow
An exception (interrupt) occurs Control jumps to predefined address for exception Interrupted address is saved for possible resumption Details based on software system / language Don't always want to detect overflow new MIPS instructions: addu, addiu, subu

note: addiu still sign-extends! note: sltu, sltiu for unsigned comparisons

CSE 338 Computer Organization CHAPTER 3

10

Multiply
Binary multiplication is just a bunch of right shifts and adds

n
multiplicand multiplier partial product array can be formed in parallel and added in parallel for faster multiplication

double precision product

2n
Negative numbers: convert and multiply ** there are better techniques
CSE 338 Computer Organization CHAPTER 3

11

Multiplication: Implementation
Start

Multiplier0 = 1

1. Test Multiplier0

Multiplier0 = 0

Multiplicand Shift left 64 bits


1a. Add multiplicand to product and place the result in Product register

64-bit ALU

Multiplier Shift right 32 bits


2. Shift the Multiplicand register left 1 bit

Product Write 64 bits

Control test
3. Shift the Multiplier register right 1 bit

No: < 32 repetitions 32nd repetition?

Datapath Control
CSE 338 Computer Organization CHAPTER 3
Yes: 32 repetitions Done

12

Final Version
Start

Multiplicand 32 bits
Product0 = 1 1. Test Product0 Product0 = 0

32-bit ALU

1a. Add multiplicand to the left half of the product and place the result in the left half of the Product register

Product 64 bits

Shift right Write

Control test

2. Shift the Product register right 1 bit

32nd repetition?

No: < 32 repetitions

Initialization: Assign multiplier to the right half of product

Yes: 32 repetitions

Done

CSE 338 Computer Organization CHAPTER 3

13

MIPS Multiply Instruction


Multiply produces a double precision product mult $s0, $s1 # hi||lo = $s0 * $s1
op rs rt rd shamt funct

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

CSE 338 Computer Organization CHAPTER 3

14

Division (first version)

CSE 338 Computer Organization CHAPTER 3

15

Division (improved version)

Start

1. Shift the Remainder register left 1 bit

2. Subtract the Divisor register from the left half of the Remainder register and place the result in the left half of the Remainder register

Remainder > 0

Test Remainder

Remainder < 0

3a. Shift the Remainder register to the left, setting the new rightmost bit to 1

3b. Restore the original value by adding the Divisor register to the left half of the Remainder register and place the sum in the left half of the Remainder register. Also shift the Remainder register to the left, setting the new rightmost bit to 0

32nd repetition?

No: < 32 repetitions

Yes: 32 repetitions

Done. Shift left half of Remainder right 1 bit

CSE 338 Computer Organization CHAPTER 3

16

MIPS Divide Instruction


Divide generates the reminder in hi and the quotient in lo div $s0, $s1 # lo = $s0 / $s1 # hi = $s0 mod $s1
op rs rt rd shamt funct

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.

CSE 338 Computer Organization CHAPTER 3

17

Floating Point (a brief look)


We need a way to represent numbers with fractions, e.g., 3.1416 very small numbers, e.g., .000000001 very large numbers, e.g., 3.15576 109 Representation: sign, exponent, significand: (1)sign significand 2exponent more bits for significand gives more accuracy more bits for exponent increases range IEEE 754 floating point standard: single precision: 8 bit exponent, 23 bit significand double precision: 11 bit exponent, 52 bit significand
CSE 338 Computer Organization CHAPTER 3

18

IEEE 754 floating-point standard


Leading 1 bit of significand is implicit Exponent is biased to make sorting easier all 0s is smallest exponent all 1s is largest bias of 127 for single precision and 1023 for double precision summary: (1)sign (1+significand) 2exponent bias Example: decimal: -.75 = - ( + ) binary: -.11 = -1.1 x 2-1 floating point: exponent = 126 = 01111110 IEEE single precision: 10111111010000000000000000000000
CSE 338 Computer Organization CHAPTER 3

19

Floating point addition


Start

1. Compare the exponents of the two numbers. Shift the smaller number to the right until its exponent would match the larger exponent

2. Add the significands

3. Normalize the sum, either shifting right and incrementing the exponent or shifting left and decrementing the exponent

Overflow or underflow? No

Yes

Exception

4. Round the significand to the appropriate number of bits

No Still normalized?

Yes

CSE 338 Computer Organization CHAPTER 3


Done

20

Floating point addition


Sign Exponent Fraction Sign Exponent Fraction

Small ALU

Exponent difference 0 1 0 1 0 1

Control

Shift right

Big ALU

Increment or decrement

Shift left or right

Rounding hardware

Sign

Exponent

Fraction

CSE 338 Computer Organization CHAPTER 3

21

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 swcl $f1,54($s2) $f1,58($s4) #$f1 = Memory[$s2+54] #Memory[$s4+58] = $f1

And supports IEEE 754 single add.s $f2,$f4,$f6 #$f2 = $f4 + $f6 and double precision operations #$f2||$f3 = $f4||$f5 + $f6||$f7 similarly for sub.s, sub.d, mul.s, mul.d, div.s, div.d add.d $f2,$f4,$f6

CSE 338 Computer Organization CHAPTER 3

22

MIPS Floating Point Instructions, Cont


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 #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 and branch operations bclt 25

CSE 338 Computer Organization CHAPTER 3

23

Floating Point Complexities


Operations are somewhat more complicated (see text) In addition to overflow we can have underflow Accuracy can be a big problem IEEE 754 keeps two extra bits, guard and round four rounding modes positive divided by zero yields infinity zero divide by zero yields not a number other complexities Read the text for description of 80x86 and Pentium bug!

CSE 338 Computer Organization CHAPTER 3

24

Chapter Three Summary


Computer arithmetic is constrained by limited precision Bit patterns have no inherent meaning but standards do exist twos complement IEEE 754 floating point Computer instructions determine meaning of the bit patterns Performance and accuracy are important so there are many complexities in real machines Algorithm choice is important and may lead to hardware optimizations for both space and time (e.g., multiplication)

CSE 338 Computer Organization CHAPTER 3

25

You might also like