# Number-crunching - PowerPoint PPT Presentation  Number-crunching

## Number-crunching

##### Presentation Transcript

1. Arithmetic CPSC 321 Computer Architecture Andreas Klappenecker

2. Overview • Number representations • Overflows • Floating point numbers • Arithmetic logic units

3. Unsigned Numbers • 32 bits are available • Range 0..232 -1 • 11012 = 23+22+20 = 1310 • Upper bound 232 –1 = 4 294 967 295

4. Number representations What signed integer number representations do you know?

5. Signed Numbers • Sign-magnitude representation • MSB represents sign, 31bits for magnitude • One’s complement • Use 0..231-1 for non-negative range • Invert all bits for negative numbers • Two’s complement • Same as one’s complement except • negative numbers are obtained by inverting all bits and adding 1

6. Advantages and Disadvantages • sign-magnitude representation • one’s complement representation • two’s complement representation

7. Signed Numbers (3bits)

8. Two’s complement • The unsigned sum of an n-bit number its negative yields? • Example with 3 bits: • 0112 • 1012 • 10002 = 2n => negate(x) = 2n-x • Explain one’s complement

9. MIPS 32bit signed numbers 0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0001two = +1ten 0000 0000 0000 0000 0000 0000 0000 0010two = +2ten ... 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 1000 0000 0000 0000 0000 0000 0000 0010two = –2,147,483,646ten ... 1111 1111 1111 1111 1111 1111 1111 1101two = –3ten 1111 1111 1111 1111 1111 1111 1111 1110two = –2ten 1111 1111 1111 1111 1111 1111 1111 1111two = –1ten

10. Conversions How do you convert an n-bit number into a 2n-bit number? (Assume two’s complement representation)

11. Conversions • Suppose that you have 3bit two’s complement number • 1012 = -3 • Convert into a 6bit two’s complement number • 1111012 = -3 • Replicate most significant bit!

12. Comparisons What can go wrong if you accidentally compare unsigned with signed numbers?

13. Comparisons for [un]signed • Register \$s0 • 1111 1111 1111 1111 1111 1111 1111 1111 • Register \$s1 • 0000 0000 0000 0000 0000 0000 0000 0001 • Compare registers (set less than) • slt \$t0, \$s0, \$s1 yes, since –1 < 1 • sltu \$t1, \$s0, \$s1 no, since 232-1>1

14. Addition & Subtraction • Just like in grade school (carry/borrow 1s)0111 0111 0110+ 0110 - 0110 - 0101 • Two's complement operations easy • subtraction using addition of negative numbers0111 + 1010

15. Overflow Overflow means that the result is too large for a finite computer word • for example, adding two n-bit numbers does not yield an n-bit number0111 + 0001 1000 • the term overflow is somewhat misleading

16. 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

17. Detecting Overflow

18. Effects of Overflow • An exception (interrupt) occurs • Control jumps to predefined address for exception • Interrupted address is saved for possible resumption • Don't always want to detect overflow • MIPS instructions: addu, addiu, subunote: addiu still sign-extends!

19. What next? • More MIPS assembly operations • How does an ALU work? • Simple digital logic design • How can we speed-up addition? • What about multiplication?