CPS 104 Computer Organization and Programming Lecture-4: Data Representations, Memory and Data Structures
CPS 104 Computer Organization and Programming Lecture-4: Data Representations, Memory and Data Structures
CPS104 Lec4.1
GK Spring 2004
Administratrivia
Homework-1 On the Web: http://kedem.cs.duke.edu/cps104/Homework.html Due January 21, in class. Addition to homework policy:
To motivate everyone to start early, any homework assignment handed in 24 hours early will automatically earn 5% bonus.
CPS104 Lec4.2
GK Spring 2004
Key idea is to use the upper half of the binary numbers to represent negative numbers. Obtain negative numbers by subtracting a large constant. i = -an-1*2n-1 + an-2*2 n-2 + ... a0*20
6-bit examples: 0101102 = 2210 ; 1010102 = -2210 010 = 0000002; 110 = 0000012; -110 = 1111112
CPS104 Lec4.3
GK Spring 2004
Review: 2s Complement Negation and Addition To negate a number do: u Step 1. complement the digits u Step 2. add 1 Examples 1410 = 0011102 0100102 -1410 = 1100012 +1100102 + 1 0001002 1100102 To add signed numbers use regular addition but disregard carry out u Example 1810 - 1410 = 1810 + (-1410) = 410
CPS104 Lec4.4
GK Spring 2004
64-bit using gcc is long long u 64-bit using Digital/Compaq compiler is long
u
To extend precision do sign bit extension u precision is number of bits used to represent a number
Example 1410 = 0011102 in 6-bit representation. 1410 = 0000000011102 in 12-bit representation -1410 = 1100102 in 6-bit representation -1410 = 1111111100102 in 12-bit representation.
CPS104 Lec4.5
GK Spring 2004
Overflow
Twos Complement representation is finite. u For any fixed precision there is a maximum number that can be represented. (The largest positive number). u There is also the smallest number (The largest negative number). What do they (the largest and smallest numbers) look like? What happened when you add (subtract) two numbers and the result is too big (or too small) to be represented?
CPS104 Lec4.6
GK Spring 2004
Overflow Example1: 0100000 01101012 (= 5310) +01010102 (= 4210) 10111112 (=-3310) Example3: 1100000 01101012 (= 5310) +11010102 (=-2210) 00111112 (= 3110)
CPS104 Lec4.7
Example2: 1000000 10101012 (=-4310) +10010102 (=-5410) 00111112 (= 3110) Example4: 0000000 00101012 (= 2110) +01010102 (= 4210) 01111112 (= 6310)
GK Spring 2004
There are infinitely many real numbers between two integers Many important numbers are real u speed of light ~= 3x108 u = 3.1415 Fixed number of bits limits range of integers u Cant represent some important numbers Humans use Scientific Notation u 1.3x104
CPS104 Lec4.8
GK Spring 2004
Z = (-1)s
S := 1-bit field ; Sign bit
2E-127
1. M
E := 8-bit field; Exponent: Biased integer, 0 E 255. M:= 23-bit field; Mantissa: Normalized fraction with hidden 1 (dont actually store it)
0 23-bit Mantissa
GK Spring 2004
The mantissa represents a fraction using binary notation: M = . s1, s2, s3 . . . = 1.0 + s1*2-1 + s2*2-2 + s3*2-3 + . . . Example: X = -0.7510 in single precision (-(1/2 + 1/4))
-0.7510 = -0.112 = (-1) x 1.12 x 2-1 = (-1) x 1.12 x 2126-127 S = 1 ; Exp = 12610 = 0111 11102 ; M = 100 0000 0000 0000 0000 00002
31 30 23 22 0
M
GK Spring 2004
CPS104 Lec4.11
GK Spring 2004
Double Precision Floating point: 64-bit representation: 1-bit sign, 11-bit (biased) exponent; 52-bit mantissa (with hidden 1).
X = (-1)s 2E-1023 1. M
Double precision floating point number
S 1 Exp 11-bit Mantissa 20 - bit 32 - bit
CPS104 Lec4.12
GK Spring 2004
00 08 10 18 20 28 30 38 40 48 50 58 60 68 70 78
01 09 11 19 21 29 31 39 41 49 51 59 61 69 71 79
SOH HT DC1 EM ! ) 1 9 A I Q Y a i q y
02 0A 12 1A 22 2A 32 3A 42 4A 52 5A 62 6A 72 7A
03 0B 13 1B 23 2B 33 3B 43 4B 53 5B 63 6B 73 7B
04 0C 14 1C 24 2C 34 3C 44 4C 54 5C 64 6C 74 7C
05 0D 15 1D 25 2D 35 3D 45 4D 55 5D 65 6D 75 7D
ENQ CR NAK GS % 5 = E M U ] e m u }
06 0E 16 1E 26 2E 36 3E 46 4E 56 5E 66 6E 76 7E
07 0F 17 1F 27 2F 37 3F 47 4F 57 5F 67 6F 77 7F
CPS104 Lec4.13
Each character is represented by a 7-bit ASCII code. GK Spring 2004 It is packed into a byte (8-bits)
How many +/- #'s? Where is decimal pt? How are +/- exponents represented?
GK Spring 2004
Summary
Computers operate on binary numbers (0s and 1s) Conversion to/from binary, octal, hexadecimal Signed binary numbers u 2s complement. u arithmetic, negation. u Overflow. Floating point representation u hidden 1 u biased exponent u single precision, double precision
CPS104 Lec4.15
GK Spring 2004
Computer Memory
What is Computer Memory? What does it look like to the program? How do we find things in computer memory?
CPS104 Lec4.16
GK Spring 2004
What is Memory? a bunch of bits Looks like a large linear array Find things by indexing into array u unsigned integer Most computers support byte (8-bit) addressing u Each byte has a unique address (location). u Byte of data at address 0x100 and 0x101 u Word of data at address 0x100 and 0x104 32-bit v.s. 64-bit addresses u we will assume 32-bit for rest of course, unless otherwise stated
2n-1
2n-1-4 2n-1
CPS104 Lec4.17
GK Spring 2004
Little Endian: byte 0 is 8 least significant bits Intel 80x86, DEC Vax,
DEC Alpha
CPS104 Lec4.18
GK Spring 2004
Alignment: require that objects fall on address that is multiple of their size. 32-bit integer u Aligned if address % 4 = 0 64-bit integer? u Aligned if ?
Byte #
0 Aligned 1 2 3
Not Aligned
CPS104 Lec4.19
GK Spring 2004
Pointers
A pointer is a memory location that contains the address of another memory location address of operator & u dont confuse with bitwise AND operator (later today)
int x; int *p; p = &x;
Given
Then
*p = 2;
x 0x26cbf0
...
GK Spring 2004
p 0x26d0a0 0x26cbf0
CPS104 Lec4.20
Vector Class u Insulates programmers from underlying data structure. u Array bounds checking u Automagically growing/shrinking when more items are added/deleted How are Vectors implemented? u real understanding comes when more levels of abstraction are understood Programming close to HW u (e.g., operating system, device drivers, etc.) Arrays can be more efficient u but be leery of claims that C-style arrays are required for efficiency Can talk about memory easier in terms of arrays u pointer to a vector?
GK Spring 2004
CPS104 Lec4.21
Arrays
new [] returns a pointer to a block of memory u how big? where? size of chunk can be set at runtime delete [] a; // storage returned In C malloc(nbytes); free(ptr);
CPS104 Lec4.22
GK Spring 2004