arrays - upload.wikimedia.org · 5/27/2020  · arrays youngw.lim 2020-05-27wed youngw.lim arrays...

37
Arrays Young W. Lim 2020-05-27 Wed Young W. Lim Arrays 2020-05-27 Wed 1 / 37

Upload: others

Post on 30-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Arrays

Young W. Lim

2020-05-27 Wed

Young W. Lim Arrays 2020-05-27 Wed 1 / 37

Page 2: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Outline

1 AccessBased on

2 Array BackgroundArraysPointersLoopsMulti-dimensional arraysFixed size arraysDynamically allocated arrays

Young W. Lim Arrays 2020-05-27 Wed 2 / 37

Page 3: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Based on

1 "Self-service Linux: Mastering the Art of Problem Determination",

Mark Wilding

1 "Computer Architecture: A Programmer’s Perspective", Bryant &O’Hallaron

I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted tocopy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts,and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.

CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short:you are free to share and make derivative works of the file under the conditions that you appropriately attribute it,and that you distribute it only under a license compatible with this one.

Young W. Lim Arrays 2020-05-27 Wed 3 / 37

Page 4: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Compling 32-bit program on 64-bit gcc

gcc -v

gcc -m32 t.c

sudo apt-get install gcc-multilib

sudo apt-get install g++-multilib

gcc-multilib

g++-multilib

gcc -m32

objdump -m i386

Young W. Lim Arrays 2020-05-27 Wed 4 / 37

Page 5: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Array Declaration

T A[N]

allocation of contiguous region of NL bytes

L: the byte size of the data type TxA: the starting address of the region

introduces an identifier A

A can be used as a pointer to the beginning of the arraythe value of this pointer is xAA[i] : the i-th element is at xA + L · ii : index between 0 and N − 1

Young W. Lim Arrays 2020-05-27 Wed 5 / 37

Page 6: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Array Delaration Examples

x + L · i L N LN

char A[12]; xA + 1 · i 1 12 12char * B[8]; xB + 4 · i 4 8 32double C[6]; xC + 8 · i 8 6 48double * D[5]; xD + 4 · i 4 5 20

Young W. Lim Arrays 2020-05-27 Wed 6 / 37

Page 7: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Accessing an array element

int E[12];E[i] access

xE + 4i%edx : the starting address xE of E%ecx : the index value iE[i] → %eaxmovl (%edx,%ecx,4), %eaxmove data at (%edx + 4 * %ecx) to %eax

Young W. Lim Arrays 2020-05-27 Wed 7 / 37

Page 8: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Pointer Arithmetic (1)

leal to generate an addressmovl to reference memory

Young W. Lim Arrays 2020-05-27 Wed 8 / 37

Page 9: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Pointer Arithmetic (2)

movl %edx, %eax (int *) E xEmovl %(%edx),%eax (int) E[0] M[xE ]movl (%edx,%ecx,4), %eax (int) E[i] M[xE + 4i ]leal 8(%edx),%eax (int *) &E[2] xE + 8leal -4(%edx,%ecx,4),%eax (int *) E+i-1 xE + 4i − 4movl(%edx,%ecx,8),%eax (int) *(&E[i]+i) xE + 8imovl %ecx,%eax (int) &E[i]-E i

Young W. Lim Arrays 2020-05-27 Wed 9 / 37

Page 10: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Pointer Arithmetic (3)

p : a pointer to data of type T

xp : the value of pxp + L · i : the value p + i

L : the size of data type T

Young W. Lim Arrays 2020-05-27 Wed 10 / 37

Page 11: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Pointer Arithmetic (4)

leal instruction is used to generate an addressmovl instruction is used to reference memoryexcept in some cases, where it copies an addressto compute the offset of the desired element ofa multi-dimensional arraya movl instruction is usedwith the start of the array as the base addressand the possibly scaled offset as an index

Young W. Lim Arrays 2020-05-27 Wed 11 / 37

Page 12: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Array and Loop (1) decimal5 and decimal5_opt

int decimal5(int* x) { int decimal5_opt(int *x) {int i; int val = 0;int val=0; int *xend = x + 4;

for (i=0; i<5; ++i) do {val = (10*val) + x[i]; val = (10*val) + *x;

x++;return(val); } while (x <= xend);

}return val;

}

Young W. Lim Arrays 2020-05-27 Wed 12 / 37

