Memory, Data, & Addressing II CSE 351 Winter 2018

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Arrays and Pointers in C Alan L. Cox
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Bits, Bytes, and Integers Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers Basic.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
University of Washington A few announcements Course web page – check often  cse.uw.edu/351  Schedule, policies, labs, homeworks, and everything else.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Week 2 - Wednesday CS 121.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
The Machine Model Memory
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Memory, Data, & Addressing II CSE 351 Autumn 2017
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
CSE 303 Concepts and Tools for Software Development
CISC/CMPE320 - Prof. McLeod
Arrays in C.
Information Storage.
COMP3221: Microprocessors and Embedded Systems
Programmazione I a.a. 2017/2018.
Programming Languages and Paradigms
Lecture 6 C++ Programming
CSE351: Memory and data
Announcements VM on the website! Speedometer!
void Pointers Lesson xx
Memory, Data, & Addressing II CSE 410 Winter 2017
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Machine-Level Programming 6 Structured Data
Data III & Integers I CSE 351 Winter 2017
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Memory, Data, & Addressing II CSE 351 Winter 2018
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Bits and Bytes Topics Representing information as bits
Memory, Data, & Addressing II CSE 351 Summer 2018
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Memory, Data, & Addressing II CSE 351 Autumn 2018
int [] scores = new int [10];
Homework Homework Continue Reading K&R Chapter 2 Questions?
Chapter 16 Pointers and Arrays
Your questions from last session
CISC/CMPE320 - Prof. McLeod
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Homework Reading Programming Assignments Finish K&R Chapter 1
Comp Org & Assembly Lang
Overloading functions
Data III & Integers I CSE 351 Winter 2018
Bits, Bytes, and Integers Part 2 3rd Lectures
Comp Org & Assembly Lang
Data III & Integers I CSE 351 Winter 2018
Data Structures and Algorithms Introduction to Pointers
Memory, Data, & Addressing II CSE 351 Winter 2019
CISC/CMPE320 - Prof. McLeod
“The Class That Gives CMU Its Zip!”
“The Class That Gives CMU Its Zip!”
Introduction to C CS 3410.
Presentation transcript:

Memory, Data, & Addressing II CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants: Kevin Bi Parker DeWilde Emily Furst Sarah House Waylon Huang Vinny Palaniappan Close the door Keep posting questions to Piazza http://xkcd.com/138/

Administrivia Lab 0 due today @ 11:59pm Homework 1 due Wednesday You will be revisiting this program throughout this class! Homework 1 due Wednesday Reminder: autograded, 20 tries, no late submissions Lab 1 released today Prelim due Jan. 15 Due Jan. 19

Memory, Data, and Addressing Representing information as bits and bytes Organizing and addressing data in memory Manipulating data in memory using C Boolean algebra and bit-level manipulations Last lecture: Basic view of hardware byte and word oriented views of memory byte ordering Today: introduce some C syntax to help us manipulate memory

Addresses and Pointers in C A pointer is a variable that holds an address Pointers are declared similarly to other variables in C Type (e.g., int *) Name (e.g., ptr) Declaration, Initialization, Assignment Type is specified using one (or more) * after some type T int *ptr; struct Scores *s; double **dPtr; Operators & = “address of” operator * = “dereference” operator, or “value at address” First, let’s talk about addresses and pointers in C. Defined. We won’t worry about ** just yet, may encounter later on. But it is simply a pointer to a pointer. Two operators used with pointers: & is the address of operation * is dereference operation Note two slightly different uses of * here in declarations to specify type as a unary operator

