If I declare these 3 arrays
int a[10][10];
int b[10][15];
int c[10][30];
For which of these three arrays, would the assembly code return the [i][j] element? Assuming that the starting address of the array is stored in %ebx.
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx /* index i */
movl 12(%ebp), %ecx /* index j */
movl %edx, %eax
sall $4, %eax
subl %edx, %eax
addl %ecx, %eax
movl (%ebx,%eax,4), %eax
popl %ebp
ret
How do you solve this type of questions
I’d approach this by doing some hand-written math with notes, such as this:
So, in
eaxat the end you get what’s in the given array (pointed byebxat the beginning) at position15 * i + j. This can properly address:Given that and your three arrays:
this addresses
bproperly, but notanorc.