02 Memory
02 Memory
Announcements
¢ On the website: cs.uw.edu/351
§ Speedometer!
§ Anonymous feedback form
§ Make sure you are subscribed to the mailing list
§ Lecture slides on the web schedule (these will be linked 1-2 days prior)
§ Lab 0, having fun? Make sure to start early
§ Discussion boards
§ Videos for optional reference – not exactly the same slides as we’ll use
§ Tips for C, debugging, etc.
§ Lecture content
§ Office hours posted: if they don’t work for you, let us know
¢ Anyone not yet enrolled? If not, see me right after class
¢ New section being created for Th 11:30 – stay tuned
Autumn 2013 Memory & data 1
University of Washington
CPU Memory
Bus
Memory
Graphics
CPU
Audio Storage
USB…
I/O
(Bus)
Network
ns
io USB…
ct
ne
n
s co
B u
CPU
I/O
controller
Memory
Storage connections
Autumn 2013 Memory & data 4
University of Washington
? Memory
CPU data
i-cache instructions
this week…
take 352… Memory
i-cache instructions
this week…
take 352… Memory
HowCPUare data
registers data
and instructions
¢represented?
The CPU holds instructions temporarily in the instruction cache.
How does a
¢ The CPU holds data temporarily in a fixed number of registers.
program find its
¢ Instruction fetching is HW-controlled.
data in memory?
¢ Data movement is programmer-controlled.
Computer
system:
i-cache instructions
this week…
take 352… Memory
HowCPU
are data
registers data
and instructions
represented?
Binary Representations
¢ Base 2 number representation
§ A base 2 digit (0 o 1) is called a bit.
§ Represent 35110 as 00000001010111112 or 1010111112
¢ Electronic implementation
§ Easy to store with bi-stable elements
§ Reliably transmitted on noisy and inaccurate wires
0 1 0
3.3V
2.8V
0.5V
0.0V
Autumn 2013 Memory & data 11
University of Washington
i-cache instructions
this week…
take 352… Memory
How does a
program find its
data in memory?
Machine Words
¢ fixed number of contiguous bytes in memory, chosen by HW
¢ the largest unit of data a machine instruction can use
¢ word size = address size = register size
¢ Word size bounds the size of the address space and memory.
§ word size = w bits => 2w addresses
§ Until recently, most machines used 32-bit (4-byte) words.
§Potential address space: 232 addresses
232 bytes » 4 x 109 bytes = 4 billion bytes = 4GB
(living humans / addressable bytes » 1.8)
§ Became too small for memory-intensive applications
§ Current x86 systems use 64-bit (8-byte) words.
§ Potential address space: 264 addresses
264 bytes » 1.8 x 1019 bytes = 18 billion billion bytes = 18 EB (exabytes)
(possible living acquaintances / addressable bytes » 2.8)
Autumn 2013 Memory & data 15
University of Washington
Memory Alignment
¢ Data of size n only stored at addresses a where a mod n = 0
§ Convention or rule, depending on platform.
§ n is usually a power of 2.
(note hex
0x00 0x01 0x02 0x03 addresses)
¢ A 32-bit (4-byte) word-aligned
view of memory: 0x00
0x04
§ Each row is a word composed of 4 bytes.
0x08
§ Cells in a row are the word’s bytes. 0x0C
0x10
0x14
0x04 0x05 0x06 0x07 0x18
… 0x1C
0x20
0x24
More about alignment later in the course.
Autumn 2013 Memory & data 18
University of Washington
Data Representations
Sizes of data types (in bytes)
Java Data Type C Data Type Typical 32-bit x86-64
boolean bool 1 1
byte char 1 1
char 2 2
short short int 2 2
int int 4 4
float float 4 4
long int 4 8
double double 8 8
long long long 8 8
long double 8 16
(reference) pointer * 4 8
Byte Ordering
¢ How should bytes within a word be ordered in memory?
¢ Example: Store the 4-byte word 0xa1 b2 c3 d4.
§ In what order will the bytes be stored?
¢ Conventions!
§ Big-endian, Little-endian
§ Based on Gulliver’s Travels: tribes cut eggs on different sides (big, little)
Byte Ordering
¢ Big-Endian (PowerPC, SPARC, The Internet)
§ Least significant byte has highest address
¢ Little-Endian (x86)
§ Least significant byte has lowest address
¢ Example
§ Variable has 4-byte representation 0xa1b2c3d4
§ Address of variable is 0x100
0x100 0x101 0x102 0x103
Big Endian 01
a1 b2
23 45
c3 d4
67
0x100 0x101 0x102 0x103
Little Endian d4
67 45
c3 b2
23 01
a1
00 00 39 00
00 00
00 00
00 30
00 39
Autumn 2013 Memory & data 26
University of Washington
Deciphering numbers
n Value: 0x12ab
n Pad to 32 bits: 0x000012ab
n Split into bytes: 00 00 12 ab
n Reverse (little-endian): ab 12 00 00
Autumn 2013 Memory & data 28
University of Washington
y = 1 + *ptr;
What is *(&y) ?
Sets y to 1 plus the value at the address held by ptr.
Because ptr points to x, this is equivalent to y=1+x;
Autumn 2013 Memory & data 29
University of Washington
¢ int x, y;
¢ int x, y;
40
Autumn 2013 Memory & data
University of Washington
0x00
5F 01 00 00 0x04 a[0]
0x08 a[1]
0x0C
0x10 …
0x14
5F 01 00 00 0x18 a[5]
0x1C
0x20
0x24
41
Autumn 2013 Memory & data
University of Washington
42
Autumn 2013 Memory & data
University of Washington
43
Autumn 2013 Memory & data
University of Washington
44
Autumn 2013 Memory & data
University of Washington
45
Autumn 2013 Memory & data
University of Washington
46
Autumn 2013 Memory & data
University of Washington
equivalent {
p[1] = 0xB;
*(p + 1) = 0xB; 5F 01 00 00
AD 0B 00 00
0x14
0x18
0x1C
a[5]
04 00 00 00 0x20 p
array indexing = address arithmetic 0x24
Both are scaled by the size of the type.
48
Autumn 2013 Memory & data
University of Washington
p = p + 2; 04 00 00 00 0x20 p
array indexing = address arithmetic 0x24
Both are scaled by the size of the type.
49
Autumn 2013 Memory & data
University of Washington
p = p + 2; 0C 00 00 00 0x20 p
array indexing = address arithmetic 0x24
Both are scaled by the size of the type.
50
Autumn 2013 Memory & data
University of Washington
p = p + 2; 0C 00 00 00 0x20 p
array indexing = address arithmetic 0x24
Both are scaled by the size of the type.
Autumn 2013
*p = a[1] + 1; Memory & data
51
University of Washington
Representing strings
¢ A C-style string is represented by an array of bytes (char).
— Elements are one-byte ASCII codes for each character.
— ASCII = American Standard Code for Information Interchange
32 space 48 0 64 @ 80 P 96 ` 112 p
33 ! 49 1 65 A 81 Q 97 a 113 q
34 ” 50 2 66 B 82 R 98 b 114 r
35 # 51 3 67 C 83 S 99 c 115 s
36 $ 52 4 68 D 84 T 100 d 116 t
37 % 53 5 69 E 85 U 101 e 117 u
38 & 54 6 70 F 86 V 102 f 118 v
39 ’ 55 7 71 G 87 W 103 g 119 w
40 ( 56 8 72 H 88 X 104 h 120 x
41 ) 57 9 73 I 89 Y 105 I 121 y
42 * 58 : 74 J 90 Z 106 j 122 z
43 + 59 ; 75 K 91 [ 107 k 123 {
44 , 60 < 76 L 92 \ 108 l 124 |
45 - 61 = 77 M 93 ] 109 m 125 }
46 . 62 > 78 N 94 ^ 110 n 126 ~
47 / 63 ? 79 O 95 _ 111 o 127 del
Null-terminated Strings
¢ For example, “Harry Potter” can be stored as a 13-byte array.
72 97 114 114 121 32 80 111 116 116 101 114 0
H a r r y P o t t e r \0
printf directives:
void show_int (int x) { %p Print pointer
show_bytes( (byte *) &x, sizeof(int)); \t Tab
}
%x Print value as hex
\n New line
Autumn 2013 Memory & data 55
University of Washington
Result (Linux):
int a = 12345;
0x11ffffcb8 0x39
0x11ffffcb9 0x30
0x11ffffcba 0x00
0x11ffffcbb 0x00
Boolean Algebra
¢ Developed by George Boole in 19th Century
§ Algebraic representation of logic
§ Encode “True” as 1 and “False” as 0
§ AND: A&B = 1 when both A is 1 and B is 1
§ OR: A|B = 1 when either A is 1 or B is 1
§ XOR: A^B = 1 when either A is 1 or B is 1, but not both
§ NOT: ~A = 1 when A is 0 and vice-versa
§ DeMorgan’s Law: ~(A | B) = ~A & ~B
& 0 1 | 0 1 ^ 0 1 ~
0 0 0 0 0 1 0 0 1 0 1
1 0 1 1 1 1 1 1 0 1 0
01010101
^ 01010101
00000000
¢ How does this relate to set operations?
01010101 { 0, 2, 4, 6 }
76543210
¢ Operations
§ & Intersection 01000001 { 0, 6 }
§ | Union 01111101 { 0, 2, 3, 4, 5, 6 }
§ ^ Symmetric difference 00111100 { 2, 3, 4, 5 }
§ ~ Complement 10101010 { 1, 3, 5, 7 }
Bit-Level Operations in C
¢ & | ^ ~
§ Apply to any “integral” data type
§long, int, short, char, unsigned
§ View arguments as bit vectors
¢ Examples (char data type)
§ ~0x41 --> 0xBE
~010000012 --> 101111102
§ ~0x00 --> 0xFF
~000000002 --> 111111112
§ 0x69 & 0x55 --> 0x41
011010012 & 010101012 --> 010000012
§ 0x69 | 0x55 --> 0x7D
011010012 | 010101012 --> 011111012
¢ Some bit-twiddling puzzles in Lab 1
Autumn 2013 Memory & data 60
University of Washington