Assignment in C A variable is represented by a memory location Declaration ≠ initialization (initially holds “garbage”) Left-Hand Side = Right-Hand Side = operator LHS is a memory location RHS is a value (could be an address) Assignment in C uses the = operator Assign RHS to LHS

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C little-endian int x, y; x is at address 0x04, y is at 0x18 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 00 01 29 F3 x Let’s go through an example. 32-bit word machine (pointers are 32-bit), little endian machine Remember: Little Endian is LSB at Lowest address (L-L-L) 01 00 y

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 00 x little endian! Animation: little endian!! 01 00 00 27 D0 3C y

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; x = y + 3; Get value at y, add 3, store in x 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C 00 x Animate: update value in x 00 27 D0 3C y

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; x = y + 3; Get value at y, add 3, store in x int *z; z is at address 0x20 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x Now, we declare a pointer, z Assume z is at address 0x20 What is stored in z? Garbage! 00 27 D0 3C y DE AD BE EF z

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; x = y + 3; Get value at y, add 3, store in x int *z = &x; &x = 0x00000004 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x Using the address of operator, &, we can initialize z. Here, we store the address of the variable x into z. Next slide to update 00 27 D0 3C y DE AD BE EF z

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; x = y + 3; Get value at y, add 3, store in x int *z = &x; &x = 0x00000004 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x “Z points to X” 00 27 D0 3C y 04 00 z

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; x = y + 3; Get value at y, add 3, store in x int *z = &x; &x = 0x00000004 int y = *z + 1; 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x Assign to Y the value pointed to by Z plus 1 00 27 D0 3C y 04 00 z

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; x = y + 3; Get value at y, add 3, store in x int *z = &x; &x = 0x00000004 int y = *z + 1; y = 0x3CD02704 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x Assign to Y the value stored in the address Z points to (also called X), plus 1 04 27 D0 3C y 04 00 z

Pointer Arithmetic in C 32-bit example (pointers are 32-bits wide) Pointer Arithmetic in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; x = y + 3; Get value at y, add 3, store in x int *z; z is at address 0x20 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x We have seen how to use pointers and the & and * operators. Now, let’s talk about how we manipulate an address stored in a pointer variable. We use something called Pointer Arithmetic. Let’s step back a few slides to the declaration of the pointer z. 00 27 D0 3C y DE AD BE EF z

Pointer Arithmetic in C 32-bit example (pointers are 32-bits wide) Pointer Arithmetic in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; x = y + 3; Get value at y, add 3, store in x int *z = &y + 3; Get address of y, “add 3”, store in z 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x Assign address of y plus 3 to Z? What value does Z hold? Is it 0x1B? Because &y is 0x18, plus 3 = 0x1B No! 00 27 D0 3C y DE AD BE EF z Pointer arithmetic

Pointer Arithmetic in C Pointer arithmetic is scaled by the size of the pointer’s target data type In this example, sizeof(int) = 4 int* z = &y + 3; Get address of y, add 3*sizeof(int), store in z &y = 0x18 24 + 3*(4) = 36 Pointer arithmetic can be dangerous! Can easily lead to bad memory accesses Be careful with data types and casting = 1*161 + 8*160 = 24 = 2*161 + 4*160 = 0x24 After 0x24, this is not 0x1B, as we might have thought!! This shows that pointer arithmetic can be dangerous, because you need to remember the scaling!

Pointer Arithmetic in C 32-bit example (pointers are 32-bits wide) Pointer Arithmetic in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; 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 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x 00 27 D0 3C y 24 00 z

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; 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? 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x Recap example to this point. Assignment to z is y + 12, store in z 0x24. Now, let’s talk about this statement: *z = y 00 27 D0 3C y 24 00 z

(pointers are 32-bits wide) 32-bit example (pointers are 32-bits wide) Assignment in C & = “address of” * = “dereference” int x, y; x = 0; y = 0x3CD02700; 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; Get value of y, put in address stored in z 0x00 0x01 0x02 0x03 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x24 03 27 D0 3C x We know y is an int. So logically, we are storing an int value somewhere. *z 00 27 D0 3C y The target of a pointer is also a memory location 24 00 z 00 27 D0 3C

(pointers are 64-bits wide) 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 Declaration: int a[6]; 64-bit example (pointers are 64-bits wide) element type a[1] a[3] a[5] number of elements name 0x4 0xC 0x5 0xD 0x6 0xE 0x7 0xF 0x0 0x8 0x1 0x9 0x2 0xA 0x3 0xB 0x00 0x08 0x10 0x18 0x20 0x28 0x30 0x38 0x40 0x48 a[0] a[2] a[4] Here, declare an array of integers, named ‘a’, with 6 elements. Let’s say this array starts at address 0x10

