Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

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

3 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]; }

4 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

5 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

6 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

7 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)

8 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

9 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

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

11 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

12 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

13 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

14 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 .L # if !=0 goto loop

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


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

Similar presentations


Ads by Google