L 01 L 04 Intro Memory Combinational Data
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Memory, Data, & Addressing II CSE 410 Winter 2017 Instructor: Justin Hsia Teaching Assistants: Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui Google’s Alpha. Go AI secretively won more than 50 straight games against the world’s top Go players Ke, the reigning top-ranked Go player, has acknowledged that human beings are no match for robots in the complex board game, after he lost three games to an AI that mysteriously popped up online in recent days. The AI turned out to be Alpha. Go in disguise. Ke [wrote] “humans have evolved in games in thousands of years—but computers now tell us humans are all wrong. From now on, we Go players will need to work with the computers to enter a new area and a new level. ” • https: //qz. com/877721/the-ai-master-bested-the-worlds-top-go-players-and-thenrevealed-itself-as-googles-alphago-in-disguise/
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Administrivia v v Homework 1 due today § Reminder: you get an unlimited number of tries! Lab 0 due Friday (1/13) § Credit/no credit assignment § You should be able to answer these questions much more thoroughly by the end of this class! v v Optional section tomorrow @ 4: 30 pm in MGH 241 § C and pointers Justin’s Friday office hours in a locked lab (CSE 003) § I’ll set up near the door, so just knock! 2
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Review Questions 1) If the word size of a machine is 64 -bits, which of the following is usually true? (pick all that apply) a) 64 bits is the size of a pointer b) 64 bits is the size of an integer c) 64 bits is the width of a register 2) (True/False) By looking at the bits stored in memory, I can tell if a particular 4 -bytes is being used to represent an integer, floating point number, or instruction. 3) If the size of a pointer on a machine is 6 bits, the address space is how many bytes? 3
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Memory, Data, and Addressing v v Representing information as bits and bytes Organizing and addressing data in memory Manipulating data in memory using C Boolean algebra and bit-level manipulations 4
L 01: L 04: Intro, Memory Combinational & Data Logic II Addresses and Pointers in C v v CSE 369, CSE 410, Autumn Winter 2017 2016 * is also used with variable declarations & = “address of” operator * = “value at address” or “dereference” operator int* ptr; int x = 5; int y = 2; ptr = &x; y = 1 + *ptr; “Dereference ptr” Declares a variable, ptr, that is a pointer to (i. e. holds the address of) an int in memory Declares two variables, x and y, that hold ints, and initializes them to 5 and 2, respectively Sets ptr to the address of x (“ptr points to x”) Sets y to “ 1 plus the value stored at the address held by ptr. Because ptr points to x, this is equivalent to y=1+x; What is *(&y) ? 5
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Assignment in C v v v A variable is represented by a memory location Declaration ≠ initialization (initially holds “garbage”) int x, y; 0 x 00 0 x 01 0 x 02 0 x 03 0 x 00 A 7 00 32 00 § x is at address 0 x 04, y is at 0 x 18 0 x 04 0 x 08 0 x 0 C 0 x 10 0 x 14 0 x 18 0 x 1 C 0 x 20 0 x 24 00 EE FA 26 00 01 FF DE 00 01 EE CE 00 00 AD 00 29 EE CA 00 10 00 F 4 BE 00 F 3 EE FE 00 00 00 96 EF 00 x y 6
L 01: L 04: Intro, Memory Combinational & Data Logic II Assignment in C v v v CSE 369, CSE 410, Autumn Winter 2017 2016 32 -bit example (pointers are 32 -bits wide) little-endian A variable is represented by a memory location Declaration ≠ initialization (initially holds “garbage”) int x, y; 0 x 00 0 x 01 0 x 02 0 x 03 0 x 00 § x is at address 0 x 04, y is at 0 x 18 0 x 04 00 01 29 F 3 x 0 x 08 0 x 0 C 0 x 10 0 x 14 0 x 18 01 00 00 00 y 0 x 1 C 0 x 20 0 x 24 7
L 01: L 04: Intro, Memory Combinational & Data Logic II Assignment in C v v v CSE 369, CSE 410, Autumn Winter 2017 2016 32 -bit example (pointers are 32 -bits wide) & = “address of” * = “dereference” left-hand side = right-hand side; § LHS must evaluate to a memory location § RHS must evaluate to a value (could be an address) 0 x 00 0 x 01 § Store RHS value at LHS location 0 x 00 int x, y; 01 0 x 04 00 00 0 x 08 x = 0; 0 x 02 0 x 03 00 29 00 F 3 x 0 x 0 C 0 x 10 0 x 14 0 x 18 01 00 00 00 y 0 x 1 C 0 x 20 0 x 24 8
L 01: L 04: Intro, Memory Combinational & Data Logic II 32 -bit example (pointers are 32 -bits wide) Assignment in C v v CSE 369, CSE 410, Autumn Winter 2017 2016 & = “address of” * = “dereference” left-hand side = right-hand side; § LHS must evaluate to a memory location § RHS must evaluate to a value (could be an address) 0 x 00 0 x 01 § Store RHS value at LHS location 0 x 00 int x, y; 0 x 04 00 00 0 x 08 x = 0; 0 x 0 C y = 0 x 3 CD 02700; 0 x 10 little endian! 0 x 02 0 x 03 00 00 x 0 x 14 01 27 00 D 0 00 3 C 00 y 0 x 18 00 0 x 1 C 0 x 20 0 x 24 9
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 32 -bit example (pointers are 32 -bits wide) Assignment in C v v v & = “address of” * = “dereference” left-hand side = right-hand side; § LHS must evaluate to a memory location § RHS must evaluate to a value (could be an address) 0 x 00 0 x 01 § Store RHS value at LHS location 0 x 00 int x, y; 00 27 00 0 x 04 03 0 x 08 x = 0; 0 x 0 C y = 0 x 3 CD 02700; 0 x 10 0 x 14 x = y + 3; 0 x 18 00 27 0 x 1 C § Get value at y, add 3, store in x 0 x 02 0 x 03 D 0 00 3 C 00 x D 0 3 C y 0 x 20 0 x 24 10
L 01: L 04: Intro, Memory Combinational & Data Logic II Assignment in C v v v CSE 369, CSE 410, Autumn Winter 2017 2016 32 -bit example (pointers are 32 -bits wide) & = “address of” * = “dereference” left-hand side = right-hand side; § LHS must evaluate to a memory location § RHS must evaluate to a value (could be an address) 0 x 00 0 x 01 § Store RHS value at LHS location 0 x 00 int x, y; 0 x 04 03 27 0 x 08 x = 0; 0 x 0 C y = 0 x 3 CD 02700; 0 x 10 0 x 14 x = y + 3; 0 x 18 00 27 0 x 1 C § Get value at y, add 3, store in x 0 x 20 DE AD int* z; 0 x 24 § z is at address 0 x 20 0 x 02 0 x 03 D 0 3 C x D 0 3 C y BE EF z 11
L 01: L 04: Intro, Memory Combinational & Data Logic II Assignment in C v v v CSE 369, CSE 410, Autumn Winter 2017 2016 32 -bit example (pointers are 32 -bits wide) & = “address of” * = “dereference” left-hand side = right-hand side; § LHS must evaluate to a memory location § RHS must evaluate to a value (could be an address) 0 x 00 0 x 01 § Store RHS value at LHS location 0 x 00 int x, y; 0 x 04 03 27 0 x 08 x = 0; 0 x 0 C y = 0 x 3 CD 02700; 0 x 10 0 x 14 x = y + 3; 0 x 18 00 27 0 x 1 C § Get value at y, add 3, store in x 0 x 20 24 00 int* z = &y + 3; 0 x 24 § Get address of y, “add 3”, store in z Pointer arithmetic 0 x 02 0 x 03 D 0 3 C x D 0 3 C y 00 00 z 12
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Pointer Arithmetic v v v Pointer arithmetic is scaled by the size of target type § In this example, sizeof(int) = 4 int* z = &y + 3; § Get address of y, add 3*sizeof(int), store in z § &y = 0 x 18 = 1*161 + 8*160 = 24 § 24 + 3*(4) = 36 = 2*161 + 4*160 = 0 x 24 Pointer arithmetic can be dangerous! § Can easily lead to bad memory accesses § Be careful with data types and casting 13
L 01: L 04: Intro, Memory Combinational & Data Logic II Assignment in C v v v int x, y; x = 0; y = 0 x 3 CD 02700; x = y + 3; § Get value at y, add 3, store in x int* z = &y + 3; § Get address of y, add 12, store in z *z = y; § What does this do? CSE 369, CSE 410, Autumn Winter 2017 2016 32 -bit example (pointers are 32 -bits wide) & = “address of” * = “dereference” 0 x 00 0 x 01 0 x 02 0 x 03 0 x 00 0 x 04 03 27 D 0 3 C x 0 x 08 0 x 0 C 0 x 10 0 x 14 0 x 18 00 27 D 0 3 C y 0 x 1 C 0 x 20 24 00 00 00 z 0 x 24 14
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 32 -bit example (pointers are 32 -bits wide) Assignment in C v v v int x, y; x = 0; y = 0 x 3 CD 02700; x = y + 3; § Get value at y, add 3, store in x int* z = &y + 3; § Get address of y, add 12, store in z The target of a pointer is also a memory location *z = y; § Get value of y, put in address stored in z & = “address of” * = “dereference” 0 x 00 0 x 04 0 x 08 0 x 0 C 0 x 10 0 x 14 0 x 18 0 x 1 C 0 x 20 0 x 24 0 x 00 0 x 01 0 x 02 0 x 03 03 27 D 0 3 C x 00 27 D 0 3 C y 24 00 00 27 00 D 0 00 3 C z 15
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Arrays are adjacent locations in memory storing the same type of data object Arrays in C a is a name for the array’s address 64 -bit example (pointers are 64 -bits wide) Declaration: int a[6]; element type name a[1] a[3] a[5] number of elements 0 x 0 0 x 8 a[0] a[2] a[4] 0 x 1 0 x 9 0 x 2 0 x. A 0 x 3 0 x. B 0 x 4 0 x. C 0 x 5 0 x. D 0 x 6 0 x. E 0 x 7 0 x. F 0 x 00 0 x 08 0 x 10 0 x 18 0 x 20 0 x 28 0 x 30 0 x 38 0 x 40 0 x 48 16
L 01: L 04: Intro, Memory Combinational & Data Logic II Arrays are adjacent locations in memory storing the same type of data object Arrays in C a is a name for the array’s address The address of a[i] is the address of a[0] plus i times the element size in bytes Declaration: int a[6]; Indexing: CSE 369, CSE 410, Autumn Winter 2017 2016 a[0] = 0 x 015 f; a[5] = a[0]; 0 x 0 0 x 8 a[0] 5 F a[2] a[4] 0 x 1 0 x 9 0 x 2 0 x. A 0 x 3 0 x. B 0 x 4 0 x. C 0 x 5 0 x. D 0 x 6 0 x. E 0 x 7 0 x. F 01 00 00 5 F 01 00 00 0 x 08 0 x 10 0 x 18 0 x 20 0 x 28 0 x 30 0 x 38 0 x 40 0 x 48 17
L 01: L 04: Intro, Memory Combinational & Data Logic II Arrays are adjacent locations in memory storing the same type of data object Arrays in C a is a name for the array’s address The address of a[i] is the address of a[0] plus i times the element size in bytes Declaration: int a[6]; Indexing: CSE 369, CSE 410, Autumn Winter 2017 2016 a[0] = 0 x 015 f; a[5] = a[0]; No bounds a[6] = 0 x. BAD; checking: a[-1] = 0 x. BAD; 0 x 0 0 x 8 a[0] 5 F a[2] a[4] 0 x 1 0 x 9 0 x 2 0 x. A 0 x 3 0 x. B 01 00 00 AD 0 B 00 00 0 x 4 0 x. C 0 x 5 0 x. D 0 x 6 0 x. E 0 x 7 0 x. F AD 0 B 00 00 5 F 01 00 00 0 x 08 0 x 10 0 x 18 0 x 20 0 x 28 0 x 30 0 x 38 0 x 40 0 x 48 18
L 01: L 04: Intro, Memory Combinational & Data Logic II Arrays are adjacent locations in memory storing the same type of data object Arrays in C a is a name for the array’s address The address of a[i] is the address of a[0] plus i times the element size in bytes Declaration: int a[6]; Indexing: CSE 369, CSE 410, Autumn Winter 2017 2016 a[0] = 0 x 015 f; a[5] = a[0]; No bounds a[6] = 0 x. BAD; checking: a[-1] = 0 x. BAD; 0 x 0 0 x 8 0 x 1 0 x 9 0 x 2 0 x. A 0 x 3 0 x. B Pointers: int* p; p = a; equivalent p = &a[0]; *p = 0 x. A; a[0] 0 A 5 F a[2] a[4] 01 00 00 00 0 x 4 0 x. C 0 x 5 0 x. D 0 x 6 0 x. E 0 x 7 0 x. F AD 0 B 00 00 5 F 01 00 00 AD 0 B 00 00 p 10 00 00 00 0 x 08 0 x 10 0 x 18 0 x 20 0 x 28 0 x 30 0 x 38 0 x 40 0 x 48 19
L 01: L 04: Intro, Memory Combinational & Data Logic II Arrays are adjacent locations in memory storing the same type of data object Arrays in C a is a name for the array’s address The address of a[i] is the address of a[0] plus i times the element size in bytes Declaration: int a[6]; Indexing: a[0] = 0 x 015 f; a[5] = a[0]; No bounds a[6] = 0 x. BAD; checking: a[-1] = 0 x. BAD; Pointers: int* p; p = a; equivalent p = &a[0]; *p = 0 x. A; array indexing = address arithmetic p[1] = 0 x. B; *(p+1) = 0 x. B; p = p + 2; 0 x 0 0 x 8 0 x 1 0 x 9 0 x 2 0 x. A 0 x 3 0 x. B 0 x 4 0 x. C 0 x 5 0 x. D 0 x 6 0 x. E 0 x 7 0 x. F AD 0 B 00 00 a[0] 0 A 5 F 00 01 00 00 0 B 00 00 00 a[2] a[4] 5 F 01 00 00 AD 0 B 00 00 (both scaled by the size of the type) equivalent CSE 369, CSE 410, Autumn Winter 2017 2016 p 10 00 00 00 0 x 08 0 x 10 0 x 18 0 x 20 0 x 28 0 x 30 0 x 38 0 x 40 0 x 48 20
L 01: L 04: Intro, Memory Combinational & Data Logic II Arrays are adjacent locations in memory storing the same type of data object Arrays in C a is a name for the array’s address The address of a[i] is the address of a[0] plus i times the element size in bytes Declaration: int a[6]; Indexing: a[0] = 0 x 015 f; a[5] = a[0]; No bounds a[6] = 0 x. BAD; checking: a[-1] = 0 x. BAD; Pointers: int* p; p = a; equivalent p = &a[0]; *p = 0 x. A; array indexing = address arithmetic (both scaled by the size of the type) equivalent CSE 369, CSE 410, Autumn Winter 2017 2016 p[1] = 0 x. B; *(p+1) = 0 x. B; p = p + 2; *p = a[1] + 1; 0 x 0 0 x 8 0 x 1 0 x 9 0 x 2 0 x. A 0 x 3 0 x. B 0 x 4 0 x. C 0 x 5 0 x. D 0 x 6 0 x. E 0 x 7 0 x. F AD 0 B 00 00 a[0] 0 A 00 00 00 0 B 00 00 00 a[2] 0 C 00 00 00 a[4] 5 F 01 00 00 AD 0 B 00 00 p 18 00 00 0 x 08 0 x 10 0 x 18 0 x 20 0 x 28 0 x 30 0 x 38 0 x 40 0 x 48 21
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Question: The variable values after Line 3 executes are shown on the right. What are they after Line 4 & 5? § Vote at http: //Poll. Ev. com/justinh 1 2 3 4 5 6 void main() { int a[] = {5, 10}; int *p = a; p = p + 1; *p = *p + 1; } (A) (B) (C) (D) Data Address (decimal) a[0] a[1] 5 10. p 100 . . p *p a[0] a[1]then p *p a[0] a[1] 101 10 5 10 then 101 11 5 11 104 10 5 10 then 104 11 5 11 100 6 6 10 then 101 6 6 10 100 6 6 10 then 104 6 6 10 22
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Representing strings v C-style string stored as an array of bytes (char *) § Elements are one-byte ASCII codes for each character § No “String” keyword, unlike Java 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 space ! ” # $ % & ’ ( ) * + , . / 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 @ A B C D E F G H I J K L M N O 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 P Q R S T U V W X Y Z [ ] ^ _ 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 ` a b c d e f g h I j k l m n o 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 ASCII: American Standard Code for Information Interchange p q r s t u v w x y z { | } ~ del 23
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Null-Terminated Strings v Example: “Donald Trump” stored as a 13 -byte array Decimal: . . 68 111 110 97 108 100 32 84 117 109 112 0 Hex: . . 0 x 44 0 x 6 F 0 x 6 E 0 x 61 0 x 6 C 0 x 64 0 x 20 0 x 54 0 x 72 0 x 75 0 x 6 D 0 x 70 0 x 00 Text: . . D o n a l d T r u m p v v Last character followed by a 0 byte (‘ ’) (a. k. a. “null terminator”) § Must take into account when allocating space in memory § Note that ‘ 0’ ≠ ‘ ’ (i. e. character 0 has non-zero value) How do we compute the length of a string? § Traverse array until null terminator encountered 24
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 C (char = 1 byte) Endianness and Strings char s[6] = "12345"; String literal 0 x 31 = 49 decimal = ASCII ‘ 1’ v IA 32, x 86 -64 (little-endian) SPARC (big-endian) 31 32 33 34 35 00 0 x 01 0 x 02 0 x 03 0 x 04 0 x 05 ‘ 1’ ‘ 2’ ‘ 3’ ‘ 4’ ‘ 5’ ‘ ’ Byte ordering (endianness) is not an issue for 1 -byte values § The whole array does not constitute a single value § Individual elements are values; chars are single bytes 25
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Examining Data Representations v Code to print byte representation of data § Any data type can be treated as a byte array by casting it to char § C has unchecked casts !! DANGER !! void show_bytes(char* start, int len) { int i; for (i = 0; i < len; i++) printf("%pt 0 x%. 2 xn", start+i, *(start+i)); printf("n"); } printf directives: %p Print pointer t Tab %x Print value as hex n New line 26
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Examining Data Representations v Code to print byte representation of data § Any data type can be treated as a byte array by casting it to char § C has unchecked casts !! DANGER !! void show_bytes(char* start, int len) { int i; for (i = 0; i < len; i++) printf("%pt 0 x%. 2 xn", start+i, *(start+i)); printf("n"); } void show_int(int x) { show_bytes( (char *) &x, sizeof(int)); } 27
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 show_bytes Execution Example int a = 12345; // 0 x 00003039 printf("int a = 12345; n"); show_int(a); v // show_bytes((char *) &a, sizeof(int)); Result (Linux x 86 -64): § Note: The addresses will change on each run (try it!), but fall in same general range int a = 12345; 0 x 7 fffb 7 f 71 dbc 0 x 39 0 x 7 fffb 7 f 71 dbd 0 x 30 0 x 7 fffb 7 f 71 dbe 0 x 00 0 x 7 fffb 7 f 71 dbf 0 x 00 28
L 01: L 04: Intro, Memory Combinational & Data Logic II CSE 369, CSE 410, Autumn Winter 2017 2016 Summary v v Assignment in C results in value being put in memory location Pointer is a C representation of a data address § & = “address of” operator § * = “value at address” or “dereference” operator Pointer arithmetic scales by size of target type § Convenient when accessing array-like structures in memory § Be careful when using – particularly when casting variables Arrays are adjacent locations in memory storing the same type of data object § Strings are null-terminated arrays of characters (ASCII) 29
- Slides: 29