1k likes | 1.31k Views
CSC 317 Computer Organization and Architecture. Chapter 2: Data Representation Spring 2007. Chapter 2 Objectives. Understand the fundamentals of numerical data representation and manipulation in digital computers. Master the skill of converting between various radix systems.
E N D
CSC 317Computer Organization and Architecture Chapter 2: Data Representation Spring 2007
Chapter 2 Objectives • Understand the fundamentals of numerical data representation and manipulation in digital computers. • Master the skill of converting between various radix systems. • Understand how errors can occur in computations because of overflow and truncation. • Understand the concepts of error detecting and correcting codes.
2.1 Introduction • A bit is the most basic unit of information in a computer. • It is a state of “on” or “off” in a digital circuit. • Or “high” or “low” voltage instead of “on” or “off.” • A byte is a group of eight bits. • A byte is the smallest possible addressable unit of computer storage. • A word is a contiguous group of bytes • Word sizes of 16, 32, or 64 bits are most common. • Usually a word represents a number or instruction. 3
2.2 Positional Numbering Systems • Computers use base 2 to represent numbers • The binary system is also called the base-2 system. • Our decimal system is the base-10 system. It uses powers of 10 for each position in a number. • Any integer quantity can be represented exactly using any base (or radix). • The decimal number 947 in powers of 10 is: 9 10 2 + 4 10 1 + 7 10 0 • The decimal number 5836.47 in powers of 10 is: 5 10 3 + 8 10 2 + 3 10 1 + 6 10 0 + 4 10 -1 + 7 10 -2 4
2.2 Positional Numbering Systems • An n-bit sequence of binary digits is an unsigned integer A whose value is: A = Σ 2i ai (0 ≤ i ≤ n-1) • The binary number 11001 in powers of 2 is: • 8 bits can represent unsigned numbers from 0 to 255 (28-1) • 1 2 4 + 1 2 3 + 0 2 2 + 0 2 1 + 1 2 0 • = 16 + 8 + 0 + 0 + 1 = 25 5
2.3 Decimal to Binary Conversions • In an earlier slide, we said that every integer value can be represented exactly using any radix system. • You can use either of two methods for radix conversion: the subtraction method and the division remainder method. • The subtraction method is more intuitive, but cumbersome. It does, however reinforce the ideas behind radix mathematics. • The division method employs the idea that successive division by a base is equivalent to successive subtraction by powers of the base. 6
2.3 Decimal to Binary Conversions • Suppose we want to convert the decimal number 190 to base 3. • We know that 3 5 = 243 so our result will be less than six digits wide. The largest power of 3 that we need is therefore 3 4 = 81, and 81 2 = 162. • Write down the 2 and subtract 162 from 190, giving 28. 7
2.3 Decimal to Binary Conversions • Converting 190 to base 3... • The next power of 3 is 3 3 = 27. We’ll need one of these, so we subtract 27 and write down the numeral 1 in our result. • The next power of 3, 3 2 = 9, is too large, but we have to assign a placeholder of zero and carry down the 1. 8
2.3 Decimal to Binary Conversions • Converting 190 to base 3... • 3 1 = 3 is again too large, so we assign a zero placeholder. • The last power of 3, 3 0 = 1, is our last choice, and it gives us a difference of zero. • Our result, reading from top to bottom is: 19010 = 210013 9
2.3 Decimal to Binary Conversions • Converting 190 to base 3... • First we take the number that we wish to convert and divide it by the radix in which we want to express our result. • In this case, 3 divides 190 63 times, with a remainder of 1. • Record the quotient and the remainder. 10
2.3 Decimal to Binary Conversions • Converting 190 to base 3... • 63 is evenly divisible by 3. • Our remainder is zero, and the quotient is 21. 11
2.3 Decimal to Binary Conversions • Converting 190 to base 3... • Continue in this way until the quotient is zero. • In the final calculation, we note that 3 divides 2 zero times with a remainder of 2. • Our result, reading from bottom to top is: • 19010 = 210013 12
2.3 Decimal to Binary Conversions • Fractional values can be approximated in all base systems. • Unlike integer values, fractions do not necessarily have exact representations under all radices. • The quantity ½ is exactly representable in the binary and decimal systems, but is not in the ternary (base 3) numbering system. 13
2.3 Decimal to Binary Conversions • Fractional decimal values have nonzero digits to the right of the decimal point. • Fractional values of other radix systems have nonzero digits to the right of the radix point. • Numerals to the right of a radix point represent negative powers of the radix: • 0.4710 = 4 10 -1 + 7 10 -2 • 0.112 = 1 2 -1 + 1 2 -2 • = ½ + ¼ • = 0.5+ 0.25 = 0.75 14
2.3 Decimal to Binary Conversions • As with whole-number conversions, you can use either of two methods: a subtraction method and an easy multiplication method. • The subtraction method for fractions is identical to the subtraction method for whole numbers. Instead of subtracting positive powers of the target radix, we subtract negative powers of the radix. • We always start with the largest value first, n -1, where n is our radix, and work our way along using larger negative exponents. 15
2.3 Decimal to Binary Conversions • The calculation to the right is an example of using the subtraction method to convert the decimal 0.8125 to binary. • Our result, reading from top to bottom is: • 0.812510 = 0.11012 • Of course, this method works with any base, not just binary. 16
2.3 Decimal to Binary Conversions • Using the multiplication method to convert the decimal 0.8125 to binary, we multiply by the radix 2. • The first product carries into the units place. 17
2.3 Decimal to Binary Conversions • Converting 0.8125 to binary . . . • Ignoring the value in the units place at each step, continue multiplying each fractional part by the radix. 18
2.3 Decimal to Binary Conversions • Converting 0.8125 to binary . . . • You are finished when the product is zero, or until you have reached the desired number of binary places. • Our result, reading from top to bottom is: • 0.812510 = 0.11012 • This method also works with any base. Just use the target radix as the multiplier. 19
2.3 Decimal to Binary Conversions • The binary numbering system is the most important radix system for digital computers. • However, it is difficult to read long strings of binary numbers-- and even a modestly-sized decimal number becomes a very long binary number. • For example: 110101000110112 = 1359510 • For compactness and ease of reading, binary values are usually expressed using the hexadecimal, or base-16, numbering system. 20
2.3 Decimal to Binary Conversions • The hexadecimal numbering system uses the numerals 0 through 9 and the letters A through F. • The decimal number 12 is C16. • The decimal number 26 is 1A16. • It is easy to convert between base 16 and base 2, because 16 = 24. • Thus, to convert from binary to hexadecimal, all we need to do is group the binary digits into groups of four. A group of four binary digits is called a hextet 21
2.3 Decimal to Binary Conversions • Using groups of hextets, the binary number 110101000110112 (= 1359510) in hexadecimal is: • Octal (base 8) values are derived from binary by using groups of three bits (8 = 23): Octal was very useful when computers used six-bit words. 22
2.4 Signed Integer Representation • Several representations exist for negative values: Sign Magnitude One's Complement Two's Complement 000 = +0 000 = +0 000 = +0 001 = +1 001 = +1 001 = +1 010 = +2 010 = +2 010 = +2 011 = +3 011 = +3 011 = +3 100 = -0 100 = -3 100 = -4 101 = -1 101 = -2 101 = -3 110 = -2 110 = -1 110 = -2 111 = -3 111 = -0 111 = -1 23
2.4 Signed Integer Representation • Sign Magnitude • Leftmost bit is sign bit: 0 for positive, 1 for negative • Remaining bits are magnitude • Drawbacks: • Sign bits give problems to addition and subtraction • Two representations for 0 • Rarely used 24
2.4 Signed Integer Representation • Two's Complement • Easier implementation of addition and subtraction • Leftmost bit still indicates sign • Positive numbers same as sign magnitude • Only one zero (all 0 bits) • Negating A: invert (complement) all bits of A and add 1 • Example: -55 start with +55: 0000...00110111 complement each bit: 1111...11001000 (1's complement) add 1: 1111...11001001 (2's complement) 25
2.4 Signed Integer Representation • Addition and Subtraction (numbers in 2's complement representation) • Add -55 and +58 (use 8 bits for simplicity) -55: 11001001 +58 00111010 ==> 100000011 underlined leftmost bit is an overflow (ignored here) • Overflow rule: • If two numbers are added and they are both positive or both negative, then overflow occurs if and only if the result has opposite sign 26
2.4 Signed Integer Representation • Subtraction uses addition • To subtract B from A, take 2's complement of B and add it to A • Digital circuit only need addition and complement circuits 27
carry_in A 1-bit Full Adder S B carry_out 2.4 Signed Integer Representation • Given a full adder (FA), we can use it to add binary digits (up to 3) 29
B31 B2 B1 B0 A31 A2 A1 A0 1-bit FA 1-bit FA 1-bit FA 1-bit FA S31 S2 S1 S0 2.4 Signed Integer Representation • Several FA's can be used to add binary numbers by feeding out the carry_out one FA to the carry_in of the FA of the left. add/sub C32 C31 C3 C2 C1 C0 32-bit Ripple Carry Adder/Subtractor (Better: Carry Lookahead Adder) Note: add/sub is ON (1) if we want A-B, otherwise is OFF 30
2.4 Signed Integer Representation • Multiplication • More complicate than addition • Done by a sequence of shifts and additions • Just like the paper-pencil approach. Use an example of a 4-bit multiplication. 1011 Multiplicand (11) 1101 Multiplier (13) 1011 0000 partial products 1011 1011 10001111 double-precision product (143) 31
2.4 Signed Integer Representation • Paper-pencil approach is inefficient • Solution: Do not wait until the end to add partial products. Algorithm (unsigned numbers): Do n times (e.g, 32 for MIPS) For each 1 on Multiplier, perform an add and a shift right For each 0 in the multiplier, perform only a shift right • Solutions for the multiplication of signed numbers: • Convert the numbers to positive numbers, multiple them as above. If signs were different, negate answer. • Apply Booth Algorithm
2.4 Signed Integer Representation • Booth’s algorithm • Multiplier and multiplicand are placed in registers Q & M • Q-1, 1-bit register placed to the right of Q0 Initialize A (third register) and Q-1 to 0 Do n times (n is the number of bits in Q): If Q0Q-1 = 01 then A <-- A + M If Q0Q-1 = 10 then A <-- A – M Arithmetic shift right A, Q, Q-1 33
2.4 Signed Integer Representation • Multiply 3 x 7 = 21 (00010101) Phase Comments M A Q Q-1 Initialize registers 0111 0000 0011 0 1 Q0Q-1 = 10, AA-M 1001 0011 0 ASR A, Q, Q-1 1100 1001 1 2 Q0Q-1 = 11, ASR A, Q, Q-1 1110 0100 1 3 Q0Q-1 = 01, AA+M 0101 0100 1 ASR A, Q, Q-1 0010 1010 0 4 Q0Q-1 = 00, ASR A, Q, Q-1 0001 0101 0 35
2.5 Floating-Point Representation • Integers can be considered as fixed point numbers • Decimal point at the far right • Floating-point (fp) numbers allow an arbitrary number of decimal places to the right of the decimal point. • For example: 0.5 0.25 = 0.125 • A 32-bit fp number: 000000000000000000011101.01001111 is equivalent to: 2 4 + 2 3 + 2 2 + 2 0 + 2 -2 + 2 -5 + 2 -6 + 2 -7 + 2 -8 = 29.30879375 36
2.5 Floating-Point Representation • Very large or very small fp numbers are expressed in scientific notation. • For example: 0.125 = 1.25 10-1 5,000,000 = 5.0 106 • Numbers written in scientific notation have three components: 37
2.5 Floating-Point Representation • Computer representation of a fp number: • The one-bit sign field is the sign of the stored value. • The size of the exponent field, determines the range of values that can be represented. • The size of the significand determines the precision of the representation. 38
2.5 Floating-Point Representation • The IEEE-754 single precision floating point standard uses an 8-bit exponent and a 23-bit significand. • The IEEE-754 double precision standard uses an 11-bit exponent and a 52-bit significand. For illustrative purposes, we will use a 14-bit model with a 5-bit exponent and an 8-bit significand. 39
2.5 Floating-Point Representation • The significand of a floating-point number is always preceded by an implied binary point. • Thus, the significand always contains a fractional binary value. • The exponent indicates the power of 2 to which the significand is raised. • (-1)sign X significand X 2exponent 40
2.5 Floating-Point Representation • Example: • Express 3210 in the simplified 14-bit floating-point model. • We know that 32 is 25. So in (binary) scientific notation 32 = 1.0 x 25 = 0.1 x 26. • Using this information, we put 110 (= 610) in the exponent field and 1 in the significand as shown. 41
2.5 Floating-Point Representation • The illustrations shown at the right are all equivalent representations for 32 using our simplified model. • Not only do these synonymous representations waste space, but they can also cause confusion. 42
2.5 Floating-Point Representation • Another problem with our system is that we have made no allowances for negative exponents. We have no way to express 0.5 (=2 -1)! (Notice that there is no sign in the exponent field!) All of these problems can be fixed with no changes to our basic model. 43
2.5 Floating-Point Representation • To resolve the problem of synonymous forms, the first digit of the significand must be 1 (normalized). This results in a unique pattern for each FP number. • In the IEEE-754 standard, this 1 is implied meaning that a 1 is assumed to the left of the binary point. • Hidden bit extends the significand by 1 bit • Number normalized to the form: 1.bbbbbb… • Biased exponents represent negative exponents • They are approximately midway point in range of values. • Bias of 127 for single precision,1023 for double precision. • Biased exponent: exponent + bias 44
2.5 Floating-Point Representation • Exponent ranges (in theory): • Unbiased: -127 to 128 ( -1023 to 1024) • Biased: 0 to 255 ( 0 to 2047) • Example: • Express -12.7510 in the revised single-precision IEEE-754 FP standard • Binary: -1100.11 = -1.10011 x 2 3 • biased exponent: 3 + 127 = 130 = 1000 0010 • significand: 1001 1000 0000 0000 0000 000 • (1 to the left of the binary point is hidden) • Bit 31 Final FP representation: Bit 0 • 1 10000010 10011000000000000000000 45
2.5 Floating-Point Representation • In IEEE-754 floating point standard: • An exponent of 0 and 255 (2047 for double precision) are used for special values (see next table). • Denormalized numbers extend the range of small numbers • Smallest normalized number: 1.0x2-126 • Smallest denormalized number: 0.000…1 x 2-126 = 2-149 • New actual range of exponents: • unbiased exponent range: -126 to 127 (-1022 to 1023) • biased exponent range: -1022 to 1023 (1 to 2046) 47
2.5 Floating-Point Representation F: fraction or significand
2.5 Floating-Point Representation Floating-point arithmetic • A FP operation may produce one of the conditions: • Exponent overflow: A positive exponent may exceed the maximum value. • Exponent underflow: A negative exponent is less than the minimum value. • Significand overflow: May happen during the addition of two significands of same sign. • Significand underflow: May happen during significand alignment. 49
2.5 Floating-Point Representation Addition and subtraction • Steps to add 2 FP numbers X and Y: Step 0: Restore hidden bits: Make explicit the implicit significand bit and change the sign of the subtrahend if it is a subtract operation • Step 1: Zero Check: Check if either operand is zero • Step 2: Significand alignment: Align significands so that exponents are equal Step 3: Addition: Add the two significands taking into account their signs. Significand or exponent overflow may exist. Step 4: Normalization: Normalize the result by shifting left significand digits and decrementing the exponent, which it may cause exponent overflow. 50