Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.

Similar presentations


Presentation on theme: "CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson."— Presentation transcript:

1 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

2 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: 0000 0000 0000 0000 0000 0000 0000 0001 And –1 ten in sign and magnitude would be: 1000 0000 0000 0000 0000 0000 0000 0001 How to Represent Negative Numbers?

3 Arithmetic circuit complicated –Special steps depending whether signs are the same or not Also, two zeros – 0x00000000 = +0 ten – 0x80000000 = –0 ten –What would two 0s mean for programming? Therefore sign and magnitude abandoned Shortcomings of sign and magnitude?

4 Example: 7 10 = 00111 2 –7 10 = 11000 2 Called One’s Complement Note: positive numbers have leading 0s, negative numbers have leadings 1s 000000000101111... 11111 1111010000... What is -00000 ? Answer: 11111 How many positive numbers in N bits? How many negative numbers? Another try: complement the bits

5 Arithmetic still somewhat complicated Still two zeros – 0x00000000 = +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?

6 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 0000 0000 0000 0000 0000 0000 0000 0000 two = 0 ten 0000 0000 0000 0000 0000 0000 0000 0001 two = 1 ten … 0111 1111 1111 1111 1111 1111 1111 1111 two = 2 31 -1 1000 0000 0000 0000 0000 0000 0000 0000 two = -2 31 1000 0000 0000 0000 0000 0000 0000 0001 two = -(2 31 – 1) 1000 0000 0000 0000 0000 0000 0000 0010 two = -(2 31 – 2) … 1111 1111 1111 1111 1111 1111 1111 1110 two = -2 1111 1111 1111 1111 1111 1111 1111 1111 two = -1 Negative Numbers

7 0000 0000 0000 0000 0000 0000 0000 0000 two = 0 ten 0000 0000 0000 0000 0000 0000 0000 0001 two = 1 ten … 0111 1111 1111 1111 1111 1111 1111 1111 two = 2 31 -1 1000 0000 0000 0000 0000 0000 0000 0000 two = -2 31 1000 0000 0000 0000 0000 0000 0000 0001 two = -(2 31 – 1) 1000 0000 0000 0000 0000 0000 0000 0010 two = -(2 31 – 2) … 1111 1111 1111 1111 1111 1111 1111 1110 two = -2 1111 1111 1111 1111 1111 1111 1111 1111 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 31 -2 31 + x 30 2 30 + x 29 2 29 + … + x 1 2 1 + x 0 2 0 2’s Complement

8 0000 0000 0000 0000 0000 0000 0000 0000 two = 0 ten 0000 0000 0000 0000 0000 0000 0000 0001 two = 1 ten … 0111 1111 1111 1111 1111 1111 1111 1111 two = 2 31 -1 1000 0000 0000 0000 0000 0000 0000 0000 two = -2 31 1000 0000 0000 0000 0000 0000 0000 0001 two = -(2 31 – 1) 1000 0000 0000 0000 0000 0000 0000 0010 two = -(2 31 – 2) … 1111 1111 1111 1111 1111 1111 1111 1110 two = -2 1111 1111 1111 1111 1111 1111 1111 1111 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

9 2 N-1 non- negatives 2 N-1 negatives one zero how many positives? 00000 00001 00010 11111 11110 10000 01111 10001 0 1 2 -2 -15 -16 15............ -3 11101 -4 11100 000000000101111... 111111111010000... 2’s Complement Number “line”: N = 5

10 Compute the 32-bit 2’s complement representations for the following decimal numbers: 5, -5, -6 Example

11 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 0000000001 00010 11111 11110 unsigned

12 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

13 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

14 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

15 Consider a comparison instruction: slt $t0, $t1, $zero and $t1 contains the 32-bit number 1111 01…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

16 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 0000 0000 0000 0010 to 0000 0000 0000 0000 0000 0000 0000 0010 and -2 10 goes from 1111 1111 1111 1110 to 1111 1111 1111 1111 1111 1111 1111 1110 Sign Extension

17 Quiz X = 1111 1111 1111 1111 1111 1111 1111 1100 two Y = 0011 1011 1001 1010 1000 1010 0000 0000 two A.X > Y (if signed) B.X > Y (if unsigned) AB 1: FF 2: FT 3: TF 4: TT

18 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

19 1.Very large numbers? (seconds/millennium)  31,556,926,000 10 (3.1556926 10 x 10 10 ) 2.Very small numbers? (Bohr radius)  0.0000000000529177 10 m (5.29177 10 x 10 -11 ) 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?

20 Representation of Fractions “Binary Point” like decimal point signifies boundary between integer and fractional parts: xx. yyyy 2121 2020 2 -1 2 -2 2 -3 2 -4 Example 6-bit representation: 10.1010 2 = 1x2 1 + 1x2 -1 + 1x2 -3 = 2.625 10 If we assume “fixed binary point”, range of 6-bit representations with this format: 0 to 3.9375 (almost 4)


Download ppt "CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson."

Similar presentations


Ads by Google