0% found this document useful (0 votes)
49 views22 pages

CPS 104 Computer Organization and Programming Lecture-4: Data Representations, Memory and Data Structures

Computer Organization and design

Uploaded by

praches
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views22 pages

CPS 104 Computer Organization and Programming Lecture-4: Data Representations, Memory and Data Structures

Computer Organization and design

Uploaded by

praches
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CPS 104 Computer Organization and Programming Lecture-4 : Data representations, Memory and Data structures.

Jan. 14, 2004 Gershon Kedem http://kedem.duke.edu/cps104/Lectures

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

Review: 2s Complement Representation for Integers


0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1

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

2s Complement Precision Extension

Most computers today support 32-bit (int) or 64-bit integers

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

What About Non-integer Numbers?


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

Floating Point Representation


Numbers are represented by:

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)

Single precision floating point number uses 32-bits for representation


31 30 22 8-bit s exp
CPS104 Lec4.9

0 23-bit Mantissa
GK Spring 2004

Floating Point Representation

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

X = 1 0111 1110 100 0000 0000 0000 0000 0000


s
CPS104 Lec4.10

M
GK Spring 2004

Floating Point Representation

Example: What floating-point number is: 0xC1580000?

CPS104 Lec4.11

GK Spring 2004

Floating Point Representation

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

ASCII Character Representation


Hex char

00 08 10 18 20 28 30 38 40 48 50 58 60 68 70 78

NUL BS DLE CAN SP ( 0 8 @ H P X ` h p x

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

STX NL DC2 SUB " * 2 : B J R Z b j r z

03 0B 13 1B 23 2B 33 3B 43 4B 53 5B 63 6B 73 7B

ETX VT DC3 ESC # + 3 ; C K S [ c k s {

04 0C 14 1C 24 2C 34 3C 44 4C 54 5C 64 6C 74 7C

EOT NP DC4 FS $ , 4 < D L T \ d l t |

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

ACK SO SYN RS & . 6 > F N V ^ f n v ~

07 0F 17 1F 27 2F 37 3F 47 4F 57 5F 67 6F 77 7F

BEL SI ETB US ' / 7 ? G O W _ g o w DEL

CPS104 Lec4.13

Each character is represented by a 7-bit ASCII code. GK Spring 2004 It is packed into a byte (8-bits)

Basic Data Types


Bit: 0, 1 Bit String: sequence of bits of a particular length 4 bits is a nibble 8 bits is a byte 16 bits is a half-word 32 bits is a word 64 bits is a double-word Character: ASCII 7 bit code Decimal: (BCD code) digits 0-9 encoded as 0000 thru 1001 two decimal digits packed per 8 bit byte Integers: 2's Complement (32-bit representation). Floating Point: Single Precision (32-bit representation). Double Precision (64-bit representation). Extended Precision(128 -bit representation).
CPS104 Lec4.14

exponent E MxR base mantissa

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

A Programs View of Memory


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

Byte Word Address Address Memory 0 0 00110110 1 00001100 2 3 1 4


2n-1


2n-1-4 2n-1

CPS104 Lec4.17

GK Spring 2004

Buzz Word Definition: Endianess


Byte Order Big Endian: byte 0 is 8 most significant bits IBM 360/370, Motorola
68k, MIPS, Sparc, HP PA

Little Endian: byte 0 is 8 least significant bits Intel 80x86, DEC Vax,
DEC Alpha

little endian byte 0 3 msb 0 big endian byte 0 1 2 3 2 1 0 lsb

CPS104 Lec4.18

GK Spring 2004

Buzz Word Definition: Alignment

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;

and x = 2; produce the same result

On 32-bit machine, p is 32-bits

x 0x26cbf0

...
GK Spring 2004

p 0x26d0a0 0x26cbf0

CPS104 Lec4.20

Vector Class v.s. Arrays

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

In C++ allocate using array form of new


int *a = new int[100]; double *b = new double[300];

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

You might also like