Arrays in C Declaration: int a[6]; The address of a[i] is the address of a[0] plus i times the element size in bytes a is a name for the array’s address Arrays are adjacent locations in memory storing the same type of data object Arrays in C Declaration: int a[6]; Indexing: a[0] = 0x015f; a[5] = a[0]; 0x4 0xC 0x5 0xD 0x6 0xE 0x7 0xF 0x0 0x8 0x1 0x9 0x2 0xA 0x3 0xB 0x00 0x08 0x10 0x18 0x20 0x28 0x30 0x38 0x40 0x48 a[0] a[2] a[4] 00 01 5F As with Java, in C we can index into an array (zer0-based) The address of a[i] is the address of a[0] (first element) plus the index i * the element size in bytes Here we assign values to index 0 and 5 00 01 5F

Arrays in C Declaration: int a[6]; The address of a[i] is the address of a[0] plus i times the element size in bytes a is a name for the array’s address Arrays are adjacent locations in memory storing the same type of data object Arrays in C Declaration: int a[6]; Indexing: a[0] = 0x015f; a[5] = a[0]; No bounds a[6] = 0xBAD; checking: a[-1] = 0xBAD; 0x4 0xC 0x5 0xD 0x6 0xE 0x7 0xF 0x0 0x8 0x1 0x9 0x2 0xA 0x3 0xB 0x00 0x08 0x10 0x18 0x20 0x28 0x30 0x38 0x40 0x48 00 0B AD a[0] a[2] a[4] 00 01 5F Unlike Java, C has no bounds checking!! We can assign values using array indexing into memory locations that exist outside of the array. You saw this in Lab 0. 00 01 5F 00 0B AD

Arrays in C Declaration: int a[6]; The address of a[i] is the address of a[0] plus i times the element size in bytes a is a name for the array’s address Arrays are adjacent locations in memory storing the same type of data object Arrays in C Declaration: int a[6]; Indexing: a[0] = 0x015f; a[5] = a[0]; No bounds a[6] = 0xBAD; checking: a[-1] = 0xBAD; Pointers: int* p; p = a; p = &a[0]; *p = 0xA; 0x4 0xC 0x5 0xD 0x6 0xE 0x7 0xF 0x0 0x8 0x1 0x9 0x2 0xA 0x3 0xB 0x00 0x08 0x10 0x18 0x20 0x28 0x30 0x38 0x40 0x48 00 0B AD equivalent a[0] a[2] a[4] 00 01 5F 00 0A It turns out, that pointers can be used to operate on arrays in C. We can declare a pointer p, assign the address of the array a to it, then write values into the array through p. 00 01 5F 00 0B AD p 00 10 00

Arrays in C Declaration: int a[6]; The address of a[i] is the address of a[0] plus i times the element size in bytes a is a name for the array’s address Arrays are adjacent locations in memory storing the same type of data object Arrays in C Declaration: int a[6]; Indexing: a[0] = 0x015f; a[5] = a[0]; No bounds a[6] = 0xBAD; checking: a[-1] = 0xBAD; Pointers: int* p; p = a; p = &a[0]; *p = 0xA; p[1] = 0xB; *(p+1) = 0xB; p = p + 2; 0x4 0xC 0x5 0xD 0x6 0xE 0x7 0xF 0x0 0x8 0x1 0x9 0x2 0xA 0x3 0xB 0x00 0x08 0x10 0x18 0x20 0x28 0x30 0x38 0x40 0x48 00 0B AD equivalent a[0] a[2] a[4] 00 01 5F 00 0A 00 0B Array indexing is actually equivalent to address or pointer arithmetic. Both are scaled by the size of the data type. And p is a pointer, so we can do pointer arithmetic on it. For example, let’s do p = p + 2 Animate: arrow, then next slide 00 01 5F array indexing = address arithmetic (both scaled by the size of the type) 00 0B AD equivalent p 00 10 00

