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

14

158

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

What are the popular tools to do this?

Eyebrow answered 8/5, 2009 at 15:15 Comment(3)
Microsoft Visual C++ Express just set a breakpoint and press Alt +8Electrocardiograph
Possible Duplicate: #137538Metanephros
Related: How to remove "noise" from GCC/clang assembly output? includes tips on creating simple functions whose asm is interesting to look at (e.g. function args instead of constants), and a link to Matt Godbolt's CppCon talk about what to look for in compiler output.Coastline
M
193

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.
Malvern answered 8/5, 2009 at 15:22 Comment(2)
I typed the disas command and it worked!.. but can you please tell me how do I see it in the Intel syntax?Accompanist
Note that objdump -drwC -S can also disassemble with mixed source+asm: Using GCC to produce readable assembly?. Also, How to remove "noise" from GCC/clang assembly output? for more about getting nice output from gcc -O2 -SCoastline
L
38

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.

Librarianship answered 8/5, 2009 at 15:25 Comment(2)
add also -fverbose-asm optionSirotek
If this doesn't work for you, make sure the S is capitalizedIrreformable
G
25

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"
Gorgon answered 8/5, 2009 at 15:23 Comment(0)
S
16

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  #
Sirotek answered 8/2, 2010 at 17:8 Comment(0)
O
14

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

Oxonian answered 25/4, 2017 at 14:39 Comment(0)
L
6

Whatever debugger you're using should have an assembly view (Visual Studio, Borland IDE, gdb, etc.). If you are not using a debugger and you merely want to see what assembly is in a program, you can use a disassembler or alternatively, run the program and attach to it with a debugger and do the dump from there. See references to disassemblers for information on options.

Lorin answered 8/5, 2009 at 15:19 Comment(0)
A
6

Lots of people already told how to emit assembly code with a given compiler. Another solution is to compile an object file and dump it with a tool such objdump, readelf (on Unix) or DUMPBIN(link) (on Windows). You can also dump an executable, but it will be more difficult to read the output.

This has the advantage of working the same way with any compiler.

Analyse answered 8/5, 2009 at 17:38 Comment(0)
E
5

As someone else mentioned, your platform's debugger is a good starting point. For the jackhammer of all debuggers and disassemblers, take a look at IDA Pro.

On Unix/Linux platforms (including Cygwin) you can use objdump --disassemble <executable>.

Elbow answered 8/5, 2009 at 15:22 Comment(3)
If there is an option to have the compiler generate the assembler (like gcc -S, or the VS /FA option below), that is preferable over disassembly. It is more symbolic.Salicaceous
Sure, if you have the source.Elbow
By the way, you'd be surprised how much symbol information can be deduced by IDA Pro.Elbow
D
5

Most compilers have an option to output an assembly listing. E.g. with VisualStudio you can use something like:

cl.exe /FAfile.asm file.c

For best readability though, most debuggers will offer a view that interleaves the disassembly with the original source, so you can compare your code with the compiler's output line by line.

Decree answered 8/5, 2009 at 15:25 Comment(0)
D
4

PE Explorer Disassembler for 32-bit PE files. IDA for others.

Deity answered 17/7, 2009 at 23:51 Comment(0)
B
3

You can also try this site: http://assembly.ynh.io/

There, you can paste your C or C++ code and press a blue button to see the assembly equivalent version.

Bisutun answered 1/8, 2015 at 5:50 Comment(2)
FYI, failing to loadMisunderstanding
Yeah, back in 2015 it was working fine, then it suddenly stopped. Then came ctoassembly.com, but it worked for only a small subset of C. The same happend: not loading anymore. Too bad.Bisutun
S
2

In Visual Studio you can generate the assembler listing for a C++ project.

Go to project properties, then to C++/Output Files and set Assembler Output setting and ASM list location to a file name.

Scanlan answered 8/5, 2009 at 16:46 Comment(0)
A
1

On an Intel Mac OS X 10.8 (Mountain Lion) the -masm=intel directive didn't work. However, if you have Xcode installed, it should have installed the tool named 'otool':

otool code.o -tV

You have to provide the compiled object code as a parameter.

Antechoir answered 20/8, 2012 at 13:40 Comment(0)
A
0

If you're an Eclipse user, you can use the Disassembly view.

The Disassembly view shows the loaded program as assembler instructions mixed with source code for comparison. The currently executing line is indicated by an arrow marker and highlighted in the view. You can do the following tasks in the Disassembly view:

  • Set breakpoints at the start of any assembler instruction
  • Enable and disable breakpoints and their set their properties
  • Step through the disassembly instructions of your program
  • Jump to specific instructions in the program
Anse answered 28/2, 2011 at 8:8 Comment(3)
Can you elaborate a bit?Unmeaning
There are also assembly view when debugging in MSVCMignonne
I don't have a working Eclipse C++ development environment right now, but here's the official documentation: help.eclipse.org/kepler/…Anse

© 2022 - 2024 — McMap. All rights reserved.