Ithaca College MachineLevel Programming VIII Data Comp 21000

  • Slides: 9
Download presentation
Ithaca College Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly

Ithaca College 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, 3 rd ed. ”, Randy Bryant & David O’Hallaron, 2015 1

Ithaca College Today ¢ Arrays § § ¢ One-dimensional Multi-dimensional (nested) Multi-level Matricies Structures

Ithaca College Today ¢ Arrays § § ¢ One-dimensional Multi-dimensional (nested) Multi-level Matricies Structures 2

Ithaca College N X N Matrix Code ¢ Fixed dimensions § Know value of

Ithaca College N X N Matrix Code ¢ 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 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]; } #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]; } 3

Ithaca College 16 X 16 Matrix Access ¢ Array Elements § Address A +

Ithaca College 16 X 16 Matrix Access ¢ Array Elements § Address A + i * (C * K) + j * K § C = 16, K = 4 #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 4

Ithaca College n X n Matrix Access ¢ Array Elements § Address A +

Ithaca College 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 5

Ithaca College Optimizing Fixed Array Access a j-th column #define N 16 typedef int

Ithaca College Optimizing Fixed Array Access a j-th column #define N 16 typedef int fix_matrix[N][N]; ¢ Computation § Step through all elements in column j ¢ Optimization § Retrieving successive elements from single column /* 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]; } 6

Ithaca College Optimizing Fixed Array Access ¢ Optimization § Compute ajp = &a[i][j] Initially

Ithaca College Optimizing Fixed Array Access ¢ Optimization § Compute ajp = &a[i][j] Initially = a + 4*j § Increment by 4*N § Register Value %ecx ajp %ebx dest %edx i /* 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]; } 0 1 2 j 4 5 … 0 1 Address of A[0][j] (initial ajp) Address of A[1][j] is initial ajp + size of row (4*16=64) 2 3 4 5 … 7

Ithaca College Optimizing Fixed Array Access ¢ Optimization § Compute ajp = &a[i][j] Initially

Ithaca College Optimizing Fixed Array Access ¢ Optimization § Compute ajp = &a[i][j] Initially = a + 4*j § Increment by 4*N § Register Value %rcx ajp %rbx dest %rdx i . L 8: movl addl cmpl jne /* 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]; } # loop: (%rcx), %rax # Read *(ajp) %rax, (%rbx, %rdx, 4) # Save in dest[i] $1, %rdx # i++ $64, %rcx # ajp += 4*N $16, %rdx # i: N. L 8 # if !=, goto loop 8

Ithaca College Optimizing Variable Array Access § Compute ajp = &a[i][j] Initially = a

Ithaca College Optimizing Variable Array Access § Compute ajp = &a[i][j] Initially = a + 4*j § Increment by 4*n § Register Value %rcx ajp %rdi dest %rdx i %rbx 4*n %rsi n . L 18: movl addl cmpl jg /* 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]; } # loop: (%rcx), %rax # Read *ajp %rax, (%rdi, %rdx, 4) # Save in dest[i] $1, %rdx # i++ $rbx, %rcx # ajp += 4*n $rdx, %rsi # n: i. L 18 # if >, goto loop 9