Reputation: 17
I'm attempting to learn assembly language and I'm not sure if I'm on the right track. I have a program in assembly language that I need to convert to C.
addq %rsi, %rdi
addq %rdi, %rdx
movq %rdx, %rax
ret
Trying to work through it myself I come up with something along the lines of
long p1(long x, long y, long z)
{
x = x + y;
z = z + x;
long a = z;
return a;
}
No matter which way I look at it I feel like I'm close but when I objdump I get nothing of the sort. Looking for some guidance.. thanks!
Upvotes: 0
Views: 156
Reputation: 58673
Your C code looks fine, and as far as I can tell, it does the same thing as your assembly code.
You shouldn't necessarily expect that compiling your C code will give exactly the same assembly as the example. There are many different ways to convert C into assembly.
For instance, if I compile your C code with gcc -O0
(no optimization), I get much longer assembly, which stores all the function arguments and variables onto the stack. This is unnecessary (and slower) but makes things easier for a debugger. If I compile with gcc -O2
, I get
leaq (%rdi,%rsi), %rax
addq %rdx, %rax
ret
Here the compiler has made clever use of the lea
instruction to do an addition and a move in one instruction.
Upvotes: 3