Arrays in C Declaration: int a[6]; The address of a[i] is the address of a[0] plus i times the element size in bytes a is a name for the array’s address Arrays are adjacent locations in memory storing the same type of data object Arrays in C Declaration: int a[6]; Indexing: a[0] = 0x015f; a[5] = a[0]; No bounds a[6] = 0xBAD; checking: a[-1] = 0xBAD; Pointers: int* p; p = a; p = &a[0]; *p = 0xA; p[1] = 0xB; *(p+1) = 0xB; p = p + 2; *p = a[1] + 1; 0x4 0xC 0x5 0xD 0x6 0xE 0x7 0xF 0x0 0x8 0x1 0x9 0x2 0xA 0x3 0xB 0x00 0x08 0x10 0x18 0x20 0x28 0x30 0x38 0x40 0x48 00 0B AD equivalent a[0] a[2] a[4] 00 0A 00 0B 00 0C And, p is updated to point to the third element in the array, at address 0x18 Then, we can assign a value into the location pointed to by p using indirection. Here we store the 2nd array value + 1 00 01 5F array indexing = address arithmetic (both scaled by the size of the type) 00 0B AD equivalent p 00 18 00

Question: The variable values after Line 3 executes are shown on the right. What are they after Line 4 & 5? 1 void main() { 2 int a[] = {5,10}; 3 int* p = a; 4 p = p + 1; 5 *p = *p + 1; 6 } 100 a[0] a[1] p 5 10 . Address (decimal) Data p *p a[0] a[1]then p *p a[0] a[1] Skip if short on time. Answer: B After line 3, p points to a[0]. Line 4 performs pointer arithmetic, adding 1*sizeof(int) to p, making p = 104. p now points to a[1] Line 5 adds 1 to the value pointed to by p (a[1]), and stores it in the location pointed to by p (a[1]) 101 10 5 10 then 101 11 5 11 (A) 104 10 5 10 then 104 11 5 11 (B) 100 6 6 10 then 101 6 6 10 (C) 100 6 6 10 then 104 6 6 10 (D)

ASCII: American Standard Code for Information Interchange Representing strings 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 space 48 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 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 I pronounce it ‘care’, you can also call it ‘char’ if you care to ASCII: American Standard Code for Information Interchange

Null-Terminated Strings Example: “Luke and Leia” stored as a 14-byte array Last character followed by a 0 byte (‘\0’) (a.k.a. “null terminator”) Must take into account when allocating space in memory Note that ‘0’ ≠ ‘\0’ (i.e. character 0 has non-zero value) How do we compute the length of a string? Traverse array until null terminator encountered Decimal:.. 76 117 107 101 32 97 110 100 105 Hex:.. 0x4c 0x75 0x6b 0x65 0x20 0x61 0x6e 0x64 0x69 0x00 Text:.. L u k e a n d i \0 Also strlen() in #include <string.h>, though doesn’t count null-terminator

Endianness and Strings C (char = 1 byte) char s[6] = "12345"; 33 34 31 32 35 00 0x00 0x01 0x02 0x03 0x04 0x05 ‘1’ ‘2’ ‘3’ ‘4’ ‘5’ ‘\0’ IA32, x86-64 (little-endian) SPARC (big-endian) String literal 0x31 = 49 decimal = ASCII ‘1’ 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

Summary 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)

Examining Data Representations 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("%p\t0x%.2x\n", start+i, *(start+i)); printf("\n"); } %p: prefix “0x” automatically printed printf directives: %p Print pointer \t Tab %x Print value as hex \n New line

Examining Data Representations 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("%p\t0x%.2x\n", start+i, *(start+i)); printf("\n"); } %p: prefix “0x” automatically printed void show_int(int x) { show_bytes( (char *) &x, sizeof(int)); }

show_bytes Execution Example Result (Linux x86-64): Note: The addresses will change on each run (try it!), but fall in same general range int x = 12345; // 0x00003039 printf("int x = %d;\n“,x); show_int(x); // show_bytes((char *) &x, sizeof(int)); int x = 12345; 0x7fffb7f71dbc 0x39 0x7fffb7f71dbd 0x30 0x7fffb7f71dbe 0x00 0x7fffb7f71dbf 0x00