C语言 从 C 到汇编
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2887813/
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
from C to assembly
提问by lego69
can somebody please explain, I have a program on C, can I convert it to assembly? if yes, how?
有人可以解释一下,我有一个 C 程序,我可以将它转换为汇编吗?如果是,如何?
回答by Anders Abel
回答by Marcelo Cantos
With gcc, you can use the -Soption:
使用 gcc,您可以使用以下-S选项:
gcc -O2 -S myfile.c
Most other options (such as the -O2) can be used here as well, to determine what kind of assembly code gets produced.
大多数其他选项(例如-O2)也可以在此处使用,以确定生成的汇编代码类型。
Note, however, that this is simply exposing an intermediate step that the compiler goes through anyway (not the generation of an assembly source file, that is, but the machine code that it represents). The end result, after passing this code through an assembler won't differ in any way from simply compiling directly to machine code.
但是请注意,这只是暴露了编译器无论如何都要经历的中间步骤(不是汇编源文件的生成,即它所代表的机器代码)。最终结果,在通过汇编程序传递此代码后,与直接编译为机器代码没有任何区别。
回答by Vijay Mathew
Your compiler should have some option to do that. For instance, for gcc you can use the -S option. A short example:
您的编译器应该有一些选项可以做到这一点。例如,对于 gcc,您可以使用 -S 选项。一个简短的例子:
// test.c
#include <stdio.h>
int
main ()
{
printf ("hello, world\n");
return 0;
}
Compile it with the -Soption. This will produce a test.s file which will contain the assembly:
用-S选项编译它。这将生成一个包含程序集的 test.s 文件:
.file "test.c"
.section .rodata
.LC0:
.string "hello, world"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl , %esp
movl $.LC0, (%esp)
call puts
movl $ cat test.c
#include <stdio.h>
int main(int argc, char **argv) {
int a = 11+12;
printf("a = %d\n", a);
return 0;
}
, %eax
addl , %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
.section .note.GNU-stack,"",@progbits
回答by Johannes Weiss
That's what your compiler does.
这就是你的编译器所做的。
The compiler compiles your C program to machine language which is a binary representation of the machine program. When a human wants to write machine language, s/he writes it in assembler, which gets translated to the binary machine language.
编译器将您的 C 程序编译为机器语言,这是机器程序的二进制表示。当一个人想要编写机器语言时,他/她用汇编程序编写它,汇编程序被翻译成二进制机器语言。
The assembler code is simply a human readable form of the binary machine language.
汇编代码只是二进制机器语言的人类可读形式。
The C program:
C程序:
$ gcc -c test.c
compile it
编译它
$ objdump -d test.o
test.o: file format elf32-i386
Disassembly of section .text:
00000000 <main>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 e4 f0 and ##代码##xfffffff0,%esp
6: 83 ec 20 sub ##代码##x20,%esp
9: c7 44 24 1c 17 00 00 movl ##代码##x17,0x1c(%esp)
10: 00
11: b8 00 00 00 00 mov ##代码##x0,%eax
16: 8b 54 24 1c mov 0x1c(%esp),%edx
1a: 89 54 24 04 mov %edx,0x4(%esp)
1e: 89 04 24 mov %eax,(%esp)
21: e8 fc ff ff ff call 22 <main+0x22>
26: b8 00 00 00 00 mov ##代码##x0,%eax
2b: c9 leave
2c: c3 ret
disassemble it:
拆解它:
##代码##回答by garph0
Most of the compilers have some option to generate assembly listings along with the binary files.
In Visual Studio you can find it under "code generation" section in the file properties.
With gcc you can use -S switch (capital S)
Finally if you have a binary you can use objdump -S (capital S).
大多数编译器都有一些选项来生成程序集列表和二进制文件。在 Visual Studio 中,您可以在文件属性的“代码生成”部分下找到它。
使用 gcc,您可以使用 -S 开关(大写 S)
最后,如果您有二进制文件,您可以使用 objdump -S(大写 S)。
回答by Evan
Since you mentioned Dev-C++ it is worth mentioning that the -Sflag also works there. Assuming Windows, Dev-C++ will still name the output .exe, but the result won't be an executable file so just change the extension to .txt or whatever so that it can be read in the editor of your choice. Add options/flags under Project>Project Options>Parameters>Compiler or Tools>Compiler Options.
既然你提到了 Dev-C++,值得一提的是,该-S标志也在那里工作。假设在 Windows 上,Dev-C++ 仍会将输出命名为 .exe,但结果不会是可执行文件,因此只需将扩展名更改为 .txt 或其他任何内容,以便可以在您选择的编辑器中读取它。在 Project>Project Options>Parameters>Compiler 或 Tools>Compiler Options 下添加选项/标志。

