Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted.

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.
University of Washington Data Structures! Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structs  Alignment Unions 1.
Machine-Level Programming IV: Structured Data Apr. 23, 2008 Topics Arrays Structures Unions EECS213.
Structured Data I: Homogenous Data Sept. 17, 1998 Topics Arrays –Single –Nested Pointers –Multilevel Arrays Optimized Array Code class08.ppt “The.
Data Representation and Alignment Topics Simple static allocation and alignment of basic types and data structures Policies Mechanisms.
1 1 Machine-Level Programming IV: x86-64 Procedures, Data Andrew Case Slides adapted from Jinyang Li, Randy Bryant & Dave O’Hallaron.
1 1 Machine-Level Programming IV: x86-64 Procedures, Data.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics Sep. 15, 2015.
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.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
Machine-Level Programming IV: Structured Data Topics Arrays Structs Unions CS 105 “Tour of the Black Holes of Computing”
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data : Introduction.
Instructors: Greg Ganger, Greg Kesden, and Dave O’Hallaron
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming V: Unions and Memory layout.
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.
Fabián E. Bustamante, Spring 2007 Machine-Level Prog. IV - Structured Data Today Arrays Structures Unions Next time Arrays.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data MCS284: Computer.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
Lecture 9 Aggregate Data Topics Arrays Structs Unions again Lab 02 comments Vim, gedit, February 22, 2016 CSCE 212 Computer Architecture.
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 V: Unions and Memory layout
Machine-Level Programming IV: Structured Data
Machine-Level Programming IV: Data
Machine-Level Programming III: Procedures
Heterogeneous Data Structures & Alignment
Machine-Level Programming IV: x86-64 Procedures, Data
Machine-Level Programming IV: Data CSCE 312
Structs and Alignment CSE 351 Autumn 2016
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Arrays CSE 351 Winter
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Machine-Level Programming 6 Structured Data
Machine-Level Programming IV: Data
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Carnegie Mellon Wrap-up of Machine-Level Programming II: Control : Introduction to Computer Systems Sept. 18, 2018.
Instructor: Phil Gibbons
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
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 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.
Arrays CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants:
Machine-Level Programming 5 Structured Data
Machine-Level Programming 5 Structured Data
Machine-Level Representation of Programs (x86-64)
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.
Credits and Disclaimers
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
CS201- Lecture 7 IA32 Data Access and Operations Part II
Presentation transcript:

Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted from Bryant and O’Hallaron

Carnegie Mellon 2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structures  Allocation  Access  Alignment

Carnegie Mellon 3 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Array Allocation T A[ L ];  Contiguously allocated region of L * sizeof( T ) bytes in memory char str[12]; xx + 12 int val[5]; x x + 4x + 8x + 12x + 16x + 20 char *p[3]; x x + 8x + 16 x + 24 &str[0] &str[1] x+1 &val[0] &val[1] &val[2]&val[3]&val[4] &str[12] &val[5] &p[0] &p[1]&p[2]&p[3]

Carnegie Mellon 4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Array Accessing Example Register %rdi contains starting address of array Register %rsi contains array index Desired digit at %rdi + 4*%rsi Use memory reference (%rdi,%rsi,4) int get_digit(int *z, int ind) { return z[ind]; } # %rdi = z # %rsi = ind movl (%rdi,%rsi,4), %eax # z[ind] IA32 #define LEN 5 int arr[LEN];

Carnegie Mellon 5 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition # %rdi = z movl $0, %rax # i = 0 jmp.L3 # goto middle.L4: # loop: addl $1, (%rdi,%rax,4) # z[i]++ addq $1, %rax # i++.L3: # middle cmpq $4, %rax # i:4 jbe.L4 # if <=, goto loop rep; ret Array Loop Example void incr(int *z) { for (int i = 0; i < LEN; i++) z[i]++; }

Carnegie Mellon 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Multi-dimentional arrays “Row-Major” ordering of all elements in memory #define ROW 4 #define COL 5 int A[ROW][COL] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }}; xx+20x+40x+60x x+(i*COL+j)*sizeof(int) &A[2][0]

Carnegie Mellon 7 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2D Array Element Access int A[4][5] int get_digit(int i, int j) { return A[i][j]; } leaq(%rdi,%rdi,4), %rax # 5*i addl%rax, %rsi # 5*i+j movl0x890d0d(,%rsi,4), %eax# Memory[A + 4*(5*i+j)]

Carnegie Mellon 8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Row vectors int *get_array(int i) { return A[i]; } # %rdi = i leaq (%rdi,%rdi,4),%rax # 5 * i leaq 0x80de(,%rax,4),%rax# A + (20 * i) A[1] A[0]: array of 5 ints A[2] A[3]

