I have a 3D array and I want to deal with the value of say c[l][i][k]..
- c is in the location
16(%ebp) - l is in
-24(%ebp) - i is in
-20(%ebp) - k is in
-12(%ebp)
The array size is [20][20][20]
The assembly code for this is
movl -24(%ebp), %eax #eax <-- l
imull $1600, %eax, %eax #eax <--1600*l
movl %eax, %ecx #ecx <--1600*l
addl 16(%ebp), %ecx #ecx <--1600*l + c
movl -20(%ebp), %edx #edx <-- i
movl %edx, %eax #eax <-- i
sall $2, %eax
addl %edx, %eax
sall $2, %eax
addl -12(%ebp), %eax
movl -8(%ebp), %edx
movl %edx, (%ecx,%eax,4)
- what is the significance of each of the assembly code lines here?
- How do we actually get the value
c[l][i][k]? - Also what is the line
movl %edx, (%ecx,%eax,4)doing?
That array is represented as a contiguous sequence of
20 * 20 * 20 * (size of one element)bytes. The address of element[l][i][k]inside this array can be computed as follows:The multiplication
1600 * ldoes the first step –l * 20 * 20 * (size of one element)bytes (which suggests that size is 4 bytes).The lines you haven’t commented compute
(((i << 2) + i) << 2) + k, which is essentially20 * i + k– the second and third steps combined, sans the multiplication by element size. (sallis “shift left”, same as C’s bitwise shift.)This missing multiplication is done by the last line:
movl %edx (%ecx, %eax, 4)computesecx + eax * 4(the address we want) and puts the value ofedxinto that address.Note that this code appears to be writing into the array, not reading from it, like your question suggests. To read that element from the array, you should use that parenthesized expression as the first argument of
movl.