Download presentation
Presentation is loading. Please wait.
Published byJuniper Hoover Modified over 8 years ago
1
1 Array
2
2 Outline Aggregate scalar data into larger data Pointers vs. Array Array subscript and pointer arithmetic Memory layout for array –Static vs. dynamic Compiler optimization for array operations Suggested reading –3.8
3
3 Array declaration T A[N] ; Allocate a contiguous region in memory –The size of the region is sizeof(T) * N bytes
4
4 Starting Address of an Array The starting address of an array A –is denoted as X A Identifier A can be used as –A pointer to the beginning of the array –Its value is X A
5
5 Accessing Array Array elements can be accessed –Using an integer index ranging between 0 and N-1 Array element i is stored at address –X A + sizeof(T) * i
6
6 Array 01234567891011 0481216 char a[12] ; xaxa x a +4x a +8 char *b[5] ; xbxb x b +4x b +8x b +12x b +16
7
7 Array 0481216 double c[2] ; xcxc x c +8 double *d[5] ; xdxd x d +4x d +8x d +12x d +16
8
8 Pointer Arithmetic Addition and subtraction –p+i, p-i (result is a pointer) –p-q (result is an int) Referencing & dereferencing –*p, &E Subscription –A[i], *(A+i)
9
9 Memory referencing instruction E is an array of int’s –Address of E is stored in register %edx –Index i is stored in register %ecx The array element E[i] is translated into – movl(%edx, %ecx, 4), %eax
10
10 Pointer Arithmetic ExpressionTypeValueAssembly code E int * xE xE movl %edx, %eax E[0] intM[x E ]movl (%edx), %eax E[i] intM[x E +4i]movl (%edx, %ecx, 4), %eax &E[2] int *x E +8leal 8(%edx,) %eax E+i-1 int *x E +4i-4lea -4(%edx, %ecx, 4), %eax *(&E[i]+i) intM[x E +4i+4i]movl (%edx, %ecx, 8), %eax &E[i]-E intimovl %ecx, %eax
11
11 Example 1 int decimal5(int *x) 2 { 3 int i; 4 int val = 0; 5 6 for (i = 0; i < 5; i++) 7 val = (10 * val) + x[i]; 8 9 return val; 10 }
12
12 Example xorl%edx, %edx# i = 0 xorl%eax, %eax# val = 0 movl8(%ebp), %ecx# get x L2: leal(%eax,%eax,4), %eax addl%eax, %eax# val *= 10 addl(%ecx,%edx,4), %eax# val += x[i] addl$1, %edx# i++ cmpl$5, %edx# i ? 5 jneL2
13
13 Nested Array int A[4][3] ; –Array A is a two-dimensional array with four rows and three columns –It is referenced as A[0][0] through A[3][2]
14
14 Nested Array int A[4][3] ; Array of array –typedef int row3_t[3] ; – row3_t A[4] ; –Array A contains 4 elements, each requiring 12 bytes to store 3 integers –The whole size of array A is 48 bytes Row major ordered in memory
15
15 Nested Array RowElementAddress A[0]A[0][0]xAxA A[0][1]x A +4 A[0][2]x A +8 A[1]A[1][0]x A +12 A[1][1]x A +16 A[1][2]x A +20 A[2]A[2][0]x A +24 A[2][1]x A +28 A[2][2]x A +32 A[3]A[3][0]x A +36 A[3][1]x A +40 A[3][2]x A +44
16
16 Nested Array T D[R][C] ; D[i][j] is at memory address –x D + L* ( C * i + j ) –L is sizeof(T)
17
17 Access A[i,j] It is in memory M [ xA +j*4+ i * 12 ] %eax contains x A %edx holds i, %ecx holds j sall $2, %ecx j*4 leal (%edx, %edx, 2), %edx i*3 leal (%ecx, %edx, 4), %ecx j*4+ i*12 movl (%eax, %ecx), %eax
18
18 Fixed-size Arrays 1 #define N 16 2 typedef int fix_matrix[N][N]; 3 4 /* Compute i,k of fixed matrix product */ 5 int fix_prod_ele (fix_matrix A, fix_matrix B, int i, int k) 6 { 7 int j; 8 int result = 0; 9 10 for (j = 0; j < N; j++) 11 result += A[i][j] * B[j][k]; 12 13 return result; 14 }
19
19 Observations The loop will access the elements of array A –as A[i][0], A[i][1], …, A[i][15] in sequence These elements occupy adjacent positions in memory –starting with the address of array A[i][0] Use a pointer variable Aptr –to access these successive locations.
20
20 Observations The loop will access the elements of array B –as B[0][k], B[1][k], …, B[15][k] in sequence These elements occupy positions in memory –starting with the address of array B[0][i] –and space 64 bytes apart. Use a pointer variable Bptr –to access these locations. Use a simple counter –to keep track of the number of iterations required
21
21 Fixed-size Arrays 1 /* Compute i,k of fixed matrix product */ 2 int fix_prod_ele_opt (fix_matrix A, fix_matrix B, int i, int k) 3 { 4 int *Arow=&A[i][0], *Bptr = &B[0][k]; 5 int j, result = 0; 6 7 For ( cnt = 0; cnt < 16; cnt++) { 8 result += Arow[j] * (*Bptr); 9 Bptr += N; j++; 10} 11 12 return result; 13 }
22
22 Fixed-size Arrays Aptr is in %edx, Bptr in %ecx, result in %esi, cnt in %ebx 1.L6: loop: 2 movl (%ecx),%eax Get *Bptr 3 imull (%esi, %edx, 4),%eax Multiply by Arow[j] 4 addl %eax,%ebx Add to result 5 addl $1, %edxj++ 6 addl $64,%ecx Add 64 to Bptr 7 cmpl $16,%edxcompare j : 16 8 jne.L6 if !=, goto loop
23
23 Variable-Size Arrays 1 int var_ele (int n, int A[n][n], int i, int j) 2 { 3 return A[i][j]; 4 } Declare an array int A[exp1][exp2] –either as a local variable –or as an argument to a function The dimensions of the array are determined –by evaluating the expressions at the time the declaration is encounterd
24
24 Optimization 1 /* Compute i,k of variable matrix product */ 2 int var_prod_ele (int n, int A[n][n], int B[n][n], int i, int k) { 3 int j; 4 int result = 0; 5 6 for (j = 0; j < n; j++) 7 result += A[i][j] * B[j][k]; 8 9 return result; 10 }
25
25 Assembly Code n at %ebp+8, A at %ebp+12, i at %ebp+16, j at %ebp+20 1.movl 8(%ebp), %eax Get n 2.sall $2, %eax Compute 4*n 3.movl %eax, %edx Copy 4*n 4.imull 16(%ebp), %edx Compute 4*n*i 5.movl 20(%ebp), %eax Get j 6.sall $2, %eax Compute 4*j 7.addl 12(%ebp), %eax Compute xA+ 4 ∗ j 8.movl (%eax,%edx), %eax Read from xA+ 4 ∗ (n ∗ i + j)
26
26 Assembly Code n stored at %ebp+8 Registers: Arow in %esi, Bptr in %ecx, j in %edx, result in %ebx, %edi holds 4*n 1.L30: loop: 2 movl(%ecx), %eax Get *Bptr 3 imull (%esi,%edx,4), %eax Multiply by Arow[j] 4 addl %eax, %ebx Add to result 5 addl $1, %edx Increment j 6 addl %edi, %ecx Add 4*n to Bptr 7 cmpl %edx, 8(%ebp) Compare n:j 8 jg.L30 If >, goto loop
27
27 Example static char daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} } /* day_of_year: set day of year from month & day */ int day_of_year(int year, int month, int day) { int i, leap; leap = year%4 == 0 && year%100 != 0 && year%400 ==0; for (i = 1; i < month; i++) day += daytab[leap][i]; return day; }
28
28 Example /* month_day: set month, day from day of year */ int month_day(int year, int yearday, int *pmonth, int *pday) { int i, leap; leap = year%4 == 0 && year%100 != 0 && year%400 ==0; for (i = 1; yearday > daytab[leap][i] ; i++) yearday -= daytab[leap][i]; *pmonth = i; *pday = yearday; }
29
29 Pointer Array /* month_name: return name of n-th month */ int month_name(int n) { static char *name[] = { “Illegal month”, “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” }; return (n 12) ? name[0] : name[n]; }
30
30 Pointer Array Illegal month\0 January\0 February\0 March\0 April\0 May\0 June\0 July\0 August\0 September\0 October\0 November\0 December\0
31
Command-line Arguments $ echo hello, world $ hello, world #include /* echo command-line arguments */ main(int argc, char *argv[]) { int i ; for ( i = 1; i < argc ; i++) printf(“%s%s”, argv[i], (i < argc-1) ? “ ” : “”); printf(“\n”) ; return 0; } 0 echo\0 Hello,\0 World\0 argv:
32
Command-line Arguments #include /* echo command-line arguments ; 2 nd version*/ main(int argc, char *argv[]) { while ( --argc > 0 ) printf(“%s%s”, ++argv, (argc > 1) ? “ ” : “”); printf(“\n”) ; return 0; } printf((argc > 1) ? “%s ” : “%s”);
33
Pointer to Function #include /* numcmp: compare s1 and s2 numerically */ int numcmp(char *s1, char *s2) { double v1, v2; v1 = atof(s1); v2 = atof(s2); if (v1 < v2) return -1; else if ( v1 > v2 )return 1; elsereturn 0; }
34
Pointer to Function #include { …… int numeric = 0, (*cmp)(void *, void *); char *s1, *s2; …… if (…) numeric = 1 ; …… cmp = ( int (*)(void *, void *)) (numeric ? numcmp : strcmp); (*cmp)(s1, s2); …… }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.