3

gcc -S test.c converts the c code into assembly. What I need is a per-instruction translator. I mean I need a way through which I can know that this set of assembly instructions correspond to this c statement and so on NOT whole c code to whole assembly code. Any idea? Thanks in advance

Mustafa
  • 109
  • 1
  • 7

1 Answers1

3

This can be done with objdump -S which tries to interpret the debugging info - provided it was compiled with -g or equivalent. For example, for the program:

int main(void)
{
    int x = 42;
    int y = 24;

    return x + y;
}

It does:

00000000 <main>:
int main(void)
{
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 10                sub    $0x10,%esp
    int x = 42;
   6:   c7 45 fc 2a 00 00 00    movl   $0x2a,-0x4(%ebp)
    int y = 24;
   d:   c7 45 f8 18 00 00 00    movl   $0x18,-0x8(%ebp)

    return x + y;
  14:   8b 45 f8                mov    -0x8(%ebp),%eax
  17:   8b 55 fc                mov    -0x4(%ebp),%edx
  1a:   01 d0                   add    %edx,%eax
}
cnicutar
  • 178,505
  • 25
  • 365
  • 392