C语言 如何编译 GCC 生成的 asm?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7190050/
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 do I compile the asm generated by GCC?
提问by Martin Kristiansen
I'm playing around with some asm code, and something is bothering me.
我在玩一些汇编代码,有些事情困扰着我。
I compile this:
我编译这个:
#include <stdio.h>
int main(int argc, char** argv){
printf("Hello World\n");
return 0;
}
with gcc file.c -S -o file.Sthis generates a nice little piece of asm code:
与gcc file.c -S -o file.S此产生一个很好的小片的汇编代码:
.cstring
LC0:
.ascii "Hello Worldgcc -c file.S -o file.o
"
.text
.globl _main
_main:
LFB3:
pushq %rbp
LCFI0:
movq %rsp, %rbp
LCFI1:
subq , %rsp
LCFI2:
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
leaq LC0(%rip), %rdi
call _puts
movl gcc file.o -o file
, %eax
leave
ret
LFE3:
.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
.set L$setgcc -S file.c -o file.s
gcc -c file.s
,LECIE1-LSCIE1
.long L$setint main(void)
{
int foo = 10, bar = 15;
__asm__ __volatile__("addl %%ebx,%%eax"
:"=a"(foo)
:"a"(foo), "b"(bar)
);
printf("foo+bar=%d\n", foo);
return 0;
}
LSCIE1:
.long 0x0
.byte 0x1
.ascii "zR# gcc -c main.s
# as main.s -o main.o
"
.byte 0x1
.byte 0x78
.byte 0x10
.byte 0x1
.byte 0x10
.byte 0xc
.byte 0x7
.byte 0x8
.byte 0x90
.byte 0x1
.align 3
LECIE1:
.globl _main.eh
_main.eh:
LSFDE1:
.set L$set,LEFDE1-LASFDE1
.long L$set
LASFDE1:
.long LASFDE1-EH_frame1
.quad LFB3-.
.set L$set,LFE3-LFB3
.quad L$set
.byte 0x0
.byte 0x4
.set L$set,LCFI0-LFB3
.long L$set
.byte 0xe
.byte 0x10
.byte 0x86
.byte 0x2
.byte 0x4
.set L$set,LCFI1-LCFI0
.long L$set
.byte 0xd
.byte 0x6
.align 3
LEFDE1:
.subsections_via_symbols
My next problem is really, how do I compile this output, and can I make GCC do it for me?
我的下一个问题真的是,我如何编译这个输出,我可以让 GCC 为我做吗?
回答by cyber_raj
Yes, You can use gcc to compile your asm code. Use -c for compilation like this:
是的,您可以使用 gcc 来编译您的 asm 代码。使用 -c 进行编译,如下所示:
nasm -f bin -o 2_hello 2_hello.asm
This will give object code file named file.o. To invoke linker perform following after above command:
这将给出名为 file.o 的目标代码文件。要调用链接器,请在上述命令后执行以下操作:
##代码##回答by Thomas Pornin
gcccan use an assembly file as input, and invoke the assembler as needed. There is a subtlety, though:
gcc可以使用汇编文件作为输入,并根据需要调用汇编程序。不过有一个微妙之处:
- If the file name ends with "
.s" (lowercase 's'), thengcccalls the assembler. - If the file name ends with "
.S" (uppercase 'S'), thengccapplies the C preprocessor on the source file (i.e. it recognizes directives such as#ifand replaces macros), and thencalls the assembler on the result.
- 如果文件名以“
.s”(小写 's')结尾,则gcc调用汇编程序。 - 如果文件名以“
.S”(大写'S')结尾,则gcc对源文件应用C 预处理器(即它识别指令,如#if并替换宏),然后根据结果调用汇编器。
So, on a general basis, you want to do things like this:
所以,一般来说,你想做这样的事情:
##代码##回答by Johannes Rudolph
You can embed the assembly code in a normal C program. Here's a good introduction. Using the appropriate syntax, you can also tell GCC you want to interact with variables declared in C. The program below instructs gcc that:
您可以将汇编代码嵌入到普通的 C 程序中。这是一个很好的介绍。使用适当的语法,您还可以告诉 GCC 您要与 C 中声明的变量进行交互。下面的程序指示 gcc:
- eax shall be foo
- ebx shall be bar
- the value in eax shall be stored in foo after the assembly code executed
- eax 应为 foo
- ebx 应为 bar
- 汇编代码执行后,eax 中的值应存储在 foo 中
\n
\n
##代码##回答by zvrba
Yes, gcc can also compile assembly source code. Alternatively, you can invoke as, which is the assembler. (gcc is just a "driver" program that uses heuristics to call C compiler, C++ compiler, assembler, linker, etc..)
是的,gcc 也可以编译汇编源代码。或者,您可以调用as,它是汇编程序。(gcc 只是一个“驱动”程序,它使用启发式来调用 C 编译器、C++ 编译器、汇编器、链接器等。)
回答by Brian Gordon
You can use GAS, which is gcc's backend assembler:
您可以使用 GAS,这是 gcc 的后端汇编程序:
回答by Srinivas Achary
If you have main.s file.
you can generate object file by GCCand also as
如果你有 main.s 文件。您可以通过生成目标文件GCC,也as
check this link, it will help you learn some binutils of GCC http://www.thegeekstuff.com/2017/01/gnu-binutils-commands/
检查此链接,它将帮助您学习 GCC 的一些 binutils http://www.thegeekstuff.com/2017/01/gnu-binutils-commands/

