Consider the source code below, where N is a constant declared with #define
typedef int mat[n][n] //(size_t is defined as unsigned long, which is 8 bytes on x8464 machines)
int ele(mat a, size_t i, size_t j){
return a[i][j];
}
Suppose this generate the following assembly code:
ele:
salq $6, %rsi // i= i<< 6 = 64i
addq %rsi, %rdi // a= a + i = a +64i
movl(%rdi, %rdx, 4), %eax // result = a + 4*j = a + 64i + 4j = a + 8*8*i + 8*1/2*j, since the size(t) = 8
The goal is find n = ____ ? I came up n = 8, you can see my steps in comments. However, the correct answer is n = 16. Can anyone help me find where is the mistake
1 answer

result = a + 64i + 4j = a + 8*8*i + 8*1/2*j, since the size(t) = 8
Your expansion here using 8 is wrong. Look at it this way.
a + 64i + 4j = a + 16*4i + 4j
Here
a
is base address of array.i
is row selector. Each row contains 16 elements of size 4 (sizeof int
) and you skipi
such rows to get the base address of thei
th row. Then you skip4j
bytes (j
ints) to reach the column you want.I don't know what your end objective is, but if all you wanted to do was find
n
, then you could simply have donesqrt(sizeof(mat)/sizeof(int))
assuming you are sure that it is an int array of square shape.