CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson & Hennessy, ©2005 Some slides and/or pictures in the following are adapted from: slides ©2008 UCB 10
So far, unsigned numbers Obvious solution: define leftmost bit to be sign! –0 +, 1 – –Rest of bits can be numerical value of number Representation called sign and magnitude MIPS uses 32-bit integers. +1 ten would be: And –1 ten in sign and magnitude would be: How to Represent Negative Numbers?
Arithmetic circuit complicated –Special steps depending whether signs are the same or not Also, two zeros – 0x = +0 ten – 0x = –0 ten –What would two 0s mean for programming? Therefore sign and magnitude abandoned Shortcomings of sign and magnitude?
Example: 7 10 = –7 10 = Called One’s Complement Note: positive numbers have leading 0s, negative numbers have leadings 1s What is ? Answer: How many positive numbers in N bits? How many negative numbers? Another try: complement the bits
Arithmetic still somewhat complicated Still two zeros – 0x = +0 ten – 0xFFFFFFFF = -0 ten Although used for awhile on some computer products, one’s complement was eventually abandoned because another solution was better Shortcomings of One’s complement?
32 bits can only represent 2 32 numbers – if we wish to also represent negative numbers, we can represent 2 31 positive numbers (incl zero) and 2 31 negative numbers two = 0 ten two = 1 ten … two = two = two = -(2 31 – 1) two = -(2 31 – 2) … two = two = -1 Negative Numbers
two = 0 ten two = 1 ten … two = two = two = -(2 31 – 1) two = -(2 31 – 2) … two = two = -1 Why is this representation favorable? Consider the sum of 1 and -2 …. we get -1 Consider the sum of 2 and -1 …. we get +1 This format can directly undergo addition without any conversions! Each number represents the quantity x x x … + x x ’s Complement
two = 0 ten two = 1 ten … two = two = two = -(2 31 – 1) two = -(2 31 – 2) … two = two = -1 Note that the sum of a number x and its inverted representation x’ always equals a string of 1s (-1) x + x’ = -1 x’ + 1 = -x … hence, can compute the negative of a number by -x = x’ + 1 inverting all bits and adding 1 Similarly, the sum of x and –x gives us all zeroes, with a carry of 1 In reality, x + (-x) = 2 n … hence the name 2’s complement 2’s Complement
2 N-1 non- negatives 2 N-1 negatives one zero how many positives? ’s Complement Number “line”: N = 5
Compute the 32-bit 2’s complement representations for the following decimal numbers: 5, -5, -6 Example
What if too big? Binary bit patterns above are simply representatives of numbers. Strictly speaking they are called “numerals” Numbers really have an number of digits –with almost all being same (00…0 or 11…1) except for a few of the rightmost digits –Just don’t normally show leading digits If result of add (or -, *, / ) cannot be represented by these rightmost HW bits, overflow is said to have occurred unsigned
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 Detecting Overflow
For an unsigned number, overflow happens when the last carry (1) cannot be accommodated For a signed number, overflow happens when the most significant bit is not the same as every bit to its left – when the sum of two positive numbers is a negative result – when the sum of two negative numbers is a positive result – The sum of a positive and negative number will never overflow MIPS allows addu and subu instructions that work with unsigned integers and never flag an overflow – to detect the overflow, other instructions will have to be executed
The hardware recognizes two formats: unsigned (corresponding to the C declaration unsigned int ) -- all numbers are positive, a 1 in the most significant bit just means it is a really large number signed (C declaration is signed int or just int ) -- numbers can be +/-, a 1 in the MSB means the number is negative This distinction enables us to represent twice as many numbers when we’re sure that we don’t need negatives Signed / Unsigned
Consider a comparison instruction: slt $t0, $t1, $zero and $t1 contains the 32-bit number …01 What gets stored in $t0 ? The result depends on whether $t1 is a signed or unsigned number – the compiler/programmer must track this and accordingly use either slt or sltu slt $t0, $t1, $zero stores 1 in $t0 sltu $t0, $t1, $zero stores 0 in $t0 MIPS Instructions
Occasionally, 16-bit signed numbers must be converted into 32-bit signed numbers – for example, when doing an add with an immediate operand The conversion is simple: take the most significant bit and use it to fill up the additional bits on the left – known as sign extension So 2 10 goes from to and goes from to Sign Extension
Quiz X = two Y = two A.X > Y (if signed) B.X > Y (if unsigned) AB 1: FF 2: FT 3: TF 4: TT
Computers are made to deal with numbers What can we represent in N bits? –2 N things, and no more! They could be… –Unsigned integers: 0to2 N - 1 (for N=32, 2 N –1 = 4,294,967,295) –Signed Integers (Two’s Complement) -2 (N-1) to 2 (N-1) - 1 (for N=32, 2 (N-1) = 2,147,483,648) Review of Numbers
1.Very large numbers? (seconds/millennium) 31,556,926, ( x ) 2.Very small numbers? (Bohr radius) m ( x ) 3.Numbers with both integer & fractional parts? 1.5 First consider #3 …our solution will also help with 1 and 2 What about other numbers?
Representation of Fractions “Binary Point” like decimal point signifies boundary between integer and fractional parts: xx. yyyy Example 6-bit representation: = 1x x x2 -3 = If we assume “fixed binary point”, range of 6-bit representations with this format: 0 to (almost 4)