Converting Assembly to C

My professor posed a 'challenge' question to us in lecture on Friday. He gave us the assembly for a C program, then removed two constants from the C code. The Assembly:

sum_element:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
movl 12(%ebp),%ecx
sall $2,%ecx
leal 0(,%eax,8),%edx
subl %eax,%edx
leal (%eax,%eax,4),%eax
movl mat2(%ecx,%eax,4),%eax
addl mat1(%ecx,%edx,4),%eax
movl %ebp,%esp
popl %ebp
ret

And then the C code:

#define M  ____
#define N  ____
int mat1[M][N];
int mat2[N][M];
int sum_element(int i, int j)
{
return mat1[i][j] + mat2[i][j];
}

He then asked us what the values of M and N were.

What I've done so far:

movl %esp,%ebp ( This moves the esp register into the ebp register)

movl 8(%ebp),%eax (This moves the contents of 8 off of the ebp register into the eax register)

movl 12(%ebp),%ecx ( This moves the contents of 12 off of the ebp register into the ecx register)

sall $2,%ecx ( This is shifting the number stored at the ecx left by 2 bits, so divide by 4)

leal 0(,%eax,8),%edx (I'm not sure what the 0 in front of the expression is doing, otherise it's loading eax*8 into the edx)

subl %eax,%edx (edx = edx - eax)

leal (%eax,%eax,4),%eax (eax = eax^2*4)

movl mat2(%ecx,%eax,4),%eax (eax = whatever mat2(ecx, eax, 4) is)

addl mat1(%ecx,%edx,4),%eax (eax = eax + whatever mat1(ecx, edx, 4) is)

The parts I don't understand are the mat1, mat2, and the leal with the 0 in front.

Thanks!

Edit: To sum up what I have so far:

ecx=ecx/4
edx=eax*8-eax
eax=eax*4+eax

I thought I'd be able to calculate the values of M and N after knowing what mat1 and mat2, were, but I'm either missing the obvious or there is a tad bit more to do. Any direction would be great.

Would ecx be j and eax be i?

Thanks again!


mat1 and mat2 are just the named addresses of memory locations, same as in the C code.

movl mat2(%ecx,%eax,4),%eax -> load *(ecx + mat2 + (eax * 4)) into eax
addl mat1(%ecx,%edx,4),%eax -> add *(ecx + mat1 + (edx * 4) to eax

where mat2 and mat1 are constant numeric values representing some addresses, known to the assembler.

leal instruction is just a fancy name for an "integer add/multiply" operation, normally used for address calculations:

leal 0(,%eax,8),%edx -> load eax * 8 + 0 into edx

mat1 and mat2 are the address of the variables. Once they are not located in the stack, they have fixed values... (they are declared as static variables)... so they are just mean the memory address of mat1 and mat2

  • ecx = j
  • eax = i
  • at the beginning of the function... but later

  • ecx = j
  • eax and edx (line 10 and 11) are both executing the array offset that comes from i . This happens because the matrixes have different sizes at the first index. One is M and the other is N .
  • about the leal 0(,%eax,8),%edx , it means edx = 8 * eax it's used to calculate the offset that comes from i , but because of subl %eax,%edx , edx = edx - eax = edx - i = i * 8 - i = 7 * i with , so the final edx = 7 * i ... M = 7 .

    The same way: in the line 10 %eax = i * N = i + 4 * i = 5 * i , therefore N = 5 . i + 4 * i comes from leal (%eax,%eax,4),%eax

    mat1[i][j] > mat1 + ( M + i ) * 4 + j * 4 > mat2(%ecx,%eax,4),%eax
    mat2[i][j] > mat2 + ( N + i ) * 4 + j * 4 > mat1(%ecx,%edx,4),%eax
    

    By the way:

  • leal (%eax,%eax,4),%eax = eax += eax + eax * 4 and not eax = eax^2*4
  • sall $2,%ecx = ecx *= 4 and NOT ecx = ecx/4
  • 链接地址: http://www.djcxy.com/p/86810.html

    上一篇: 否则,有没有一个不是诺言?

    下一篇: 将装配转换为C