How can I see the assembly code for a C++ program?


Question

How can I see the assembly code for a C++ program?

What are the popular tools to do this?

1
126
6/7/2014 2:10:23 PM

Accepted Answer

Ask the compiler

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):

    g++ -g -c -Wa,-alh foo.cc

  • For Visual Studio, use /FAsc.

Peek into the binary

If you have compiled binary,

Use your debugger

Debuggers could also show disassebly.

159
6/27/2014 10:11:34 AM

In GCC/G++, compile with -S. That will output a something.s file with the assembly code.

Edit: If you want the output to be in Intel syntax (which is IMO, much more readable, and most assembly tutorials use it), compile with -masm=intel.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon