Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.

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:
Machine-Level Programming IV: Structured Data Apr. 23, 2008 Topics Arrays Structures Unions EECS213.
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.
Machine-Level Programming IV: Structured Data Topics Arrays Structs Unions CS 105 “Tour of the 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.
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.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
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 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.
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.
Machine-Level Programming I: Basics
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 III: Procedures
Arrays.
Machine-Level Programming IV: x86-64 Procedures, Data
Machine-Level Programming 5 Structured Data
Machine-Level Programming IV: Data CSCE 312
Arrays.
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Arrays CSE 351 Winter
Feb 2 Announcements Arrays/Structs in C Lab 2? HW2 released
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
Code Optimization and Performance
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 V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
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:
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Programming 5 Structured Data
Machine-Level Programming 5 Structured Data
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Machine-Level Representation of Programs (x86-64)
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Program Optimization CSE 238/2038/2138: Systems Programming
Structured Data I: Homogenous Data Feb. 10, 2000
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Instructor: Fatma CORUT ERGİN
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
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:

Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides from the book “Computer Systems: a Programmer’s Perspective, 3rd ed.”, Randy Bryant & David O’Hallaron, 2015

Today Arrays Structures One-dimensional Multi-dimensional (nested) Multi-level Structures

N X N Matrix Code Fixed dimensions #define N 16 typedef int fix_matrix[N][N]; /* Get element a[i][j] */ int fix_ele (fix_matrix a, int i, int j) { return a[i][j]; } Fixed dimensions Know value of N at compile time Variable dimensions, explicit indexing Traditional way to implement dynamic arrays Variable dimensions, implicit indexing Now supported by gcc #define IDX(n, i, j) ((i)*(n)+(j)) /* Get element a[i][j] */ int vec_ele (int n, int *a, int i, int j) { return a[IDX(n,i,j)]; } /* Get element a[i][j] */ int var_ele (int n, int a[n][n], int i, int j) { return a[i][j]; }

16 X 16 Matrix Access Array Elements Address A + i * (C * K) + j * K #define N 16 typedef int fix_matrix[N][N]; /* Get element a[i][j] */ int fix_ele(fix_matrix a, int i, int j) { return a[i][j]; } # a in %rdi, i in %rsi, j in %rdx salq $6, %rsi # 64*i addq %rsi, %rdi # a + 64*i movl (%rdi,%rdx,4), %eax # M[a + 64*i + 4*j] ret

n X n Matrix Access Array Elements Address A + i * (C * K) + j * K C = n, K = 4 /* Get element a[i][j] */ int var_ele(int n, int a[n][n], int i, int j) { return a[i][j]; } # n in %rdi, a in %rsi, i in %rdx, j in %rcx imulq %rdx, %rdi # n*i leaq (%rsi,%rdi,4), %rax # a + 4*n*i movl (%rax,%rcx,4), %eax # a + 4*n*i + 4*j ret

Optimizing Fixed Array Access j-th column #define N 16 typedef int fix_matrix[N][N]; /* Retrieve column j from array */ void fix_column (fix_matrix a, int j, int *dest) { int i; for (i = 0; i < N; i++) dest[i] = a[i][j]; } Computation Step through all elements in column j Optimization Retrieving successive elements from single column

Optimizing Fixed Array Access Optimization Compute ajp = &a[i][j] Initially = a + 4*j Increment by 4*N /* Retrieve column j from array */ void fix_column (fix_matrix a, int j, int *dest) { int i; for (i = 0; i < N; i++) dest[i] = a[i][j]; } Register Value %ecx ajp %ebx dest %edx i 1 2 j 4 5 … 3 Address of A[0][j] (initial ajp) Address of A[1][j] is initial ajp + size of row (4*16=64)

Optimizing Fixed Array Access Optimization Compute ajp = &a[i][j] Initially = a + 4*j Increment by 4*N /* Retrieve column j from array */ void fix_column (fix_matrix a, int j, int *dest) { int i; for (i = 0; i < N; i++) dest[i] = a[i][j]; } Register Value %rcx ajp %rbx dest %rdx i .L8: # loop: movl (%rcx), %rax # Read *(ajp) movl %rax, (%rbx,%rdx,4) # Save in dest[i] addl $1, %rdx # i++ addl $64, %rcx # ajp += 4*N cmpl $16, %rdx # i:N jne .L8 # if !=, goto loop

Optimizing Variable Array Access Compute ajp = &a[i][j] Initially = a + 4*j Increment by 4*n /* Retrieve column j from array */ void var_column (int n, int a[n][n], int j, int *dest) { int i; for (i = 0; i < n; i++) dest[i] = a[i][j]; } Register Value %rcx ajp %rdi dest %rdx i %rbx 4*n %rsi n .L18: # loop: movl (%rcx), %rax # Read *ajp movl %rax, (%rdi,%rdx,4) # Save in dest[i] addl $1, %rdx # i++ addl $rbx, %rcx # ajp += 4*n cmpl $rdx, %rsi # n:i jg .L18 # if >, goto loop

Today Procedures (x86-64) Arrays Structures One-dimensional Multi-dimensional (nested) Multi-level Structures Allocation Access

Structure Allocation Memory Layout Concept struct rec { int a[4]; int i; struct rec *next; }; Memory Layout a i next 16 24 32 Concept Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Fields ordered according to declaration 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

Structure Access r r+16 Accessing Structure Member X86-64 Assembly a i struct rec { int a[4]; int i; struct rec *next; }; a i next 16 24 32 Accessing Structure Member Pointer indicates first byte of structure Access elements with offsets void set_i(struct rec *r, int val) { r->i = val; } X86-64 Assembly # %rdx = val # %rax = r movq %rdx, 16(%rax) # Mem[r+16] = val

Generating Pointer to Structure Member r+4*idx struct rec { int a[4]; int i; struct rec *n; }; a i next 16 24 32 Generating Pointer to Array Element Offset of each structure member determined at compile time Arguments Mem[%ebp+8]: r Mem[%ebp+12]: idx Compute as r + 4*idx int *get_ap (struct rec *r, int idx) { return &r->a[idx]; } # r in %rdi, idx in %rsi leaq (%rdi,%rsi,4), %rax ret

Following Linked List C Code Element i a i next 16 24 32 struct rec { int a[4]; int i; struct rec *next; }; Following Linked List C Code void set_val (struct rec *r, int val) { while (r) { int i = r->i; r->a[i] = val; r = r->next; } a i next 16 24 32 Element i Register Value %rdi r %rsi val .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

Summary Arrays Structures One-dimensional Multi-dimensional (nested) Multi-level Structures Allocation Access