
May 4th, 2013, 04:29 PM
#1
Consider the following code (help please)
Im working on some homework and have gotten stuck.
Here is the problem I am working on.
Consider the following source code, where R, S, and T are constants declared with
Code:
#define:
int A[R][S][T];
int store_ele(int i, int j, int k, int *dest)
{
*dest = A[i][j][k];
return sizeof(A);
}
In compiling this program, gcc generates the following assembly code:
i at %ebp+8, j at %ebp+12, k at %ebp+16, dest at %ebp+20
1 movl 12(%ebp), %edx
2 leal (%edx,%edx,4), %eax
3 leal (%edx,%eax,2), %eax
4 imull $99, 8(%ebp), %edx
5 addl %edx, %eax
6 addl 16(%ebp), %eax
7 movl A(,%eax,4), %edx
8 movl 20(%ebp), %eax
9 movl %edx, (%eax)
10 movl $1980, %eax
A. Extend Equation 3.1 from two dimensions to three to provide a formula for
the location of array element A[i][j][k].
B. Use your reverse engineering skills to determine the values of R, S, and T
based on the assembly code.
I kind of know how to read this, I know that they are all being stored as a group under A, and j gets moved to the edx register in line 1.
However when it comes to the actual questions I dont know what it means by Extend equation 3.1 or how to come up with a formula for the location of A[i][j][k]. To me it appears that the final value is moved to the eax register in the last line.
Moving on to B, I do not know how to tell which values are corresponding to R, S, and T because you are not give initial locations of these variables.
Any answers, suggestions, or assistance would be greatly appreciated, thank you very much for your time and help.

May 5th, 2013, 04:31 AM
#2
Re: Consider the following code (help please)
However when it comes to the actual questions I dont know what it means by Extend equation 3.1 or how to come up with a formula for the location of A[i][j][k]. To me it appears that the final value is moved to the eax register in the last line.
return values are generally returned in eax. though I think the *dest is being used as a return value also.
To extend the code is relatively easy, youre just turning a two dimensional array into a three dimensional array. Im not sure how most hlls implement arrays, but at the assembly level, it is a single dimensional array, it just hides the math behind it. To access a specific element in the array youd follow a particular algorithm. in the above example:
Code:
*dest = A[i][j][k];
the math would look something like *dest=A[(i*SizeOfDimensionJ*SizeOfDimensionK)+(j*SizeOfDimensionK)+k]; (I think thats it at least).
I think R==i
S==j
and T==k
I believe line 1 moves j into edx, lines 26 are computing the index into the array and storing it in eax, line 7 retrieves the element at that position, line 8 saves the value in *dest, Im not sure what line 9 is doing, then line 10 is storing 1980 in eax for the "return sizeof(A);" instruction.
if I am reading this right: http://en.wikibooks.org/wiki/X86_***...operand_syntax, then I think the formula would translate into something like:
line 2:eax=edx+edx*4==edx*5
line 3:eax==edx+eax*2==edx+(edx*5)*2==edx+edx*10==edx*11 (so here you have j*11);
line 4:edx==99*i
line 5:eax=eax+edx==j*11+i*99
line 6:eax=eax+k==j*11+i*99+k
Be warned, I could be full of crap though since Ive never used gcc before.
Last edited by AKRichard; May 5th, 2013 at 04:54 AM.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
