University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Advertisements

Machine-Level Programming IV: Structured Data Feb 5, 2004 Topics Arrays Structs Unions class08.ppt “The course that gives CMU its Zip!”
University of Washington Data Structures! Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structs  Alignment Unions 1.
Structured Data: Sept. 20, 2001 Topics Arrays Structs Unions class08.ppt “The course that gives CMU its Zip!”
Machine-Level Programming IV: Data Sept. 19, 2007 Structured Data Arrays Structs UnionsData/Control Buffer overflow class07.ppt F’07.
Machine-Level Programming IV: Structured Data Apr. 23, 2008 Topics Arrays Structures Unions EECS213.
Machine-Level Programming IV: Structured Data Topics Arrays Structures Unions CS213.
Structured Data I: Homogenous Data Sept. 17, 1998 Topics Arrays –Single –Nested Pointers –Multilevel Arrays Optimized Array Code class08.ppt “The.
Structured Data II Heterogenous Data Sept. 22, 1998 Topics Structure Allocation Alignment Operating on Byte Strings Unions Byte Ordering Alpha Memory Organization.
Data Representation and Alignment Topics Simple static allocation and alignment of basic types and data structures Policies Mechanisms.
– 1 – Referencing Examples Code Does Not Do Any Bounds Checking! ReferenceAddressValueGuaranteed? mit[3]36 + 4* 3 = 483 Yes mit[5]36 + 4* 5 = 566 No mit[-1]36.
1 1 Machine-Level Programming IV: x86-64 Procedures, Data Andrew Case Slides adapted from Jinyang Li, Randy Bryant & Dave O’Hallaron.
Machine-Level Programming 6 Structured Data Topics Structs Unions.
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”
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data : Introduction.
University of Amsterdam Computer Systems – Data in C Arnoud Visser 1 Computer Systems New to C?
Machine-level Programming IV: Data Structures Topics –Arrays –Structs –Unions.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Topics: Arrays.
Fabián E. Bustamante, Spring 2007 Machine-Level Prog. IV - Structured Data Today Arrays Structures Unions Next time Arrays.
Machine-Level Programming IV: Structured Data
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data MCS284: Computer.
Assembly - Arrays תרגול 7 מערכים.
Machine-Level Programming IV: Structured Data Topics Arrays Structs Unions CS 105 “Tour of the Black Holes of Computing”
Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted.
1 Array. 2 Outline Aggregate scalar data into larger data Pointers vs. Array Array subscript and pointer arithmetic Memory layout for array –Static vs.
Lecture 9 Aggregate Data Topics Arrays Structs Unions again Lab 02 comments Vim, gedit, February 22, 2016 CSCE 212 Computer Architecture.
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data
Arrays CSE 351 Spring 2017 Instructor: Ruth Anderson
Machine-Level Programming IV: Structured Data
Machine-Level Programming IV: Data
Machine-Level Programming IV: Structured Data Sept. 30, 2008
Heterogeneous Data Structures & Alignment
Referencing Examples Code Does Not Do Any Bounds Checking!
Machine-Level Programming IV: x86-64 Procedures, Data
Machine-Level Programming 5 Structured Data
Machine-Level Programming IV: Data CSCE 312
Machine-level Programming IV: Data Structures
IA32 Stack Discipline From Last Time
Feb 2 Announcements Arrays/Structs in C Lab 2? HW2 released
IA32 Stack Discipline From Last Time
Machine-Level Programming IV: Data September 8, 2008
Machine-Level Programming 6 Structured Data
Machine-Level Programming IV: Structured Data Sept. 19, 2002
Machine-Level Programming IV: Data
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Instructor: Phil Gibbons
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming IV: Structured Data
Structured Data II Heterogenous Data Feb. 15, 2000
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
Machine Level Representation of Programs (III)
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming 5 Structured Data
Machine-Level Programming 5 Structured Data
Machine-Level Programming IV: Structured Data
Structured Data I: Homogenous Data Feb. 10, 2000
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: Structured Data
Machine-Level Programming IV: Data
Machine-Level Programming IV: Structured Data
Machine-Level Programming IV: Structured Data
Presentation transcript:

University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1

