
CS485G Spring 2015 29
8. Lecture 16, 2/25/2015
9. General addressing case: T A[R][C]; defines a two-dimensional
array of type T with R rows and C columns. Say type T requires k
bytes. Then the location of A[i][j] is A + kiR + kj = A + k(iR + j).
10. Example:
1 int getElement(int n, int x[n][n], int i, int j) {
2 return a[i][j];
3 }
movl 8(%ebp), %eax # n (param 1)
sall $2, %eax # a = 4n
movl %eax, %edx # d = 4n
imull 16(%ebp), %edx # d = 4 n
*
i
movl 20(%ebp), %eax # a = j (param 4)
sall $2, %eax # a = 4j
addl 12(%ebp), %eax # a = x + 4j
movl (%eax,%edx), %eax # a =
*
(x + 4j + 4n
*
i)
11. Walking down a column can be optimized by computing the address
of the first element in the column, then repeatedly adding the stride
(the number of bytes per row).
29 Structures
1. A struct is a contiguously allocated region of memory with named
fields. Each field has its own type.
2. Example:
1 struct rec {
2 int y[3];
3 int i;
4 struct rec
*
n;
5 };
Memory layout of rec, starting at address x, is based on offsets:
y x
i x+12
n x+16
end x+20
Kommentare zu diesen Handbüchern