220 likes | 528 Views
Arithmetic. CPSC 321 Computer Architecture Andreas Klappenecker . Overview. Number representations Overflows Floating point numbers Arithmetic logic units. Unsigned Numbers. 32 bits are available Range 0..2 32 -1 1101 2 = 2 3 +2 2 +2 0 = 13 10 Upper bound 2 32 –1 = 4 294 967 295.
E N D
Arithmetic CPSC 321 Computer Architecture Andreas Klappenecker
Overview • Number representations • Overflows • Floating point numbers • Arithmetic logic units
Unsigned Numbers • 32 bits are available • Range 0..232 -1 • 11012 = 23+22+20 = 1310 • Upper bound 232 –1 = 4 294 967 295
Number representations What signed integer number representations do you know?
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
Advantages and Disadvantages • sign-magnitude representation • one’s complement representation • two’s complement representation
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
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
Conversions How do you convert an n-bit number into a 2n-bit number? (Assume two’s complement representation)
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!
Comparisons What can go wrong if you accidentally compare unsigned with signed numbers?
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
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
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
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
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!
What next? • More MIPS assembly operations • How does an ALU work? • Simple digital logic design • How can we speed-up addition? • What about multiplication?