Reputation: 705
I have this piece of code that I have to convert from assembly to c. I have to convert the assembly code for a function into the c code for that function.
function :
1.pushl %ebp
2.movl %esp, %ebp
3.movl 12(%ebp), %eax
4.movl 8(%ebp), %edx
5.addl %edx, %eax
6.addl 16(%ebp), %eax
7.popl %ebp
8.ret
I numbered the lines just for explanation purposes. Here is what I think each line is doing:
1.Save frame pointer by moving it onto the stack.
2. Create new frame pointer
3. Assigns value of the second parameter, which is 12 bytes above the base pointer, to the register eax
4. Assigns value of first parameter which is 8 bytes above base pointer to the register edx
5. Adds edx to the value in eax and stores the value in eax
6. Adds the value in third parameter which is 16 bytes about the base pointer, to the register eax
7.pops base pointer off of stack
8. returns the calling function
Now I am having trouble really understanding how to translate this into c code. I was thinking the code would look a little like this:
function (int *x, int *y, int *z){
*y = x + y;
*y = z + y;
return y;
}
I'm not sure it would be exactly like this, but this is what I came up with. I am having trouble really understanding how to understand assembly. Does this translation make sense?Any help is appreciated.
Upvotes: 2
Views: 802
Reputation: 34829
Your interpretation of the assembly code is correct. One thing that I might add is that when a function returns a value, the value is returned in register A
. So the C function looks like this.
int function( int x, int y, int z )
{
return x + y + z;
}
Upvotes: 2