Memory and Its Data
Total Page:16
File Type:pdf, Size:1020Kb
University of Washington Computer Systems CSE 410 Autumn 2013 4 – Memory and its Data Memory 1 University of Washington Memory, Data, and Addressing Preliminaries Representing information as bits and bytes Organizing and addressing data in memory Manipulating data in memory using C Memory 2 University of Washington Hardware: Logical View CPU Memory Bus Disks Net USB Etc. Memory 3 University of Washington Memory We know that information is represented using bit strings ▪ Terminology ▪ Byte: 8 bits ▪ Word: the “natural size” for the processor – Usually 32 or 64 bits Data represenations ▪ An integer might require 4 bytes to represent ▪ A long integer 8 ▪ A character might take one or two bytes ▪ An image might take millions of bytes Places we store values are called memory Memory 4 University of Washington Computer System Memory The Central Processing Unit (CPU) manipulates information ▪ It can add, subtract, and, or, etc. ▪ It can compare and branch Basic system architecture: Example: 16GB Main DDR4-2400 CPU Memory (2400x8=19,200 MB/sec) Example: Core i5-8400 2.8GHz, 6 cores, 6 threads Memory 5 University of Washington Memory is slow Registers Main CPU Memory There are a very limited number of registers (often 16) The “programmer” (compiler) is aware of the registers Instructions load memory into a register and store a register into memory Memory 6 University of Washington Caches (later in the course) Cache Main CPU Memory Caches are managed by the hardware ▪ They’re transparent to the program Both registers and caches address latency: the delay from when a request is made and the first bit becomes available Memory 7 University of Washington Basic CPU Operation “Machine instructions” are bit strings ▪ We’ll look in more detail in a bit The feel of machine instructions ▪ load an integer from memory into register 2 ▪ load another integer from memory into register 3 ▪ set reg 3 = reg 2 + reg 3 The “Program Counter” (PC) contains the address of the next instruction to execute Basic CPU operation loop: ▪ Fetch the instruction pointed at by the PC ▪ Increment the PC to point to the next instruction ▪ Execute the instruction just fetched Memory 8 University of Washington Byte-Oriented Memory Organization • • • Programs refer to addresses ▪ Conceptually, a very large array of bytes, each with an address (index) ▪ System provides an address space private to each “process” ▪ Process = program being executed + its data + its “state” ▪ Program can clobber its own data, but not that of others ▪ Clobbering code or “state” often leads to crashes (or security holes) Compiler + run-time system control memory allocation ▪ Where different program objects should be stored ▪ All allocation within a single address space Memory 9 University of Washington Machine Words Machine has a “word size” ▪ Nominal size of integer-valued data ▪ Including addresses ▪ Until recently, most machines used 32 bits (4 bytes) words ▪ Limits addresses to 4GB ▪ Became too small for memory-intensive applications ▪ More recent and high-end systems use 64 bits (8 bytes) words ▪ Potential address space 1.8 X 1019 bytes (18 EB – exabytes) ▪ x86-64 supports 48-bit physical addresses: 256 TB (terabytes) ▪ For backward-compatibility, many CPUs support different word sizes ▪ Always a power-of-2 in the number of bytes: 1, 2, 4, 8, … Memory 10 University of Washington Word-Oriented Memory Organization 64-bit 32-bit Bytes Addr. Addresses specify Words Words locations of bytes in memory 0000 Addr ▪ Address of first byte in word = 0001 ?? 0002 ▪ Addresses of successive words Addr differ by 4 (32-bit) or 8 (64-bit) = 0003 ?? 0004 ▪ Address of word 0, 1, .. 10? Addr = 0005 ?? 0006 0007 0008 Addr = 0009 ?? Addr 0010 = 0011 ?? 0012 Addr = 0013 ?? 0014 0015 Memory 11 University of Washington Word-Oriented Memory Organization 64-bit 32-bit Bytes Addr. Addresses specify Words Words locations of bytes in memory 0000 Addr ▪ Address of first byte in word = 0001 0000?? 0002 ▪ Addresses of successive words Addr differ by 4 (32-bit) or 8 (64-bit) = 0003 0000?? 0004 ▪ Address of word 0, 1, .. 10? Addr = 0005 0004?? 0006 0007 0008 Addr = 0009 0008?? Addr 0010 = 0011 0008?? 0012 Addr = 0013 0012?? 0014 0015 Memory 12 University of Washington Memory, Data, and Addressing Preliminaries Representing information as bits and bytes Organizing and addressing data in memory Manipulating data in memory using C Memory 13 University of Washington Addresses and Pointers Address is a location in memory Pointer is a data object that contains an address Here address 0004 0000 stores the value 351 (or 15F16) 00 00 01 5F 0004 0008 000C 0010 0014 0018 001C 0020 0024 Memory 14 University of Washington Addresses and Pointers Address is a location in memory Pointer is a data object that contains an address Address 0004 0000 stores the value 351 (or 15F16) 00 00 01 5F 0004 Pointer to address 0004 0008 stored at address 001C 000C 0010 0014 0018 00 00 00 04 001C 0020 0024 Memory 15 University of Washington Addresses and Pointers Address is a location in memory Pointer is a data object that contains an address Address 0004 0000 stores the value 351 (or 15F16) 00 00 01 5F 0004 Pointer to address 0004 0008 stored at address 001C 000C 0010 Pointer to a pointer 0014 in 0024 0018 00 00 00 04 001C 0020 00 00 00 1C 0024 Memory 16 University of Washington Addresses and Pointers Address is a location in memory Pointer is a data object that contains an address Address 0004 0000 stores the value 351 (or 15F16) 00 00 01 5F 0004 Pointer to address 0004 0008 stored at address 001C 000C 0010 Pointer to a pointer 00 00 00 0C 0014 in 0024 0018 Address 0014 00 00 00 04 001C stores the value 12 0020 00 00 00 1C 0024 ▪ Is it a pointer? Memory 17 University of Washington Pointers Q: What makes some bit string a pointer? A: That we use it as one Q: What makes some bit string an integer? A: That we use it as one. Memory 18 University of Washington Byte Ordering How should bytes within multi-byte word be ordered in memory? Say you want to store 0xaabbccdd ▪ What order will the four bytes 0xaa, 0xbb, 0xcc, and 0xdd be stored? Memory 19 University of Washington Byte Ordering How should bytes within multi-byte word be ordered in memory? Say you want to store 0xaabbccdd ▪ What order will the bytes be stored? Endianness: big endian vs. little endian ▪ Two different conventions, used by different architectures ▪ Origin: Gulliver’s Travels (see CS:APP2 textbook, section 2.1) Memory 20 University of Washington Byte Ordering Example Big-Endian (Internet) ▪ Least significant byte has highest address Little-Endian (x86) ▪ Least significant byte has lowest address Example ▪ Variable has 4-byte representation 0x01234567 ▪ Address of variable is 0x100 0x100 0x101 0x102 0x103 Big Endian 01 23 45 67 0x100 0x101 0x102 0x103 Little Endian 67 45 23 01 Memory 21 University of Washington Memory, Data, and Addressing Preliminaries Representing information as bits and bytes Organizing and addressing data in memory Manipulating data in memory using C Memory 22 University of Washington & = ‘address of value’ Addresses and Pointers in C * = ‘value at address’ or ‘dereference’ Variable declarations ▪ int x, y; ▪ Finds two locations in memory in which to store 2 integers (1 word each) Pointer declarations use * ▪ int *ptr; ▪ Declares a variable ptr that is a pointer to a data item that is an integer Assignment to a pointer ▪ ptr = &x; ▪ Assigns ptr to point to the address where x is stored ▪ (stores the address of x in ptr) Memory 23 University of Washington & = ‘address of value’ Addresses and Pointers in C * = ‘value at address’ or ‘dereference’ To use the value pointed to by a pointer we use dereference (*) ▪ Given a pointer, we can get the value it points to by using the * operator ▪ *ptr is the value at the memory address given by the value of ptr Examples ▪ If ptr = &x then y = *ptr + 1 is the same as y = x + 1 ▪ If ptr = &y then y = *ptr + 1 is the same as y = y + 1 ▪ What is *(&x) equivalent to? Memory 24 University of Washington & = ‘address of value’ Addresses and Pointers in C * = ‘value at address’ or ‘dereference’ C allows us to do arithmetic on pointers ▪ ptr = ptr + 1; // really adds 4: type of ptr is int*, and an int uses 4 bytes! ▪ Changes the value of the pointer so that it now points to the next data item in memory (that may be y, or it may not – this is dangerous!) Memory 25 University of Washington Assignment in C Left-hand-side = right-hand-side ▪ LHS must evaluate to a memory LOCATION ▪ RHS must evaluate to a VALUE (could be an address) E.g., x at location 0x04, y at 0x18 ▪ x originally 0x0, y originally 0x3CD02700 0000 00 00 00 00 0004 0008 000C 0010 0014 00 27 D0 3C 0018 001C 0020 0024 Memory 26 University of Washington Assignment in C Left-hand-side = right-hand-side ▪ LHS must evaluate to a memory LOCATION ▪ RHS must evaluate to a VALUE (could be an address) E.g., x at location 0x04, y at 0x18 ▪ x originally 0x0, y originally 0x3CD02700 0000 00 00 00 00 0004 ▪ int x, y; 0008 x = y + 3; //get value at y, add 3, put it in x 000C 0010 0014 00 27 D0 3C 0018 001C 0020 0024 Memory 27 University of Washington Assignment in C Left-hand-side = right-hand-side ▪ LHS must evaluate to a memory LOCATION ▪ RHS must evaluate to a VALUE (could be an address) E.g., x at location 0x04, y at 0x18 ▪ x originally 0x0, y originally 0x3CD02700 0000 03 27 D0 3C 0004 ▪ int x, y; 0008 x = y + 3; //get value at y, add 3, put it in x 000C 0010 0014 00 27 D0 3C 0018 001C 0020 0024 Memory 28 University