How do you get assembler output from C/C++ source in gcc? How do you get assembler output from C/C++ source in gcc? c c

How do you get assembler output from C/C++ source in gcc?


Use the -S option to gcc (or g++).

gcc -S helloworld.c

This will run the preprocessor (cpp) over helloworld.c, perform the initial compilation and then stop before the assembler is run.

By default this will output a file helloworld.s. The output file can be still be set by using the -o option.

gcc -S -o my_asm_output.s helloworld.c

Of course this only works if you have the original source.An alternative if you only have the resultant object file is to use objdump, by setting the --disassemble option (or -d for the abbreviated form).

objdump -S --disassemble helloworld > helloworld.dump

This option works best if debugging option is enabled for the object file (-g at compilation time) and the file hasn't been stripped.

Running file helloworld will give you some indication as to the level of detail that you will get by using objdump.


This will generate assembly code with the C code + line numbers interweaved, to more easily see which lines generate what code:

# create assembler code:g++ -S -fverbose-asm -g -O2 test.cc -o test.s# create asm interlaced with source lines:as -alhnd test.s > test.lst

Found in Algorithms for programmers, page 3 (which is the overall 15th page of the PDF).


The following command line is from Christian Garbin's blog

g++ -g -O -Wa,-aslh horton_ex2_05.cpp >list.txt

I ran G++ from a DOS window on Win-XP, against a routine that contains an implicit cast

c:\gpp_code>g++ -g -O -Wa,-aslh horton_ex2_05.cpp >list.txthorton_ex2_05.cpp: In function `int main()':horton_ex2_05.cpp:92: warning: assignment to `int' from `double'

The output is asssembled generated code iterspersed with the original C++ code (the C++ code is shown as comments in the generated asm stream)

  16:horton_ex2_05.cpp **** using std::setw;  17:horton_ex2_05.cpp ****  18:horton_ex2_05.cpp **** void disp_Time_Line (void);  19:horton_ex2_05.cpp ****  20:horton_ex2_05.cpp **** int main(void)  21:horton_ex2_05.cpp **** { 164                    %ebp 165                            subl $128,%esp?GAS LISTING C:\DOCUME~1\CRAIGM~1\LOCALS~1\Temp\ccx52rCc.s166 0128 55                    call ___main167 0129 89E5          .stabn 68,0,21,LM2-_main168 012b 81EC8000      LM2:168      0000169 0131 E8000000      LBB2:169      00170                    .stabn 68,0,25,LM3-_main171                    LM3:172                            movl $0,-16(%ebp)