Chapter4

Chapter4 - Chapter 4 Data Representation EEC 70 Fall 2010...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 4: Data Representation EEC 70 Fall 2010 Professor Kent Wilken 1 Data Representation We need a specific way of representing integers, floating point, and characters in binary floating Memory is organized as a array of bytes (a bytes (a groups of 8 bits), so integers, reals, and chars are an integer number of bytes are 7 0 2 Integer Representation Integer size usually is processor’s “bit size”. Integer 32-bit processor like MIPS has 32-bit (4 byte) 32 bit integers integers Goals of integer representations: Goals • Allow an all positive representation (0 to 232-1) • Allow positive and negative representation (roughly Allow -231 to +231) • Allow fast arithmetic • Provide a unique representation for each value makes comparison easier makes • Make it easy for humans to read ?? can automatically (via I/O programs) convert to base 10 as needed can 3 Different Integer Representations Different Unsigned Unsigned Sign Magnitude Sign One’s Complement One Two’s Complement Two Biased Biased Binary coded decimal Binary Most modern computers use two’s Most complement and unsigned complement • Hardware is simple and fast 4 Unsigned Weighted representation we have already seen seen • Range: 0 to 2n-1, for n bits. For 3 bits: Decimal 0 1 2 3 4 5 6 7 Unsigned 000 001 010 011 100 101 110 111 5 Sign Magnitude Use left-most bit as the sign bit: Use most sign • sign bit of 0 indicates positive, 1 negative Remaining bits represent number’s unsigned Remaining unsigned magnitude magnitude n-1 n-1 Range: -2n-1+1 to 2n-1-1 for n-bit number Seems “natural” but problematic because • two representations for 0 two ⇒ more complex hardware • different hardware for different addition and subtraction Decimal Decimal 0 1 2 3 -0 -1 -2 -3 Sign Mag. 000 001 010 011 100 101 110 111 6 One’s Complement Used in early machines, leads to method in modern machines: two’s complement modern Positive numbers use same representation as unsigned. unsigned. Negation (producing the additive inverse) is additive is done by complementing (inverting) each bit: done 1 → 0, 0 → 1 0, Two representations Two for zero (not so good) Negative numbers Negative have leading “1” have Decimal 0 1 2 3 -0 -1 -2 -3 One’s Comp. 000 001 010 011 111 110 101 100 7 Two’s Complement Improvement on one’s complement that has a single Improvement complement representation for zero, allows addition and subtraction to be done uniformly to All bits except the most significant bit (MSB) are given most (MSB) the same weight as unsigned representation. the For an n-bit number, MSB has weight -2n-1 For bit Allows range from -2n-1 to +2n-1 -1 to The additive inverse (negation) of a two’s complement The complement number is its one’s complement plus 1: number Example: 3ten = 011 ; -3ten = 100 +001 = 101 011 011 + 101 = 000 (carry out ignored, see next slide) (3) (-3) 8 Two’s Complement: Additive Inverse B + (B + 1) = (B + B) + 1 = [111 … 111] + 1 = [-2n-1 + ( 2n-2 + 2n-3 + … + 21 + 20 )] + 1 = -2n-1 + [(2n-2 + 2n-3 + … + 21 + 20 ) + 1] = -2n-1 + 2n-1 =0 Decimal Two’s Comp. 0 1 2 3 -1 -2 -3 -4 000 001 010 011 111 110 101 100 9 Two’s Complement (cont.) Subtraction can be done by producing the additive inverse and then adding: additive A - B = A + (-B) Example: 00101 - 11001 = 00101 + 00111 = 01100 Example: (5) (-7) (5) (7) (12) The complement and increment (+1) operations are easily done in hardware, ⇒ subtraction can be subtraction done using an simple modification of the hardware used for addition (see EEC180A, EEC170). used Note: negative numbers have a “1” in the left-most most bit position, the sign bit. sign 10 Two’s Complement (cont.) How to represent an integer in two’s How complement: complement: • Positive numbers: binary value with a zero sign bit • Negative numbers: take positive value Negative take the one’s complement complement add 1 00101 (+5) 00101 11010 11010 + 00001 ===== 11011 (-5) Negate any number (positive or negative) by: complement, increment complement, 11 Biased Representation Representation which has consecutive bit patterns in the same order as unsigned (0000 ... 1111), but allows positive and negative numbers positive • For 4 bits we can bias by +4: the value For represented is 4 less than the unsigned value of the same bit pattern of • Could bias by a different value for a specific purpose • Biased notation used for floating point exponent to make >, < comparisons easier (details in Chapter 6) Decimal -4 -3 -2 -1 0 1 2 3 Biased +4 000 001 010 011 100 101 110 111 12 Binary Coded Decimal (BCD) Encoding that makes each decimal digit an even number of bits (4), used in very early computers number Easier for humans to work with base 10 numbers stored in the computer?? numbers Inefficient use of memory: 4 bits represents 10 values rather than 16 values Decimal 0 1 2 3 4 5 6 7 BCD 0000 0001 0001 0010 0010 0011 0011 0100 0100 0101 0101 0110 0110 0111 0111 Decimal 8 9 N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A BCD 1000 1001 1001 1010 1011 1100 1101 1110 1111 13 Sign Extension How to change a two’s complement integer How complement represented using with smaller number of bits (e.g., 8) into the same integer represented using a larger number of bits (e.g., 32)? larger • Replicate the sign bit to fill the extra bits: 0xxxxxxx becomes 00000000 0xxxxxxx for positive number, this clearly adds zero to for the number 1xxxxxxx becomes 11111111 1xxxxxxx 1xxxxxxx for negative number, we are moving the -2n-1 for term to a higher bit position, but we are adding additional positive terms. The net result is no change in the value. 14 Overflow Sometimes an arithmetic result cannot be represented with the number of bits used. represented For example, for 3-bit unsigned representation: 011 (3) +110 (6) --001 (1?!) This condition is called overflow This overflow It may be necessary to detect overflow and take appropriate action appropriate For unsigned addition, overflow occurs when the result is smaller than either of the operand (see above example) (see 15 Overflow For two’s complement addition, overflow For complement occurs when: occurs • adding two positive integers and the result is adding negative negative 011 +010 --101 (3) (2) (-3?!) • adding two negative integers and the result is adding positive positive 101 (-3) +110 (-2) --011 (3?!) 16 Character Representation The traditional character representation is ASCII: American Standard for Computer merican tandard omputer Information Interchange nformation ASCII uses 8-bits to represent each character ASCII bits (see Table 4.4, page 102). Examples: (see Binary Hex Character 0100 1000 48 ‘H’ 0111 0101 0111 75 ‘u’ 0110 1000 68 ‘h’ 0011 1111 3f ‘?’ Note: lowercase/uppercase have different codes 17 Terminal Input/Output Terminal I/O is done using ASCII characters characters • On input, terminal keystroke is mapped to On ASCII code, passed to processor ASCII • On output, ASCII code from processor is On mapped to dot matrix pattern for display 18 ASCII Table 19 Parity ASCII table shows characters for values 0-127ten. ASCII Only requires 7 bits. Why 8 bits? Only Eighth bit (MSB) is called a parity bit can be parity can used used to detect errors used • Parity bit can be set so the total number of 1s is odd Parity (called odd parity): odd 1100 1000 ‘H’ 0111 0101 ‘u’ 0110 1000 ‘h’ 1011 1111 ‘?’ 1000 0000 nul • Parity allows any single bit error (single bit flip) to be Parity detected detected 20 Unicode What about non-English alphabets? What Unicode (Universal Code) uses 2 bytes per character, Unicode (Universal allows 65,536 different characters. Provides a unique code for characters in most all languages, including: code • Latin • Greek • Cyrillic • Hebrew • Arabic • Persian • Armenian • Korean • Chinese • Japanese • nine major Indian scripts • various Southeast Asian scripts • various African scripts • Scandinavian • Turkish • etc. 21 String Declaration String declarations put corresponding ASCII codes in consecutive memory locations codes .data error_ reply: .ascii null_char: .byte . . . “Huh?” 0 01001000 01110101 01101000 00111111 00000000 .text puts error_reply 22 Memory is Typeless Memory has not notion of data types, only binary values binary .data error_ reply: .word 0x4875683f null_char: .byte . . . 0 01001000 01110101 01101000 00111111 00000000 .text puts error_reply Type is determined by the instructions that operate on that memory operate 23 Characters as Integers Characters can be treated as 8-bit integers (memory is Characters bit typeless) typeless) • Can do addition, subtraction, etc.: char1: .byte ‘8’ # ASCII representation is 0x38 char2: .byte ‘9’ # ASCII representation is 0x39 ASCII digit: .word digit: ... sub digit, char1, 0x30 # subtraction produces digit value • Can compare character values: blt char_input, 0x30, not_digit #first digit character is 0x30 bgt char_input, 0x39, not_digit #last digit character is 0x39 #input character is a digit ... not_digit: not_digit: #input character is non-digit #input 24 Converting ASCII Strings to Integers For input ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, we want integer value we 12345ten. 12345 • Must subtract 0x30 bias out of each character to yield true digit Must bias out value value • Must give each digit proper weight. Because digits come left to right, we don’t know digit weight right when it is read in. char string → integer algorithm outline: integer = 0 while there are more digit characters get character digit = character - 0x30 # remove the bias to get true digit digit integer = integer x 10 + digit # prior part of integer must be # increased in weight by 10x 25 Converting Integers to ASCII Strings For value 12345ten must produce character sequence For must ‘1’, ‘2’, ‘3’, ‘4’, ‘5’ • Must determine digit sequence, from MSD to LSD • Must add 0x30 bias to digit for proper character value integer → char string algorithm outline: weight = 1 while (weight x 10) <= integer weight = weight x 10 while weight >= 1 digit = integer / weight character = digit + 0x30 putc character integer = integer mod weight weight = weight / 10 # determine the max weight # get the next digit # create proper character # output character # get remainder # get next digit weight 26 Floating Point Representation MIPS and most other processors use IEEE IEEE standard floating point representation. standard Representation consists of three parts: Representation • Sign Bit: 1 bit # sign bit for mantissa • Exponent: 8 bits # biased notation allows pos/neg exp. biased • Fraction: 23 bits 31 30 S 0 23 22 Exp Fraction The value represented is: The (-1)S x f x 2e where e = Exp - bias Exp f = Fraction/223 + 1 27 Floating Point Representation (cont.) For single precision numbers (32-bits) For bias = 127 Exp is biased to allow easier magnitude comparison comparison • larger unsigned value is a larger exponent 00000000 00000001 00000001 00000010 00000010 00000011 00000011 ... 11111101 11111110 11111111 reserved -126 -125 -124 ... 126 127 reserved 28 Floating Point Representation (cont.) The floating point numbers are normalized: The 1.fraction x 2e Because the integer part of the mantissa is mantissa is always* 1, there is no need to store it. The 1 is implied. Called the hidden bit. implied Called hidden 31 30 S 0 23 22 Exp . Fraction 1 *Except for 0, which has a 0 for the integer part. 0 is treated specially. The all zeros pattern is reserved, used to represent 0. • Allows comparison with zero to be the same for integer Allows and floating point. and 29 Floating Point Representation: Floating Example Example Let’s put 64.2 in floating point representation Let First convert to binary presentation: 64 is 1000000two 0.2 is .0011two (see Chapter 3 slides) thus 64.2 = 1000000.0011two Next normalize the number: Next 1000000.0011two = 1.0000000011two x 26 The FP fraction is the first 23 digits to the right of the The binary point (ignoring rounding, See Chapter 6): rounding 0000 0000 1100 1100 1100 110 The exponent must be biased: The 6ten + 127ten = 133ten = 10000101two 30 Example (cont.) The resulting FP representation is: The 0 10000101 00000000110011001100110 S Exp Fraction In Hex: In 0100 0010 1000 0000 0110 0110 0110 0110 0x 0x 4 2 8 0 6 6 6 6 When we look in memory and see 0x42806666, how do we know it is a floating point number and not an integer or 4 characters? and 31 Special Floating Point Special Representations Representations Zero: 0x 0000 0000 Zero: +infinity (result is too large to represent): +infinity 0 11111111 0000...0000 -infinity (result is too small to represent): 1 11111111 0000...0000 Not A Number (NaN), e.g., result of division by 0: division s 11111111 fraction where s is either 0 or 1, and fraction is where non-zero non More on floating point in Chapter 6 More 32 Review Questions A variation on Problem 2.13 from the text: Write a SAL program that counts the Write number of punctuation characters in a line of text entered by the user, where a punctuation character is any nonpunctuation numeral, non-alpha and non-space space character. character. 33 Review Questions What number does 1000 0001 represent? What Is it possible to tell? If not, what extra information is needed? information 34 Review Questions In a normal computer each byte or word consists In of a collection of memory cells that are the same size (e.g., each memory cell is one bit that holds two values). Consider a weird computer where a weird word consists of a collection of different weird consists size memory cells. Specifically, a weird word includes one cell of each of the following sizes: 2-value, 3-value, 4-value, 5-value, 6-value, 7value, and 8-value. To the nearest bit, a weird value. word provides storage which is equivalent to a normal word with how many bits? 35 Review Questions Give the single precision floating point Give representation for the number -83.7ten 36 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online