tags:

views:

1865

answers:

7

I was wondering how to use GCC on my C source file to dump a mnemonic version of the machine code so I could see what my code was being compiled into. You can do this with Java but I haven't been able to find a way with GCC.

I am trying to re-write a C method in assembly and seeing how GCC does it would be a big help.

+10  A: 

Use the -S (note: capital S) switch to GCC, and it will emit the assembly code to a file with a .s extension. For example, the following command:

gcc -O2 -S -c foo.c

will leave the generated assembly code on the file foo.s.

Ripped straight from http://www.delorie.com/djgpp/v2faq/faq8%5F20.html

Andrew Keeton
You shouldn't mix -c and -S, only use one of them. In this case, one is overriding the other, probably depending on the order in which they're used.
Adam Rosenfield
Thanks it worked great
James
+4  A: 

Use the -S (note: capital S) switch to GCC, and it will emit the assembly code to a file with a .s extension. For example, the following command:

gcc -O2 -S -c foo.c

codymanix
+28  A: 

If you compile with debug symbols, you can use objdump to produce a more readable disassembly.

>objdump --help
[...]
-S, --source             Intermix source code with disassembly
-l, --line-numbers       Include line numbers and filenames in output


Example:

> gcc -g -c test.c
> objdump -d -M intel -S test.o

test.o:     file format elf32-i386


Disassembly of section .text:

00000000 <main>:
#include <stdio.h>

int main(void)
{
   0:   55                    push   ebp
   1:   89 e5                 mov    ebp,esp
   3:   83 e4 f0              and    esp,0xfffffff0
   6:   83 ec 10              sub    esp,0x10
    puts("test");
   9:   c7 04 24 00 00 00 00  mov    DWORD PTR [esp],0x0
  10:   e8 fc ff ff ff        call   11 <main+0x11>

    return 0;
  15:   b8 00 00 00 00        mov    eax,0x0
}
  1a:   c9                    leave  
  1b:   c3                    ret
Bastien Léonard
Wow, EXACTLY what I needed!
James
Is there a switch to grab only the Intel instructions?
James
All of these are Intel instructions since they run on Intel processors :D.
toto
+1 I'm ashamed to say I never knew about objdump
anon
Amuck
It is possible to forgo the intermediate object file with the by using the switch sequence `-Wa,-adhln -g to gcc`. This assumes that the assembler is gas and this may not always be the case.
Marc
+7  A: 

I would like to add to these answers that if you give gcc the flag -fverbose-asm, the assembler it emits will be a lot clearer to read.

kmm
+1  A: 
amaterasu
Paul Nathan
Longpoke
A: 

I think there is some option to output commented C code for each line, entry, whatever..

ZeroCool
+3  A: 

You can use gdb for this like objdump.

This excerpt is taken from http://sources.redhat.com/gdb/current/onlinedocs/gdb%5F9.html#SEC64


Here is an example showing mixed source+assembly for Intel x86:

(gdb) disas /m main
Dump of assembler code for function main:
5 {
0x08048330 : push %ebp
0x08048331 : mov %esp,%ebp
0x08048333 : sub $0x8,%esp
0x08048336 : and $0xfffffff0,%esp
0x08048339 : sub $0x10,%esp

6 printf ("Hello.\n");
0x0804833c : movl $0x8048440,(%esp)
0x08048343 : call 0x8048284

7 return 0;
8 }
0x08048348 : mov $0x0,%eax
0x0804834d : leave
0x0804834e : ret

End of assembler dump.


dexkid
Probably a good asm primer ;)
amaterasu