Roadmap C: Java: Assembly language: OS: Machine code: Computer system:

Slides:



Advertisements
Similar presentations
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Advertisements

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Data Representation and Alignment Topics Simple static allocation and alignment of basic types and data structures Policies Mechanisms.
Machine-Level Programming 6 Structured Data Topics Structs Unions.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Ithaca College 1 Machine-Level Programming VIII: Advanced Topics: alignment & Unions Comp 21000: Introduction to Computer Systems & Assembly Lang Systems.
Machine-Level Programming IV: Structured Data Topics Arrays Structs Unions CS 105 “Tour of the Black Holes of Computing”
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
University of Amsterdam Computer Systems – Data in C Arnoud Visser 1 Computer Systems New to C?
University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Topics: Arrays.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data MCS284: Computer.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted.
Lecture 9 Aggregate Data Topics Arrays Structs Unions again Lab 02 comments Vim, gedit, February 22, 2016 CSCE 212 Computer Architecture.
Spring 2016Assembly Review Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Chapter 7 Memory Management
Memory Management Chapter 7.
Java and C I CSE 351 Spring 2017 Instructor: Ruth Anderson
Final exam: Wednesday, March 20, 2:30pm
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data
Arrays CSE 351 Spring 2017 Instructor: Ruth Anderson
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming IV: Structured Data
Run-time organization
Today How’s Lab 3 going? HW 3 will be out today
CSE351: Memory, Data, & Addressing I CSE 351 Spring 2017
Machine-Level Programming IV: Data
The Hardware/Software Interface CSE351 Winter 2013
Switch & Procedures & The Stack I CSE 351 Winter 2017
Heterogeneous Data Structures & Alignment
Java and C I CSE 351 Autumn 2016 Instructor: Justin Hsia
Machine-Level Programming IV: Data CSCE 312
Data III & Integers I CSE 351 Autumn 2016
Structs and Alignment CSE 351 Autumn 2016
Arrays CSE 351 Winter
Feb 2 Announcements Arrays/Structs in C Lab 2? HW2 released
CS Introduction to Operating Systems
Machine-Level Programming 6 Structured Data
Machine-Level Programming IV: Data
Data III & Integers I CSE 351 Winter 2017
Instructor: Phil Gibbons
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
CSE 351 Section 10 The END…Almost 3/7/12
Structs and Alignment CSE 410 Winter 2017
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data /18-213/14-513/15-513: Introduction to Computer Systems 8th Lecture, September 20, 2018.
Instructors: Majd Sakr and Khaled Harras
Java and C CSE 351 Summer 2018 Instructor: Justin Hsia
Machine-Level Programming IV: Machine Data 9th Lecture
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Java and C CSE 351 Winter 2018 Instructor: Mark Wyse
Operating System Chapter 7. Memory Management
Arrays CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants:
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Structs & Alignment CSE 351 Autumn 2018
Executables & Arrays CSE 351 Autumn 2018
Java and C CSE 351 Autumn 2018 Instructor: Teaching Assistants:
Java and C I CSE 351 Spring 2017 Instructor: Ruth Anderson
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Instructor: Fatma CORUT ERGİN
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Data Spring, 2019 Euiseong Seo
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming IV: Data
August 31 addresses Drop box Questions? 31 August 2004
Presentation transcript:

Roadmap C: Java: Assembly language: OS: Machine code: Computer system: Memory & data Integers & floats Machine code & C x86 assembly Procedures & stacks Arrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C C: Java: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100); c.setGals(17); float mpg = c.getMPG(); Assembly language: get_mpg: pushq %rbp movq %rsp, %rbp ... popq %rbp ret OS: Machine code: 0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111 Computer system:

Data Structures in Assembly Arrays One-dimensional Multi-dimensional (nested) Multi-level Structs Alignment Unions

Review: Structs in Lab 0 // Use typedef to create a type: FourInts typedef struct { int a, b, c, d; } FourInts; // Name of type is “FourInts” int main(int argc, char* argv[]) { FourInts f1; // Allocates memory to hold a FourInts // (16 bytes) on stack (local variable) f1.a = 0; // Assign the first field in f1 to be zero FourInts* f2; // Declare f2 as a pointer to a FourInts // Allocate space for a FourInts on the heap, // f2 is a “pointer to”/”address of” this space. f2 = (FourInts*)malloc(sizeof(FourInts)); f2->b = 17; // Assign the second field to be 17 … }

