如何查看 C++ 程序的汇编代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/840321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I see the assembly code for a C++ program?
提问by Geek
How can I see the assembly code for a C++ program?
如何查看 C++ 程序的汇编代码?
What are the popular tools to do this?
有哪些流行的工具可以做到这一点?
回答by Employed Russian
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.
如果您自己构建程序,则可以要求编译器发出汇编源代码。对于大多数 UNIX 编译器,使用该-S
开关。
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
.
如果您使用的是 GNU 汇编器,编译 with
-g -Wa,-alh
将在 stdout 上提供混合的源代码和汇编(-Wa
要求编译器驱动程序将选项传递给汇编器,-al
打开汇编列表,并-ah
添加“高级源”列表):g++ -g -c -Wa,-alh foo.cc
对于 Visual Studio,请使用
/FAsc
.
Peek into the binary
查看二进制文件
If you have compiled binary,
如果你已经编译了二进制文件,
- use
objdump -d a.out
on UNIX (also works for cygwin), dumpbin /DISASM foo.exe
on Windows.
objdump -d a.out
在 UNIX 上使用(也适用于 cygwin),dumpbin /DISASM foo.exe
在 Windows 上。
Use your debugger
使用您的调试器
Debuggers could also show disassebly.
调试器也可能显示为可拆卸。
- Use
disas
command in GDB, - or the disassembly windowof Visual Studio on Windows.
disas
在 GDB 中使用命令,- 或Windows 上 Visual Studio的反汇编窗口。
回答by Zifre
In GCC/G++, compile with -S
. That will output a something.s
file with the assembly code.
在 GCC/G++ 中,使用-S
. 这将输出一个something.s
带有汇编代码的文件。
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
.
编辑:如果您希望输出采用 Intel 语法(这是 IMO,更具可读性,并且大多数汇编教程都使用它),请使用-masm=intel
.
回答by Viktor Sehr
In Visual Studio;
在 Visual Studio 中;
- set a breakpoint
- run the program until it stops at the breakpoint
- rightclick on the sourcecode and pick "show dissasembly"
- 设置断点
- 运行程序直到它在断点处停止
- 右键单击源代码并选择“显示拆卸”
回答by osgx
For gcc/g++
对于 gcc/g++
gcc -save-temps -fverbose-asm prog.c
This will generate prog.s with some comments on variables used in every asm line:
这将生成 prog.s,其中包含对每个 asm 行中使用的变量的一些注释:
movl , -24(%ebp) #, readme
movl -16(%ebp), %eax # pid, pid
movl %eax, 4(%esp) # pid,
movl $.LC0, (%esp) #,
call printf #
回答by Carlo Wood
This site is currently working for me (2017): https://godbolt.org/
这个网站目前正在为我工作(2017):https: //godbolt.org/
回答by Adam Markowitz
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 disassemblersfor information on options.
您使用的任何调试器都应该有一个程序集视图(Visual Studio、Borland IDE、gdb 等)。如果您没有使用调试器,而只想查看程序中的程序集,则可以使用反汇编器,或者运行程序并使用调试器附加到它,然后从那里进行转储。有关选项的信息,请参阅反汇编程序的参考资料。
回答by Bastien Léonard
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.
很多人已经告诉过如何使用给定的编译器发出汇编代码。另一种解决方案是编译一个目标文件并使用诸如objdump、readelf(在 Unix 上)或 DUMPBIN( link) (在 Windows 上)之类的工具转储它。您也可以转储可执行文件,但读取输出会更加困难。
This has the advantage of working the same way with any compiler.
这具有与任何编译器以相同方式工作的优点。
回答by Nik
Most compilers have an option to output an assembly listing. E.g. with VisualStudio you can use something like:
大多数编译器都有输出汇编列表的选项。例如,使用 VisualStudio,您可以使用以下内容:
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.
不过,为了获得最佳可读性,大多数调试器会提供一个视图,将反汇编与原始源代码交错排列,这样您就可以将代码与编译器的输出逐行进行比较。
回答by Ori Pessach
As someone else mentioned, your platform's debugger is a good starting point. For the Hymanhammer of all debuggers and disassemblers, take a look at IDA Pro.
正如其他人提到的,您平台的调试器是一个很好的起点。对于所有调试器和反汇编器的手提钻,看看IDA Pro。
On Unix/Linux platforms (including Cygwin) you can use objdump --disassemble <executable>
.
在 Unix/Linux 平台(包括 Cygwin)上,您可以使用objdump --disassemble <executable>
.
回答by Wylder
PE Explorer Disassemblerfor 32-bit PE files. IDA for others.
用于 32 位 PE 文件的PE Explorer 反汇编程序。其他人的IDA。