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

gcc output assembly annotated
gcc output assembly intel syntax
c to assembly
c to assembly examples
assembly programming in c
gcc options
using assembly language with c/c++
generate assembly code from c online

How does one do this?

If I want to analyze how something is getting compiled, how would I get the emitted assembly code?

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.

How can I see the assembly code for a C++ program?, that contains the source for a computer program. It may be written in a number of different programming languages, but is commonly used for storing Assembly code. Here we will see how to generate assembler output from C or C++ code using gcc. The gcc provides a great feature to get all intermediate outputs from a source code while executing. To get the assembler output we can use the option ‘-S’ for the gcc. This option shows the output after compiling, but before sending to the assembler.

This will generate the asm code with the C code + line numbers interweaved to more easily see what lines generate what code.

# create assembler code:
c++ -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).

objdump.c, Here we will see how to generate assembler output from C or C++ code using gcc.The gcc provides a great feature to get all intermediate  The C standard way of doing the job is to use the basic asm command: __asm__ ( assembler instructions ); The assembler instructions are represented by a single string or a set of strings, one for each line of assembler. You have to terminate each line correctly for the assembler in use.

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.txt
horton_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.s
166 0128 55                    call ___main
167 0129 89E5          .stabn 68,0,21,LM2-_main
168 012b 81EC8000      LM2:
168      0000
169 0131 E8000000      LBB2:
169      00
170                    .stabn 68,0,25,LM3-_main
171                    LM3:
172                            movl $0,-16(%ebp)

S File Extension, Q: How can I create a file where I can see the C code and its assembly translation together? A: Use the -S (note: capital S) switch to GCC, and it will emit the  Get an ad-free experience with special benefits, and directly support Reddit. get reddit premium. How do you get assembler output from C/C++ source in gcc?

Use the -S switch

g++ -S main.cpp

or also with gcc

gcc -S main.c

Also see this

How do you get assembler output from C/C++ source in gcc?, The standard method to generate assembler code from a C program is to run gcc -S test.c . This will output a file test.s . But, with some  The moral of the story here, then, is to be sure your C/C++ coding is as tight as possible in the first place, before deciding that a venture into assembly language is necessary. Don't forget that assembly, unlike C/C++, is not portable, and, that if you do make use of SSE instructions directly in your code, you'll be restricting your code to run only on processors supporting those instructions.

If what you want to see depends on the linking of the output, then objdump on the output object file/executable may also be useful in addition to the aforementioned gcc -S. Here's a very useful script by Loren Merritt that converts the default objdump syntax into the more readable nasm syntax:

#!/usr/bin/perl -w
$ptr='(BYTE|WORD|DWORD|QWORD|XMMWORD) PTR ';
$reg='(?:[er]?(?:[abcd]x|[sd]i|[sb]p)|[abcd][hl]|r1?[0-589][dwb]?|mm[0-7]|xmm1?[0-9])';
open FH, '-|', '/usr/bin/objdump', '-w', '-M', 'intel', @ARGV or die;
$prev = "";
while(<FH>){
    if(/$ptr/o) {
        s/$ptr(\[[^\[\]]+\],$reg)/$2/o or
        s/($reg,)$ptr(\[[^\[\]]+\])/$1$3/o or
        s/$ptr/lc $1/oe;
    }
    if($prev =~ /\t(repz )?ret / and
       $_ =~ /\tnop |\txchg *ax,ax$/) {
       # drop this line
    } else {
       print $prev;
       $prev = $_;
    }
}
print $prev;
close FH;

I suspect this can also be used on the output of gcc -S.

Assembly output, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 .file "test.c" .section .rodata .LC0: If you do it on the .o's then external references will not be fixed up and will show 00 00, if you do it on the .elf you'll get a complete diassembly of the entire program. Most Makefiles already do the latter and call the output.lss. As you use Studio just start with -save-temps and -fverbose-asm for [All Files] under "custom options".

Coding Tricks 101: How to Save the Assembler Code Generated by , How to Generate Mixed Source and Assembly List from Source Code using GCC tagged C, Command line, gcc, Howto, Linux, Programming,  Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C++, Rust, Go (and many more) code.

understand gcc assembly output, This will cause gcc to run the compiler, generating an assembly file. Suppose we write a C code and store it in a file name “geeks.c” . godbolt is a very useful tool, they list only has C++ compilers but you can use -x c flag in order to get it treat the code as C. It will then generate an assembly listing for your code side by side and you can use the Colourise option to generate colored bars to visually indicate which source code maps to the generated assembly.

How to Generate Mixed Source and Assembly List from Source , c c-header cpp-output c++ c++-header c++-cpp-output objective-c whether it be an executable file, an object file, an assembler file or preprocessed C code. If you are building the program yourself, you can ask your compiler to emit assembly source. For most UNIX compilers use the -S switch. If you are using the GNU assembler, compiling with -g -Wa,-alh will give intermixed source and assembly on stdout ( -Wa asks compiler driver to pass options to assembler, -al turns on assembly listing, and -ah adds "high-level source" listing):

Comments
  • For tips on making the asm output human readable, see also: How to remove "noise" from GCC/clang assembly output?
  • While this is correct, I found the results from Cr McDonough's answer to be more useful.
  • an addition use : objdump -M intel -S --disassemble helloworld > helloworld.dump to get the object dump in intel syntax compatible with nasm on linux.
  • If you have a single function to optimize/check, then you can give a try to online Interactive C++ Compilers i.e. godbolt
  • @touchStone: GAS .intel_syntax is not compatible with NASM. It's more like MASM (e.g. mov eax, symbol is a load, unlike in NASM where it's a mov r32, imm32 of the address), but not totally compatible with MASM either. I do highly recommend it as a nice format to read, especially if you like to write in NASM syntax though. objdump -drwC -Mintel | less or gcc foo.c -O1 -fverbose-asm -masm=intel -S -o- | less are useful. (See also How to remove "noise" from GCC/clang assembly output?). -masm=intel works with clang, too.
  • Better use gcc -O -fverbose-asm -S
  • (That's actually on page (numbered) 3 (which is the 15th page of the PDF))
  • Sadly, as on OS X doesn't know these flags. If it did, though, you could probably one-line this using -Wa to pass options to as.
  • g++ -g -O0 -c -fverbose-asm -Wa,-adhln test.cpp > test.lst would be the short hand version of this.
  • You can also use either gcc -c -g -Wa,-ahl=test.s test.c or gcc -c -g -Wa,-a,-ad test.c > test.txt
  • A blog post explaining this in more detail, including the one-command version like legends and Lu'u posted. But why -O0? That's full of loads/stores that make it hard to track a value, and doesn't tell you anything about how efficient the optimized code will be.