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

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

What are the popular tools to do this?


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 objdump -d a.out on UNIX (also works for cygwin),
  • dumpbin /DISASM foo.exe on Windows.

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.

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.


In Visual Studio;

  1. set a breakpoint
  2. run the program until it stops at the breakpoint
  3. rightclick on the sourcecode and pick "show dissasembly"

For gcc/g++

gcc -save-temps -fverbose-asm prog.c

This will generate prog.s with some comments on variables used in every asm line:

    movl    $42, -24(%ebp)  #, readme
    movl    -16(%ebp), %eax # pid, pid
    movl    %eax, 4(%esp)   # pid,
    movl    $.LC0, (%esp)   #,
    call    printf  #

This site is currently working for me (2017): https://godbolt.org/