C++ – How to see the assembly code for a C++ program

assemblycdisassembly

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

What are the popular tools to do this?

Best 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 a binary

If you have a compiled binary,

Use your debugger

Debuggers could also show disassembly.

  • Use disas command in GDB.
    Use set disassembly-flavor intel if you prefer Intel syntax.
  • or the disassembly window of Visual Studio on Windows.