Page 13: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Array and Loop (2) decimal5_opt assembly

movl 8(%ebp), %ecx int decimal5_opt(int *x) {xorl %eax, %eax int val = 0;leal 16(%ecx), %ebx int *xend = x + 4;

.L12:leal (%eax,%eax,4), %edx do {movl (%ecx), %eax val = (10*val) + *x;leal (%eax,%edx,2), %eax x++;addl $4, %ecx } while (x <= xend);cmpl %ebx, %ecxjbe .L12 return val;

}

Young W. Lim Arrays 2020-05-27 Wed 13 / 37

Page 14: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Array and Loop (3) assembly with low level comments

movl 8(%ebp), %ecx ; M[%ebp+8] -> %ecxxorl %eax, %eax ; %eax ^ %eax -> %eax

; 0 -> valleal 16(%ecx), %ebx ; (%ecx+16) -> %ebx

; x + 4 -> xend.L12: ; loop:

leal (%eax,%eax,4), %edx ; (%eax + %eax*4) -> %edx; 5*val

movl (%ecx), %eax ; M[%ecx] -> %eax; *x

leal (%eax,%edx,2), %eax ; (%eax + %edx*2) -> %eax; *x + 10*val -> val

addl $4, %ecx ; 4 + %ecx -> %ecx; x++

cmpl %ebx, %ecx ; compare x :xendjbe .L12 ; if <=, goto loop

Young W. Lim Arrays 2020-05-27 Wed 14 / 37

Page 15: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Array and Loop (4) assembly with high level comments

movl 8(%ebp), %ecx ; get base address of array xxorl %eax, %eax ; val = 0leal 16(%ecx), %ebx ; xend = x+4 (16 bytes = 4 dwords)

.L12: ; loop:leal (%eax,%eax,4), %edx ; compute 5 *valmovl (%ec), %eax ; compute *xleal (%eax,%edx,2), %eax ; compute *x + 2 * (5 * val)addl $4, %ecx ; x++cmpl %ebx, %ecx ; compare x :xendjbe .L12 ; if <=, goto loop

Young W. Lim Arrays 2020-05-27 Wed 15 / 37

Page 16: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Nested Array (1)

int A[4][3]

typedef int Row[3]; // Row type definitionRow A[4]; // A is an array of Row type

data type Row is defined to be an array of three integersarray A contains four such arrayseach requiring 12 bytes to store the three integersthe total array size is then 4*4*3 = 48 bytes

Young W. Lim Arrays 2020-05-27 Wed 16 / 37

Page 17: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Nested Array (2)

Array A can also be viewed as a 2-dimensional arraywith 4 rows and 3 columns, referenced as A[0][0] trought A[3][2]The array elements are ordered in memory in row major orderall elements of row 0 followed by all elements row 1, and so on

viewing A as an array of 4 elments,each of which is an array of 3 int’sfirst A[0] (row 0), followed by A[1] (row 1), and so on

Young W. Lim Arrays 2020-05-27 Wed 17 / 37

Page 18: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Nested Array (3)

A[0][0] xA + (0 ∗ 3+ 0) ∗ 4 row 0 col 0A[0][1] xA + (0 ∗ 3+ 1) ∗ 4 col 1A[0][2] xA + (0 ∗ 3+ 2) ∗ 4 col 2A[1][0] xA + (1 ∗ 3+ 0) ∗ 4 row 1 col 0A[1][1] xA + (1 ∗ 3+ 1) ∗ 4 col 1A[1][2] xA + (1 ∗ 3+ 2) ∗ 4 col 2A[2][0] xA + (2 ∗ 3+ 0) ∗ 4 row 2 col 0A[2][1] xA + (2 ∗ 3+ 1) ∗ 4 col 1A[2][2] xA + (2 ∗ 3+ 2) ∗ 4 col 2A[3][0] xA + (3 ∗ 3+ 0) ∗ 4 row 3 col 0A[3][1] xA + (3 ∗ 3+ 1) ∗ 4 col 1A[3][2] xA + (3 ∗ 3+ 2) ∗ 4 col 2

Young W. Lim Arrays 2020-05-27 Wed 18 / 37

Page 19: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

2-dimensional Arrays

to access elements of multi-dimensional arrays,the compiler generates code to computethe offset of the desired elementthen usesa movl instructionusing the start of the array as the base addressand the (possibly scaled) offset as an index

