0

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!

Ben
  • 17
  • 1
  • 1
  • 4
  • 2
    Go the other way. `gcc -S ur_c.c` will produce assembly output to look at. – dawg Feb 19 '16 at 05:12
  • please edit the code. `{` should be `}`. Is there any compile error with your code ? – Van Tr Feb 19 '16 at 05:14
  • That's what I've been attempting but the assembly output doesn't match the problem. I don't think I'm way off but maybe I am? – Ben Feb 19 '16 at 05:14
  • There is no guarantee that an arbitrary sequence of assembler statements must have a `C` counterpart which, when compiled, would generate the exact same sequence. In fact, that's provably false with any reasonable assumptions. Instead, what are you *actually* trying to do or figure out? – dxiv Feb 19 '16 at 05:24
  • The assignment is to take assembly code and write a C program that functions the same. Maybe I'm trying to be too specific? – Ben Feb 19 '16 at 05:25
  • `functions the same` does _not_ mean _compile to the same exact assembly_. – dxiv Feb 19 '16 at 05:27
  • You know.. as I was typing that I realized my mistake. I believe I'm just being paranoid but damn do I love stack for the quick responses :P – Ben Feb 19 '16 at 05:30
  • I'm not marking this as duplicate, but I wish to point out that there was another question related to this assignment here: http://stackoverflow.com/q/35468163/3857942 – Michael Petch Feb 19 '16 at 06:28

1 Answers1

3

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.

Community
  • 1
  • 1
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • Ahh, that's where my confusion came in. I imagined that I could have it disassemble the same if I were to give it the correct C code. – Ben Feb 19 '16 at 05:27
  • That being said it would be the same thing to simplify it and make it long a = x + y + z; correct? – Ben Feb 19 '16 at 05:27
  • @Ben: Yep, or even the one line `return x+y+z;` – Nate Eldredge Feb 19 '16 at 05:28
  • Really appreciate the help. I was hoping that was the case but I was having trouble finding information as to whether assembly/c lined up that specifically. – Ben Feb 19 '16 at 05:29