CSE 2021: Computer Organization Recap from Last Time load from disk High-Level Program Lecture-2(a) Data Translation Binary patterns, signed and unsigned integers Today s topic Data Translation Code Translation compiler Assembly Lang (obj) Shakil M. Khan (adapted from Prof. Roumani) Machine Lang (exe) save.exe to disk CSE-2021 May-12-2011 2 Data Translation It s all about numbers! mapping text, pictures, sound, to numbers The binary world binary patterns, bits and bytes, hex, the 2 n rule Unsigned Integers the binary system, shift/mod, converting to/from Signed Integers sign and magnitude, one s/two s complement, converting, add/sub, sign extension Real Numbers IEEE-754, single and double precisions It s All about Numbers! Text Pictures Video clips Audio CSE-2021 May-12-2011 3 CSE-2021 May-12-2011 4 1
The Binary World Bit Numbering Binary digits (bits) 0 or 1 ON or OFF high or low (voltage) Binary patterns strings of bits e.g.: 10 0100 0101 0000 0001 1111 1101 0101 Representation size 1 byte = 8 bits 1 word = 4 bytes = 32 bits 0 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1 0 0 0 0 0 0 1 1 31 30 29 3 2 1 0 Least Significant bit (LSb) = bit #0 Most Significant bit (MSb) = bit #31 Least Significant Byte (LSB) Most Significant Byte (MSB) CSE-2021 May-12-2011 5 CSE-2021 May-12-2011 6 The 2 n Rule (1) n bits can represent 2 n distinct numbers Examples: 2 1 => {0,1} 2 2 => {00,01,10,11} 2 3 => {000,001,010,011,100,101,110,111} 2 4 => {0000,0001,0010,0011,0100,0101,0110,0111, 1000,1001,1010,1011,1100,1101,1110,1111} The 2 n Rule (2) The largest natural number that can be represented by n bits = 2 n -1 Examples: 1 bit => 1 2 bits => 3 3 bits => 7 4 bits => 15 CSE-2021 May-12-2011 7 CSE-2021 May-12-2011 8 2
Binary Patterns (Exercises) Unsigned Integers (1) Prove that a binary pattern of size n can represent any one of 2 n different things (hint: use induction) We want to represent a signal that has 41 possible values. How many bits do we need? How many bytes are needed to store a 640 x 320 coloured picture using 16 colour shade for each of Red, Green, and Blue? The binary system the value of i th digit d = d x 2 i Converting to decimal 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 31 30 29 3 2 1 0 = 0x2 31 +0x2 30 +0x2 29 +1x2 28 + +0x2 2 +1x2 1 +1x2 0 = 0 + 0 + 0 + 268435456 + + 0 + 2 + 1 = 268435459 10 CSE-2021 May-12-2011 9 CSE-2021 May-12-2011 10 Unsigned Integers (2) Range/word: 0 to +4,294,967,295 (in binary: 00...000 to 11...111) Given an n-bit number x x n 1 n 2 1 0 n 12 xn 22 x12 x02 Unsigned Integers (3) Converting from decimal the mod e.g. convert 6 10 to binary convert 101 10 to binary Range: 0 to +2 n 1 CSE-2021 May-12-2011 11 CSE-2021 May-12-2011 12 3
Logical Shifts Other Bitwise Operations Logical shifts Left shift Z by n bits =Z x 2 n Right shift Z by n bits =Z / 2 n X Y and or 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 Shift Left Logical 000000 CSE-2021 May-12-2011 13 xor not (X) 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 nand, nor,... CSE-2021 May-12-2011 14 The Hex Notation Unsigned Integers (Exercises) Read/write long binary strings: tedious Solution use higher base data size of all computers today = multiples of 4 Use hexadecimal numbers why not decimal? 16 = 2 4, so convert 4 bits at a time using lookup table often written with prefix `0x Examples? bits 0000 0001 0010 0011 0100 0101 0110 0111 hex 0 1 2 3 4 5 6 7 bits 1000 1001 1010 1011 1100 1101 1110 1111 hex 8 9 A B C D E F Represent the unsigned integer 75 Determine the decimal value of the unsigned integer 0x4A Given an unsigned integer k, how many bits are needed to represent it? Compute largest unsigned integer that can fit in one, two, four, and eight bytes are your answers consistent with the 2 n rule? Consider the bit pattern 0xAFC7 and imagine shifting all its bits left by one position. Show that it becomes 0x5F8E CSE-2021 May-12-2011 15 CSE-2021 May-12-2011 16 4
More Exercises How many bytes are needed to store a 640 x 320 coloured picture using 16 colour shades for each of Red, Green, and Blue? As above but for a one-min silent video clip with 24 fps and the same specs as above? As above but add 16-bit sound sampled at 40 khz? Signed Integers Signed integers sign and magnitude the 1 s complement the 2 s complement Converting to decimal from decimal Arithmetic right shifts Sign vs. zero extension CSE-2021 May-12-2011 17 CSE-2021 May-12-2011 18 Sign and Magnitude Range/word: -2,147,483,647 to +2,147,483,647 (in binary: 11...111 to 01...111) Format reserve 1 bit for sign rest for magnitude Issues where to put the sign bit? both positive and negative zeros 00...000 and 10...000 complex h/w: needs extra step to set sign bit The 1 s Complement Range/word: -2,147,483,647 to +2,147,483,647 (in binary: 10...000 to 01...111) Format positive as unsigned (but MSb not used: why?) invert all bits for negative Issues again, positive and negative zeros 00...000 and 11...111 complex h/w: needs extra step to subtract CSE-2021 May-12-2011 19 CSE-2021 May-12-2011 20 5
The 2 s Complement (1) Used universally since 1965 Range/word: -2,147,483,648 to +2,147,483,647 (in binary: 10...000 to 01...111) Advantages non-negative numbers have the same unsigned and 2scomplement representation all negative numbers have 1 in MSb, so h/w needs to check sign bit only for polarity The 2 s Complement (2) Issues positive and negative numbers not balanced Some specific numbers 0: 0000 0000 0000 1: 1111 1111 1111 Most-negative: 1000 0000 0000 Most-positive: 0111 1111 1111 CSE-2021 May-12-2011 21 CSE-2021 May-12-2011 22 The 2 s Complement (3) Given an n-bit number x = -x n-1 2 n-1 + x n-2 2 n-2 +... + x 1 2 1 + x 0 2 0 Range: 2 n 1 to +2 n 1 1 Example 1111 1111 1111 1111 1111 1111 1111 1100 2 = 1 2 31 + 1 2 30 + + 1 2 2 +0 2 1 +0 2 0 = 2,147,483,648 + 2,147,483,644 = 410 Signed Negation Complement and add 1 Example: negate +2 +2 = 0000 0000 0010 2 2 = 1111 1111 1101 2 + 1 = 1111 1111 1110 2 More examples CSE-2021 May-12-2011 23 CSE-2021 May-12-2011 24 6
Signed integers : add or sub? Arithmetic Right Shift Compute 0x4E + 0xA3 0x4E = 0 1 0 0 1 1 1 0 78 +78 0xA3 = 1 0 1 0 0 0 1 1 163-93 0xF1 = 1 1 1 1 0 0 0 1 241-15 Try right shifting a signed negative number 0xFFFF FFFE >> 1 = 0x7FFF FFFF -1 shifted right once = large positive number!! Solution arithmetic right shift What about left shifting signed numbers? CSE-2021 May-12-2011 25 CSE-2021 May-12-2011 26 Sign Extension Representing a number using more bits preserve the numeric value Many instructions have 16-bit operands need to extend to 32 bits Replicate the sign bit to the left contrast with zero extension (for unsigned ints) Examples: 8-bit to 16-bit +2: 0000 0010 => 0000 0000 0000 0010 2: 1111 1110 => 1111 1111 1111 1110 Signed Integers (Exercises) Represent the signed integer -75 Determine the decimal value of the signed integer 0x4A Why does sign extension work? Compute smallest and largest signed integer that can fit in one, two, four, and eight bytes. are your answers consistent with 2 n? CSE-2021 May-12-2011 27 CSE-2021 May-12-2011 28 7
Labs Office Hours Labs start next week PreLab + exercise PreLab can submit from home unsubmit folder Exercise (LabTest mode) submit as many times as you want submitting = OK or reveals failed test case (no penalty) automatic 5/5 (manual 0/5-4/5) Makeup A-D + K-N Office hours TA (right before the lab) + forum (every Friday) change my office hours (or call me @ x33928, I can let you in)??? CSE-2021 May-12-2011 29 CSE-2021 May-12-2011 30 CSE 2021: Computer Organization Lecture-2(b) Data Translation Floating point representation (IEEE 754) Shakil M. Khan Floating Point Representation for non-integral numbers including very small and very large numbers Like scientific notation 2.34 10 56 normalized +0.002 10 4 +987.02 10 9 not normalized In binary ±1.xxxxxxx 2 2 yyyy Types float and double in C CSE-2021 May-12-2011 32 8
Floating Point Standard Defined by IEEE Std 754-1985 Developed in response to divergence of representations portability issues for scientific code Now almost universally adopted Two representations single precision (32-bit) double precision (64-bit) CSE-2021 May-12-2011 33 IEEE Floating-Point Format single: 8 bits double: 11 bits single: 23 bits double: 52 bits S Exponent Fraction S x ( 1) (1 Fraction) 2 (Exponent Bias) S: sign bit (0 non-negative, 1 negative) Normalize significand: 1.0 significand < 2.0 always has a leading pre-binary-point 1 bit, so no need to represent it explicitly (hidden bit) significand is fraction with the 1. restored Exponent: excess representation: actual exponent + Bias ensures exponent is unsigned single: bias = 127; double: bias = 1203 CSE-2021 May-12-2011 34 Single-Precision Range Exponents 00000000 and 11111111 reserved Smallest value Exponent: 00000001 actual exponent = 1 127 = 126 Fraction: 000 00 significand = 1.0 ±1.0 2 126 ±1.2 10 38 Largest value exponent: 11111110 actual exponent = 254 127 = +127 Fraction: 111 11 significand 2.0 ±2.0 2 +127 ±3.4 10 +38 Double-Precision Range Exponents 0000 00 and 1111 11 reserved Smallest value Exponent: 00000000001 actual exponent = 1 1023 = 1022 Fraction: 000 00 significand = 1.0 ±1.0 2 1022 ±2.2 10 308 Largest value Exponent: 11111111110 actual exponent = 2046 1023 = +1023 Fraction: 111 11 significand 2.0 ±2.0 2 +1023 ±1.8 10 +308 CSE-2021 May-12-2011 35 CSE-2021 May-12-2011 36 9
Floating-Point Precision Relative precision all fraction bits are significant Single: approx 2 23 equivalent to 23 log 10 2 23 0.3 6 decimal digits of precision Double: approx 2 52 equivalent to 52 log 10 2 52 0.3 16 decimal digits of precision Floating-Point Example Represent 0.75 0.75 = ( 1) 1 1.1 2 2 1 S = 1 Fraction = 1000 00 2 Exponent = 1 + Bias Single: 1 + 127 = 126 = 01111110 2 Double: 1 + 1023 = 1022 = 01111111110 2 Single: 1011111101000 00 Double: 1011111111101000 00 CSE-2021 May-12-2011 37 CSE-2021 May-12-2011 38 Floating-Point Example What number is represented by the singleprecision float 11000000101000 00 S = 1 Fraction = 01000 00 2 Exponent = 10000001 2 = 129 x = ( 1) 1 (1 + 01 2 ) 2 (129 127) = ( 1) 1.25 2 2 = 5.0 Infinities and NaNs Exponent = 111...1, Fraction = 000...0 ±Infinity can be used in subsequent calculations, avoiding need for overflow check Exponent = 111...1, Fraction 000...0 Not-a-Number (NaN) indicates illegal or undefined result e.g., 0.0 / 0.0 can be used in subsequent calculations CSE-2021 May-12-2011 39 CSE-2021 May-12-2011 40 10
Floating-Point (Exercises) Convert to IEEE-754-210.25 10 How about 1.36 10 can only approximate Try -0.085 10 CSE-2021 May-12-2011 41 11