T D[R][C];array element D[i][j] is at memory addressxD + L(Ci + j)

Young W. Lim Arrays 2020-05-27 Wed 19 / 37

Page 20: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

2-dimensional Array Examples

int A[4][3]

%eax contains xA%edx holds i%ecx holds jcopy A[i][j] to %eax

sall $2, %ecx ; %ecx*4 ; j*4 -> %ecxleal (%edx, %edx, 2), %edx ; %edx + %edx*2 ; i*3 -> %edxleal (%ecx, %edx, 4), %edx ; %ecx + %edx*4 ; j*4 + i*12n -> %edxmovl (%eax, %edx), %eax ; %eax + %edx ; M[xA + 4(3*i + j)] -> %eax

Young W. Lim Arrays 2020-05-27 Wed 20 / 37

Page 21: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Fixed size arrays (1)

the c compiler is able to make many optimizationsfor code operating on multi-dimensional arrays of fixed sizethe loop will access the elements of array AA[i][0], A[i][1], . . . , A[i][15] in sequencethese elements occupy adjacent locations in memoryuse a pointer Aptr to access the successive locations

Young W. Lim Arrays 2020-05-27 Wed 21 / 37

Page 22: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Fixed size arrays (2)

the loop will access the elements of array BB[0][k], B[1][k], . . . , B[15][k] in sequencethese elements occupy 64-byte bytes apart locations in memoryuse a pointer Bptr to access these successive locationsin c, this pointer is shown as being incremented by 16,althogh in fact the actual pointer is incremented by 4*16=64 bytes

Young W. Lim Arrays 2020-05-27 Wed 22 / 37

Page 23: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Fixed Size Array (1) fprod and fprod_opt

#define N 16 int fprod_opt(fmatrix A,typedef int fmatrix[N][N]; fmatrix B, int i, int k) {

int *Ap = &A[i][0];int fprod(fmatrix A, int *Bp = &B[0][k];fmatrix B, int i, int k) int cnt = N - 1;{ int result = 0;

int j;int result = 0; do {

result += (*Ap) * (*Bp);for (j=0; j<N; j++) Ap += 1;

result += Bp += N;A[i][j] * B[j][k]; cnt--;

} while (cnt >= 0);return result; return result;

} }

Young W. Lim Arrays 2020-05-27 Wed 23 / 37

Page 24: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Fixed Size Array (2) assembly for fprod

.L23: int fprod_opt(fmatrix A,movl (%edx), %eax fmatrix B, int i, int k) {imull (%ecx), %eax int *Ap = &A[i][0];addl %eax, %esi int *Bp = &B[0][k];addl $64, %ecx int result = 0;addl $4, %edxdecl %ebx do {jns .L23 result += (*Ap) * (*Bp);

Ap += 1; // 4 bytes strideBp += N; // 4*16 = 64 bytes stridecnt--;

} while (cnt >= 0);return result;

}

Young W. Lim Arrays 2020-05-27 Wed 24 / 37

Page 25: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Fixed Size Array (3) assembly with low level comments

.L23:movl (%edx), %eax ; M[%edx] -> %eaximull (%ecx), %eax ; M[%ecx] * %eax -> %eaxaddl %eax, %esi ; %eax + %esi -> %esiaddl $64, %ecx ; 64 + %ecx -> %ecxaddl $4, %edx ; 4 + %edx -> %edxdecl %ebx ; %ebx -1 -> %ebxjns .L23

Young W. Lim Arrays 2020-05-27 Wed 25 / 37

Page 26: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Dynamically allocated Arrays (1)

multi-dimensional arrayswhen the sizes are known at the compile timeexcept the size of the first dimensionin many applications, a code is required to workfor arbitrary size arrays that have beendynamically allocatedfor these we must explicitly enocdethe mapping of multi-dimensional arraysinto one-dimensional ones

Young W. Lim Arrays 2020-05-27 Wed 26 / 37

Page 27: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Dynamically allocated Arrays (2)

we can define a data type var_matrix as simply as int *typedef int *var_matrix;

to allocate and initialize storagefor an n × n array of integers,calloc library function can be usedvar_matrix new_var_matrix(int n){

return (var_matrix) calloc(sizeof(int), n*n);}

Young W. Lim Arrays 2020-05-27 Wed 27 / 37

Page 28: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Dynamically allocated Arrays (3)

calloc library function has two arguments