Syntax for structs without typedef struct rec { // Declares the type “struct rec” int a[4]; // Total size = _______ bytes long i; struct rec *next; }; struct rec r1; // Allocates memory to hold a struct rec // named r1, on stack or globally, // depending on where this code appears struct rec *r; // Allocates memory for a pointer r = &r1; // Initializes r to “point to” r1 Minor syntax note: Need that semicolon after a struct declaration (easy to forget)

Syntax for structs with typedef struct rec { // Declares the type “struct rec” int a[4]; // Total size = _______ bytes long i; struct rec *next; }; struct rec r1; // Allocates memory to hold a struct rec // named r1, on stack or globally, // depending on where this code appears struct rec *r; // Allocates memory for a pointer r = &r1; // Initializes r to “point to” r1 typedef struct rec { int a[4]; long i; struct rec *next; } Record; // typedef creates new name for ‘struct rec’ // (that doesn’t need ‘struct’ in front of it) Record r2; // Declare variable of type ‘Record’ // (really a ‘struct rec’)

More Structs Syntax Equivalent to: struct rec { // Declares the type “struct rec” int a[4]; long i; struct rec *next; }; struct rec r1; // Declares r1 as a struct rec Equivalent to: struct rec { // Declares the type “struct rec” int a[4]; long i; struct rec *next; } r1; // Declares r1 as a struct rec

More Structs Syntax: Pointers struct rec { // Declares the type “struct rec” int a[4]; long i; struct rec *next; }; struct rec *r; // Declares r as pointer to a struct rec Equivalent to: struct rec { // Declares the type “struct rec” int a[4]; long i; struct rec *next; } *r; // Declares r as pointer to a struct rec

Accessing Structure Members struct rec { int a[4]; long i; struct rec *next; }; Given an instance of the struct, we can use the . operator: struct rec r1; r1.i = val; Given a pointer to a struct: struct rec *r; r = &r1; // or malloc space for r to point to We have two options: Using * and . operators: (*r).i = val; Or, use -> operator for short: r->i = val; The pointer is the address of the first byte of the structure Access members with offsets How does a “.” access get translated into assembly? It looks exactly like a pointer access: an offset is added to the beginning of the struct to reach the appropriate member, then the access is performed. When a “.” access is performed, no “pointer” (i.e. no address kept in some register) is needed: the struct’s location on the current function’s stack frame (or in the args section of the caller’s stack frame) is always known, so the offsets can always be calculated relative to %rsp.

Java: Java side-note class Record { ... } Record x = new Record(); An instance of a class is like a pointer to a struct containing the fields (Ignoring methods and subclassing for now) So Java’s x.f is like C’s x->f, i.e., (*x).f In Java, almost everything is a pointer (“reference”) to an object Cannot declare variables or fields that are structs or arrays Always a pointer to a struct or array So every Java variable or field is <= 8 bytes (but can point to lots of data)

Structure Representation next 16 24 32 struct rec { int a[4]; long i; struct rec *next; } *r; a Characteristics Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types

Structure Representation next 16 24 32 struct rec { int a[4]; long i; struct rec *next; } *r; a Structure represented as block of memory Big enough to hold all of the fields Fields ordered according to declaration order Even if another ordering could yield a more compact representation Compiler determines overall size + positions of fields Machine-level program has no understanding of the structures in the source code

Accessing a Structure Member next 16 24 32 r->i struct rec { int a[4]; long i; struct rec *next; } *r; a Compiler knows the offset of each member within a struct. Compute as: *(r+offset) long get_i(struct rec *r) { return r->i; } # r in %rdi, index in %rsi movq 16(%rdi), %rax ret

Exercise: Generating Pointer to Structure Member next 16 24 32 struct rec { int a[4]; long i; struct rec *next; } *r; a long* address_of_i(struct rec *r) { return &(r->i); } # r in %rdi ,%rax ret struct rec* address_of_next(struct rec *r) { return &(r->next); } # r in %rdi ,%rax ret

Exercise: Generating Pointer to Structure Member next 16 24 32 struct rec { int a[4]; long i; struct rec *next; } *r; a long* address_of_i(struct rec *r) { return &(r->i); } # r in %rdi leaq 16(%rdi), %rax ret struct rec* address_of_next(struct rec *r) { return &(r->next); } # r in %rdi leaq 24(%rdi), %rax ret

Generating Pointer to Structure Member next 16 24 32 r + 4*index struct rec { int a[4]; long i; struct rec *next; } *r; a Generating Pointer to Array Element Offset of each structure member determined at compile time Compute as: r + 4*index int* find_address_of_elem (struct rec *r, long index) { return &r->a[index]; } Note the order of operation: & applies to the entire expression Also: 2 instances of “syntactic sugar” &(r->a[index]) # r in %rdi, index in %rsi leaq (%rdi,%rsi,4), %rax ret

Review: Memory Alignment in x86-64 For good memory system performance, Intel recommends data be aligned However the x86-64 hardware will work correctly regardless of alignment of data. Aligned means: Any primitive object of K bytes must have an address that is a multiple of K. This means we could expect these types to have starting addresses that are the following multiples: K Type Addresses 1 char No restrictions 2 short Lowest bit must be zero: …02 4 int, float Lowest 2 bits zero: …002 8 long, double, pointers Lowest 3 bits zero: …0002 16 long double Lowest 4 bits zero: …00002

Alignment Principles Aligned Data Motivation for Aligning Data Primitive data type requires K bytes Address must be multiple of K Required on some machines; advised on x86-64 Motivation for Aligning Data Memory accessed by (aligned) chunks of 4 or 8 bytes (system dependent) Inefficient to load or store value that spans quad word boundaries Virtual memory trickier when value spans 2 pages (more on this later)

Structures & Alignment Unaligned Data Aligned Data Primitive data type requires K bytes Address must be multiple of K struct S1 { char c; int i[2]; double v; } *p; c i[0] i[1] v p p+1 p+5 p+9 p+17 struct S1 { … } *p; - this defines a new type “struct S1”, and creates a variable p with type “struct S1 *” #include <stddef.h>// for offsetof printf("sizeof p = %d\n", (int) sizeof(p)); printf("offset of v in p = %d\n", (int) offsetof(struct S1, v)); c 3 bytes i[0] i[1] 4 bytes v p+0 p+4 p+8 p+16 p+24 Multiple of 4 Multiple of 8 Multiple of 8 Multiple of 8 internal fragmentation

Satisfying Alignment with Structures Within structure: Must satisfy each element’s alignment requirement Overall structure placement Each structure has alignment requirement K K = Largest alignment of any element Initial address of structure & structure length must be multiples of K Example: K = 8, due to double element struct S1 { char c; int i[2]; double v; } *p; c 3 bytes i[0] i[1] 4 bytes v p+0 p+4 p+8 p+16 p+24 Multiple of 4 Multiple of 8 Multiple of 8 Multiple of 8 internal fragmentation

Satisfying Alignment Requirements: Another Example For largest alignment requirement K Overall structure size must be multiple of K Compiler will add padding at end of structure to meet overall structure alignment requirement struct S2 { double v; int i[2]; char c; } *p; v i[0] i[1] c 7 bytes p+0 p+8 p+16 p+24 external fragmentation Multiple of K=8

Alignment of Structs Compiler: Maintains declared ordering of fields in struct Each field must be aligned within the struct (may insert padding) offsetof can be used to find the actual offset of a field Overall struct must be aligned according to largest field Total struct size must be multiple of its alignment (may insert padding) sizeof should be used to get true size of structs

Arrays of Structures Overall structure length multiple of K Create an array of ten S2 structs called “a” struct S2 { double v; int i[2]; char c; } a[10]; Overall structure length multiple of K Satisfy alignment requirement for every element in array a[0] a[1] a[2] • • • a+0 a+24 a+48 a+72 struct S2 { … } a[10]; - declares a new type “struct S2”, then declares an array a that contains 10 “struct S2” elements v i[0] i[1] c 7 bytes a+24 a+32 a+40 a+48 external fragmentation

Accessing Array Elements struct S3 { short i; float v; short j; } a[10]; Compute start of array element as: 12*index sizeof(S3) = 12, including alignment padding Element j is at offset 8 within structure Assembler gives offset a+8 a[0] • • • a[index] • • • a+0 a+12 a+12*index i 2 bytes v j a+12*index a+ 12*index +8 short get_j(int index) { return a[index].j; } # %rdi = index leaq (%rdi,%rdi,2),%rax # 3*index movzwl a+8(,%rax,4),%eax

How the Programmer Can Save Space Compiler must respect order elements are declared in Sometimes the programmer can save space by declaring large data types first struct S4 { char c; int i; char d; } *p; struct S5 { int i; char c; char d; } *p; c 3 bytes i d 3 bytes c i d 2 bytes 12 bytes 8 bytes

Unions Only allocates enough space for the largest element in union Can only use one member at a time union U { char c; int i[2]; double v; } *up; c i[0] i[1] v up+0 up+4 up+8 struct S { char c; int i[2]; double v; } *sp; c 3 bytes i[0] i[1] 4 bytes v sp+0 sp+4 sp+8 sp+16 sp+24

What Are Unions Good For? Unions allow the same region of memory to be referenced as different types Different “views” of the same memory location Can be used to circumvent C’s type system (bad idea and technically not guaranteed to work) Better idea: use a struct inside a union to access some memory location either as a whole or by its parts But watch out for endianness at a small scale… Layout details are implementation/machine-specific… union int_or_bytes { int i; struct bytes { char b0, b1, b2, b3; }

Summary Arrays in C Structures Unions Contiguous allocations of memory No bounds checking Can usually be treated like a pointer to first element Aligned to satisfy every element’s alignment requirement Structures Allocate bytes in order declared Pad in middle and at end to satisfy alignment Unions Provide different views of the same memory location