Carnegie Mellon 9 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Multi-Level Array Example int A[5] = { 1, 5, 2, 1, 3 }; int B[5] = { 0, 2, 1, 3, 9 }; int C[5] = { 9, 4, 7, 2, 0 }; int *aofa[3] = {B, A, C}; aofa A B C

Carnegie Mellon 10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Element Access in Multi-Level Array salq $2, %rsi # 4*j addq 0x8eaf(,%rdi,8), %rsi # p = a2a[i] + 4*j movl (%rsi), %eax # return *p ret int *aofa[3] = {A, B, C}; int get_digit(long i, long j) { return a2a[i][j]; } Mem[A+4*(5*i+j)] Mem[Mem[aofa+8*i]+4*j] How does this differ from accessing a 2D array?

Carnegie Mellon 11 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today Arrays Structures  Allocation  Access  Alignment

Carnegie Mellon 12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Structure Representation Fields ordered according to declaration Compiler determines overall size + positions of fields a r inext struct node { int a[4]; long i; struct node *next; };

Carnegie Mellon 13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition.L11: # loop: movslq 16(%rdi), %rax # i = M[r+16] movl %esi, (%rdi,%rax,4) # M[r+4*i] = val movq 24(%rdi), %rdi # r = M[r+24] testq %rdi, %rdi # Test r jne.L11 # if !=0 goto loop void set_val (struct node *r, int val) { while (r) { int i = r->i; r->a[i] = val; r = r->next; } Following Linked List RegisterValue %rdir %rsival r inext a

Carnegie Mellon 14 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Structures & Alignment Unaligned Data Aligned Data  Primitive data type of x bytes  Address must be multiple of x  Alignment makes field access faster 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;

Carnegie Mellon 15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition struct S1 { char c; int i[2]; double v; } *p; struct S1 { char c; int i[2]; double v; } *p; Satisfying Alignment with Structures Within structure:  Must satisfy each element’s alignment requirement Initial address must also be aligned  K = Largest alignment of any element  Initial address & structure length must be multiples of K Example:  K = 8, due to double element ci[0]i[1]v 3 bytes4 bytes p+0p+4p+8p+16p+24 Multiple of 4Multiple of 8

Carnegie Mellon 16 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Saving Space Put large data types first Effect (K=4) struct S4 { char c; int i; char d; } *p; struct S4 { char c; int i; char d; } *p; struct S5 { int i; char c; char d; } *p; struct S5 { int i; char c; char d; } *p; ci 3 bytes d cid 2 bytes

Carnegie Mellon 17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Summary Arrays  Elements packed into contiguous region of memory  Use index arithmetic to locate individual elements Structures  Elements packed into single region of memory  Access using offsets determined by compiler  Possible require internal and external padding to ensure alignment

Carnegie Mellon 18 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #1 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An CmpBadSizeCmpBadSize int A1[3] int *A2

Carnegie Mellon 19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #1 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An CmpBadSizeCmpBadSize int A1[3] YN12YN4 int *A2 YN8YY4 A1 A2 Allocated int Unallocated pointer Allocated pointer Unallocated int

Carnegie Mellon 20 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #2 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An**An CmpBadSizeCmpBadSizeCmpBadSize int A1[3] int *A2[3] int (*A3)[3]

Carnegie Mellon 21 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #2 Decl AnAn*An**An CmpBadSizeCmpBadSizeCmpBadSize int A1[3] YN12YN4N-- int *A2[3] YN24YN8YY4 int (*A3)[3] YN8YY12YY4 A1 A2 Allocated int Unallocated pointer Allocated pointer Unallocated int A3

Carnegie Mellon 22 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #3 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An**An Cm p BadSizeCm p BadSizeCm p BadSize int A1[3][5] int *A2[3][5] int (*A3)[3][5] int (*A4[3])[5] Decl ***An Cm p BadSize int A1[3][5] int *A2[3][5] int (*A3)[3][5] int (*A4[3])[5]

Carnegie Mellon 23 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Declaration int A1[3][5] int *A2[3][5] int (*A3)[3][5] int (*A4[3])[5] A2 A4 Allocated int Unallocated pointer Allocated pointer Unallocated int Allocated pointer to unallocated int A1 A3

Carnegie Mellon 24 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #3 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An**An Cm p BadSizeCm p BadSizeCm p BadSize int A1[3][5] YN60YN20YN4 int *A2[3][5] YN120YN40YN8 int (*A3)[3][5] YN8YY60YY20 int (*A4[3])[5] YN24YN8YY20 Decl ***An Cm p BadSize int A1[3][5] N-- int *A2[3][5] YY4 int (*A3)[3][5] YY4 int (*A4[3])[5] YY4