the size of each array elementthe number of array elements required

attempts to allocate space for the entire array

if successful,it initializes the entire region of memory to 0sif sufficient space is not available,it returns null

Young W. Lim Arrays 2020-05-27 Wed 28 / 37

Page 29: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

row-major order index computation examples

int var_ele (var_matrix A, int i, int j, int n){

return A[(i*n)+j);}

movl 8(%ebp), %edx ; Get Amovl 12(%ebp), %eax ; Get iimull 20(%ebp), %eax ; Compute n*iaddl 16(%ebp), %eax ; Compute n*i + jmovl (%edx, %eax, 4), %eax ; Get A[i*n + j]

Young W. Lim Arrays 2020-05-27 Wed 29 / 37

Page 30: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Comparing index computations (1)

dynamic version is somewhat more complexmust use a multiply instruction to scale i by nrather than a series of shifts and addsthis multiplication is not significant performance penaltyin many cases, the compiler can simplifythe index computations for variable sized arraysusing the same principles as the fixed array case

Young W. Lim Arrays 2020-05-27 Wed 30 / 37

Page 31: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Comparing index computations (2)

rather than generating a pointer variable Bptrthe compiler creates an integer variable nTjPkfor n Times j Plus ksince its value n*j+k relative to the original codeinitially, nTjPk equals k, andit is incremented by n by each iteration

Young W. Lim Arrays 2020-05-27 Wed 31 / 37

Page 32: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Index computations for dynamically allocated arrays

in many cases, the compiler can simplifythe index computation for variable sized arraysusing the same principles as for fixed-size arraysthe compile is able to eliminate the integer multiplicationi ∗ n and j ∗ n by exploiting the sequential access patterresulting from the loop structures

Young W. Lim Arrays 2020-05-27 Wed 32 / 37

Page 33: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Register Spilling (1)

variables B and n must be retrievd from memoryon each iterationregister spillingthe compiler chose to spill variables B and nbecause they are read onlythey do not change value within the loopSpilling is a common problem for IA32,since the processor has so few registers

Young W. Lim Arrays 2020-05-27 Wed 33 / 37

Page 34: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Register Spilling (2)

not enough registers to hold all the neededtemporary datamust keep some local variables in memoryregister spilling

Young W. Lim Arrays 2020-05-27 Wed 34 / 37

Page 35: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Dynamically Allocated Array (1)

typedef int *vmatrix;. . .vmatrix A // int *A;. . .vmatrix func(int n) {

return (vmatrix)calloc(

sizeof(int), n*n);}. . .int foo(vmatrix A, int i,int j, int n) {

return A[(i*n) + j);}

movl 8(%ebp), %edxmovl 12(%ebp), %eaximull 20(%ebp), %eaxaddl 16(%ebp), %eaxmovl (%eax, %eax, 4), %eax

Young W. Lim Arrays 2020-05-27 Wed 35 / 37

Page 36: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Dynamically Allocated Array (2)

typedef int *vmatrix;

int vprod(vmatrix A,vmatrix B, int i,int k, int n) {

int j;int result = 0;

for (j=0; j<n; j++) {result +=

A[i*n+j] * B[j*n+k];

return result;}

int vprod(vmatrix A,vmatrix B, int i,int k, int n) {

int *Ap = &A[i*n];int nT = n, result = 0;

if (n <= 0) return result;do {

result += (*Ap) * B[nT];Ap++;nT+= n;cnt--;

} while (cnt);return result;

}

Young W. Lim Arrays 2020-05-27 Wed 36 / 37

Page 37: Arrays - upload.wikimedia.org · 5/27/2020  · Arrays YoungW.Lim 2020-05-27Wed YoungW.Lim Arrays 2020-05-27Wed 1/37

Dynamically Allocated Array (3)

.L37:movl 12(%ebp), %eaxmovl (%ebx), %ediaddl $4, %ebximull (%eax,%ecx,4), %ediaddl %edi, %esiaddl 24(%ebp), %ecxdecl %edxjnz .L37

int vprod(vmatrix A,vmatrix B, int i,int k, int n) {

int *Ap = &A[i*n];int nT = n, result = 0;

if (n <= 0) return result;do {

result += (*Ap) * B[nT];Ap++;nT+= n;cnt--;

} while (cnt);return result;

}

Young W. Lim Arrays 2020-05-27 Wed 37 / 37