I'm having some difficulty translating IA32 Assembly code back to its C code counterpart. I'm 99% of the way there, but the byte offsets and register storage are confusing me.
The assembly code in question, movl %edx, %eax
, seems to set the value stored in %eax
equal to the value in %edx
, but wouldn't that mean sub = result
?
I'm new to this, so your guidance is appreciated!
int d(int x, int y, int z) // x at %ebp+8, y at %ebp+12, z at %ebp+16
{
int sub, result;
sub = z - y; // movl 12(%ebp), %edx
result = sub; // subl 16(%ebp), %edx
??????????? // movl %edx, %eax
result <<= 31; // sall $31, %eax
result >>= 31; // sarl $31, %eax
result = sub * result; // imull 8(%ebp), %edx
sub ^= x; // xorl %edx, %eax
return result;
}
The first two lines of asm are actually the first line of C but reversed and performed in two parts:
sub = y; // movl 12(%ebp), %edx
sub -= z; // subl 16(%ebp), %edx
You seem to have slight trouble with the fact that at&t syntax (that this is) puts the destination operand on the right. As such the movl %edx, %eax
is indeed the result = sub
as written in the code. Also, the imull 8(%ebp), %edx
clearly writes into edx
so that's sub = x * result
(the eax
operand is implicit). Finally xorl %edx, %eax
is of course result ^= sub
. x
, which is 8(%ebp)
, is not even mentioned on that line.