CS 265 Computer Architecture Wei Lu, Ph.D., P.Eng. 1
Part 1: Data Representation Our goal: revisit and re-establish fundamental of mathematics for the computer architecture course Overview: what are bits & bytes? bit and arithmetic value? bit and hexadecimal? character set in PCs? how to do binary arithmetic operation (+,-,*,/)? integer representation in PCs: signed and unsigned binary and floating point floating point representation? order of byte in computers big endian vs. little endian 2
Conversion in Between Float and Binary 3
Floating point means a number with an integral part and a fractional part Converting floating point in between decimal and binary Place values: Floating point 2 5 2 4 2 3 2 2 2 1 2 0 2-1 2-2 2-3 2-4 2-5 32 16 8 4 2 1 0.5 0.25 0.125 0.0625 0.03125 Integral part Fractional part Example: 4.5 (decimal) = 100.1 (binary) 4
Converting floating point Example: 6.25 = 110.01 Method: 6 = 110.25 x 2 = 0.5.5 x 2 = 1.0 (Stop when fractional part is 0) 110.01 Example: 6.2 110.001100110011 110 (Integral part: convert in the usual way).5 (Fractional part: successive multiplication by 2) 6 = 110.2 x 2 = 0.4.4 x 2 = 0.8.8 x 2 = 1.6.6 x 2 = 1.2.2 x 2 = 0.4 (repeats) 110.0011 0011 0011 5
One more example to convert floating point Example: 6.4? 110.0110 0110 0110 How the floating binary number stored in computer? 6
How Float Represented in Computer 7
Floating point representation in computer Computers use a form of scientific notation for floatingpoint representation Numbers written in scientific notation have three basic components: Example: 1.101 x 2 010 use base 2 instead of base 10 Sign Mantissa Exponent Exponent specifies the order of magnitude in a base Mantissa specifies most significant part of value Sign specifies negative or positive 8
Floating point representation in computer Computer representation of a floating-point number consists of three fixed-size fields: This is the standard arrangement of these fields. 9
Floating point representation in computer (1) The one-bit sign field is the sign of the stored value. (2) The size of the exponent field, determines the range of values that can be represented. (3) The size of the significand determines the precision of the representation. 10
Floating point representation in computer single-precision (32 bits) floating point representation defined by IEEE standard 754 1 sign bit "biased" exponent (8 bits) "normalized" mantissa (23 bits) Floating point = (-1) s x (1.m) x 2 (e-bias) Notice that the 1 in 1.m is always assumed. where, bias = 127 11
Floating point representation in computer double-precision (64 bits) floating point representation defined by IEEE standard 754 1 sign bit "biased" exponent (11 bits) "normalized" mantissa (52 bits) Floating point = (-1) s x (1.m) x 2 (e-bias) where, bias = 1023 12
Floating point representation: example 1 Given decimal real number 6.25, what is its single precision floating number stored in PC? Step 1. 6.25 (decimal) = 110.01 (binary) Step 2. Move the radix point until a single 1 appears on the left, and multiply by the corresponding power of 2 1.1001x2 2 so the sign bit is 0 (positive) the biased exponent is 2 + 127 = 129 = 10000001 = e and the normalized mantissa is 1001 (drop the 1, rest zero-fill). 0 10000001 10010000000000000000000 0100 0000 1100 1000 0000 0000 0000 0000 40C80000 hexadecimal 13
Floating point representation: example 2 Given decimal real number 6.2, what is its single precision floating number stored in PC? Step 1. 6.2 (decimal) = 110.001100110011 (binary) Step 2. Move the radix point until a single 1 appears on the left, and multiply by the corresponding power of 2 = 1.10001100110011 x2 2 so the sign bit is 0 (positive) the biased exponent is 2 + 127 = 129= 10000001 and the normalized mantissa is 10001100110011 0 10000001 10001100110011001100110 0100 0000 1100 0110 0110 0110 0110 0110 40C66666 hexadecimal 14
Floating point representation: example 3 Double value in memory (in hex): 4024 0000 0000 0000 what s its binary: 0100 0000 0010 0100 0000 0000 0000 0000 what s its decimal? s (bit 64) = 0 = positive number e (bits 52 to 62) = 100 0000 0010 = 1024 + 2 = 1026 m (bits 0 to 51) =.0100 0000 0000 0000 0000 Value = (-1) 0 x (1.01) b x 2 (1026-1023) = 1x (2 0 +2-2 )x2 3 =(1+1/4)x8=8+2=10 15
Floating point representation: example 4 What decimal floating-point number is represented by 0xC1870000? 1100 0001 1000 0111 0000 0000 0000 0000 1 10000011 00001110000000000000000 Sign Biased exponent Normalized mantissa so the sign is negative the unbiased exponent is 131-127 = 4 and the unnormalized mantissa is 1.00001110000000000000000 (add the 1 left of the radix point). Move the radix point 4 places to the right: 10000.111-10000.111 = -16.875 16
Byte Order: Big Endian vs. Little Endian 17
Byte order Byte ordering, or endianness, is a major computer architectural consideration Byte ordering describes how data stored in memory, which is visible and important to programmer More details later in the course, here we introduce basic concept 18
Byte order If we have a two-byte integer, the integer may be stored so that the least significant byte is followed by the most significant byte or vice versa. In little endian machines, the least significant byte is stored in lowest memory location. (DEC, IBM (Intel Pentium)) Big endian machines store the most significant byte in lowest memory. (Motorola, Sun SPARC) 19
Big and little endian: an example Given 32 bits (4 bytes) integer (or single-precision floating point numbers) 0x 90AB12CD, the 4 bytes are: 90, AB, 12, CD, How can we store these values into memory? Big Endian In big endian, we store the most significant byte in the smallest address. Here's how it would look: Little Endian In little endian, we store the least significant byte in the smallest address. Here's how it would look: Address Value 1000 90 1001 AB 1002 12 1003 CD Address Value 1000 CD 1001 12 1002 AB 1003 90 20
Endianness: consequence for programmer For JAVA programmer, we have to know: - Everything in Java binary format files is stored big-endian, i.e. MSB (Most Significant Byte) first. - JAVA is platform independent because and Java hides internal endianness from us no matter what internal byte order used (i.e. Mac is big-endian order and intel PC is little-endian) For C programmer, we have to know: - Most files compiled by C use little-endian in windows PC For both C and JAVA programmer, we have to fix the byte order problem when transferring data files between computers for which the byte ordering differs 21
Summary: conceptual issues Bits grouped into sets to represent Integers Characters Floating point values Signed integers can be represented as Sign magnitude One's complement Two's complement Bytes of integer can be numbered in Big-endian order and Little-endian order 22
Summary: technical issues Conversion in between Binary, Hexadecimal, Decimal, Single Float and Double Signed Integer represented by Sign magnitude One's complement Two's complement The consequence of byte order to our programmers 23
Thank you for your attendance Any questions? 24