University of Washington Multi-Level Array Example 2 zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig uw = { 9, 8, 1, 9, 5 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; int *univ[3] = {uw, cmu, ucb}; Same thing as a 2D array? int univ2D[3] = { { 9, 8, 1, 9, 5 }, { 1, 5, 2, 1, 3 }, { 9, 4, 7, 2, 0 } };

University of Washington Multi-Level Array Example 3 Variable univ denotes array of 3 elements Each element is a pointer  4 bytes Each pointer points to array of int s zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig uw = { 9, 8, 1, 9, 5 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; #define UCOUNT 3 int *univ[UCOUNT] = {uw, cmu, ucb}; univ cmu uw ucb How do access an element?

University of Washington Element Access in Multi-Level Array 4 Computation (IA32)  Element access Mem[Mem[univ+4*index]+4*dig]  Must do two memory reads  First get pointer to row array  Then access element within array # %ecx = index # %eax = dig leal 0(,%ecx,4),%edx# 4*index movl univ(%edx),%edx# Mem[univ+4*index] movl (%edx,%eax,4),%eax# Mem[...+4*dig] int get_univ_digit (int index, int dig) { return univ[index][dig]; }

University of Washington Array Element Accesses 5 int get_sea_digit (int index, int dig) { return sea[index][dig]; } int get_univ_digit (int index, int dig) { return univ[index][dig]; } Nested array Multi-level array Access looks similar, but it isn’t: Mem[sea+20*index+4*dig] Mem[Mem[univ+4*index]+4*dig]

University of Washington Strange Referencing Examples ReferenceAddressValueGuaranteed? univ[2][3] univ[1][5] univ[2][-1] univ[3][-1] univ[1][12] univ cmu uw ucb

University of Washington Strange Referencing Examples ReferenceAddressValueGuaranteed? univ[2][3] univ[1][5] univ[2][-1] univ[3][-1] univ[1][12]  Code does not do any bounds checking  Location of each lower-level array in memory is not guaranteed univ cmu uw ucb *3 = 682 Yes 16+4*5 = 369 No 56+4*-1 = 525 No ???? No 16+4*12 = 647 No

University of Washington Using Nested Arrays 8 #define N 16 typedef int fix_matrix[N][N]; /* Compute element i,k of fixed matrix product */ int fix_prod_ele (fix_matrix a, fix_matrix b, int i, int k) { int j; int result = 0; for (j = 0; j < N; j++) result += a[i][j]*b[j][k]; return result; }

University of Washington Using Nested Arrays Strengths  Generates very efficient assembly code  Avoids multiply in index computation Limitation  Only works for fixed array size 9 #define N 16 typedef int fix_matrix[N][N]; /* Compute element i,k of fixed matrix product */ int fix_prod_ele (fix_matrix a, fix_matrix b, int i, int k) { int j; int result = 0; for (j = 0; j < N; j++) result += a[i][j]*b[j][k]; return result; } ab i-th row k-th column x

University of Washington Dynamic Nested Arrays Strength  Can create matrix of any size Programming  Must do index computation explicitly Performance  Accessing single element costly  Must do multiplication 10 int * new_var_matrix(int n) { return (int *) calloc(sizeof(int), n*n); } int var_ele (int *a, int i, int j, int n) { return a[i*n+j]; } movl 12(%ebp),%eax# i movl 8(%ebp),%edx# a imull 20(%ebp),%eax# n*i addl 16(%ebp),%eax# n*i+j movl (%edx,%eax,4),%eax# Mem[a+4*(i*n+j)]

University of Washington Arrays in C Contiguous allocations of memory No bounds checking Can usually be treated like a pointer to first element 11

University of Washington Data Structures in Assembly Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structs  Alignment Unions 12

University of Washington struct rec { int i; int a[3]; int *p; }; Structures 13

University of Washington struct rec { int i; int a[3]; int *p; }; Structures Characteristics  Contiguously-allocated region of memory  Refer to members within structure by names  Members may be of different types 14 Memory Layout iap

University of Washington struct rec { int i; int a[3]; int *p; }; Structures 15 Accessing Structure Member  Given an instance of the struct, we can use the. operator, just like Java:  struct rec r1; r1.i = val;  What if we have a pointer to a struct: struct rec *r = &r1;

University of Washington struct rec { int i; int a[3]; int *p; }; IA32 Assembly # %eax = val # %edx = r movl %eax,(%edx)# Mem[r] = val void set_i(struct rec *r, int val) { r->i = val; } Structures 16 Accessing Structure Member  Given an instance of the struct, we can use the. operator, just like Java:  struct rec r1; r1.i = val;  What if we have a pointer to a struct: struct rec *r = &r1;  Using * and. operators:  Or, use -> operator for short:  Pointer indicates first byte of structure; access members with offsets (*r).i = val; r->i = val;

University of Washington # %ecx = idx # %edx = r leal 0(,%ecx,4),%eax# 4*idx leal 4(%eax,%edx),%eax# r+4*idx+4 int *find_a (struct rec *r, int idx) { return &r->a[idx]; } Generating Pointer to Structure Member Generating Pointer to Array Element  Offset of each structure member determined at compile time 17 struct rec { int i; int a[3]; int *p; }; iap r+4+4*idxr

University of Washington Structures & Alignment Unaligned Data How would it look like if data items were aligned (address multiple of type size) ? ci[0]i[1]v pp+1p+5p+9p+17 struct S1 { char c; int i[2]; double v; } *p; struct S1 { char c; int i[2]; double v; } *p; 18

University of Washington Structures & Alignment Unaligned Data Aligned Data  Primitive data type requires K bytes  Address must be multiple of K ci[0]i[1]v 3 bytes4 bytes p+0p+4p+8p+16p+24 Multiple of 4Multiple of 8 ci[0]i[1]v pp+1p+5p+9p+17 struct S1 { char c; int i[2]; double v; } *p; struct S1 { char c; int i[2]; double v; } *p; 19

University of Washington Alignment Principles Aligned Data  Primitive data type requires K bytes  Address must be multiple of K Aligned data is required on some machines; it is advised on IA32  Treated differently by IA32 Linux, x86-64 Linux, and Windows! What is the motivation for alignment? 20

University of Washington Alignment Principles Aligned Data  Primitive data type requires K bytes  Address must be multiple of K Aligned data is required on some machines; it is advised on IA32  Treated differently by IA32 Linux, x86-64 Linux, and Windows! Motivation for Aligning Data  Physical memory is accessed by aligned chunks of 4 or 8 bytes (system- dependent)  Inefficient to load or store datum that spans quad word boundaries  Also, virtual memory is very tricky when datum spans two pages (later…) Compiler  Inserts padding in structure to ensure correct alignment of fields  sizeof() should be used to get true size of structs 21

University of Washington Specific Cases of Alignment (IA32) 1 byte: char, …  no restrictions on address 2 bytes: short, …  lowest 1 bit of address must be bytes: int, float, char *, …  lowest 2 bits of address must be bytes: double, …  Windows (and most other OSs & instruction sets): lowest 3 bits  Linux: lowest 2 bits of address must be 00 2  i.e., treated the same as a 4-byte primitive data type 12 bytes: long double  Windows, Linux: lowest 2 bits of address must be

University of Washington Saving Space 23 ci[0]i[1]v 3 bytes4 bytes p1+0p1+4p1+8p1+16p1+24 struct S1 { char c; int i[2]; double v; } *p1;

University of Washington Saving Space Put large data types first: Effect (example x86-64, both have K=8) 24 ci[0]i[1]v 3 bytes4 bytes p1+0p1+4p1+8p1+16p1+24 struct S1 { char c; int i[2]; double v; } *p1; struct S2 { double v; int i[2]; char c; } *p2; ci[0]i[1]v p2+0p2+8p2+16 Unfortunately, doesn’t satisfy requirement that struct’s total size is a multiple of K

University of Washington Arrays of Structures Satisfy alignment requirement for every element How would accessing an element work? 25 struct S2 { double v; int i[2]; char c; } a[10]; ci[0]i[1]v a+24a+32a+40 a[0] a+0 a[1]a[2] a+24a+48a+72 7 bytes a+48

University of Washington Unions Allocated according to largest element Can only use one member at a time union U1 { char c; int i[2]; double v; } *up; union U1 { char c; int i[2]; double v; } *up; struct S1 { char c; int i[2]; double v; } *sp; struct S1 { char c; int i[2]; double v; } *sp; c 3 bytes i[0]i[1] 4 bytes v sp+0sp+4sp+8sp+16sp+24 c i[0]i[1] v up+0up+4up+8 26

University of Washington 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) Better idea: use a struct inside a union to access some memory location either as a whole or by its parts 27

University of Washington Unions For Embedded Programming 28 typedef union { unsigned char byte; struct { unsigned char b0:1; unsigned char b1:1; unsigned char b2:1; unsigned char b3:1; unsigned char reserved:4; } bits; } hw_register; hw_register reg; reg.byte = 0x3F; // reg.bits.b2 = 0; // reg.bits.b3 = 0; // unsigned short a = reg.byte; printf("0x%X\n", a); // output: 0x33 (Note: the placement of these fields and other parts of this example are implementation- dependent)

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