Consider the following source code, where R, S, and T are constants declared wit
ID: 670156 • Letter: C
Question
Consider the following source code, where R, S, and T are constants declared with #define: long int A[R][S][T]; int store_ele(int h, int i, int j, long int *dest) { A[h][i][j] = *dest; return sizeof(A); } In compiling this program, GCC generates the following assembly code (with -O2): store_ele: movslq %edi, %rdi movslq %esi, %rsi movq (%rcx), %rcx leaq (%rdi,%rdi,2), %rax movslq %edx, %rdx addq %rax, %rsi leaq (%rdx,%rsi,4), %rax movq %rcx, A(,%rax,8) movl $960, %eax ret Please explain what each instruction is doing and how to find the values of R S and T
Explanation / Answer
For an array declared as Type D[R][C], array element D[i][j] is at address &D[i][j] = xd + L(C*i + j) where L is the size of the data type in bytes. A. It follows that for an array declared as Type D[R][S][T], array element D[i][j][k] is at address &D[i][j][k] = xd + L(R(S*i + j) + k) B. i is at %ebp+8, j is at +12, k is at +16, dest is at +20 movl 12(%ebp), %edx #moves j to %edx leal (%edx, %edx, 4), %eax #multiplies %edx times 5, stores in %eax leal (%edx, %eax, 2), %eax #multiplies %eax times 2 and adds %edx (stores in %eax) imull $99, 8(%ebp), %edx #multiplies 99*x and stores it in %edx addl %edx, %eax #adds %edx to %eax addl 16(%ebp), %eax #adds k to %eax movl A(,%eax, 4), %edx #multiplies %eax * 4 and adds A movl 20(%ebp), %eax #moves dest into %eax movl %edx, (%eax) #moves %edx to the address pointed by %eax (dest) movl $1980, %eax #moves 1980 into %eax eax = dest edx = A + 4(11j + 99x + k) Registers look like this ^ before the last 2 lines are executed. xd + L(R(S*i + j) + k) = A + 4(11j + 99x + k) xd = A L = 4 (R(S*i + j) + k) = (99x + 11j + k) = (11(9x + j) + k) R = 11 S = 9 Since it returns 1980 as sizeof, we know the size of the array is 1980 bytes which is L(R*S*T) 1980 = 4(11*9*T) = 396T T = 5 RESULTS: R = 11 S = 9 T = 5