I'm playing around with some asm code, and something is bothering me.
我在玩asm的代码,有什么事困扰着我。
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.S
this generates a nice little piece of asm code:
用gcc文件。c - s - o文件。这就产生了一个很好的asm代码:
.cstring
LC0:
.ascii "Hello World\0"
.text
.globl _main
_main:
LFB3:
pushq %rbp
LCFI0:
movq %rsp, %rbp
LCFI1:
subq $16, %rsp
LCFI2:
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
leaq LC0(%rip), %rdi
call _puts
movl $0, %eax
leave
ret
LFE3:
.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
.set L$set$0,LECIE1-LSCIE1
.long L$set$0
LSCIE1:
.long 0x0
.byte 0x1
.ascii "zR\0"
.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$1,LEFDE1-LASFDE1
.long L$set$1
LASFDE1:
.long LASFDE1-EH_frame1
.quad LFB3-.
.set L$set$2,LFE3-LFB3
.quad L$set$2
.byte 0x0
.byte 0x4
.set L$set$3,LCFI0-LFB3
.long L$set$3
.byte 0xe
.byte 0x10
.byte 0x86
.byte 0x2
.byte 0x4
.set L$set$4,LCFI1-LCFI0
.long L$set$4
.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帮我做吗?
7 个解决方案
#1
67
Yes, You can use gcc to compile your asm code. Use -c for compilation like this:
是的,您可以使用gcc来编译asm代码。使用-c编译如下:
gcc -c file.S -o file.o
This will give object code file named file.o. To invoke linker perform following after above command:
这将给出名为file.o的对象代码文件。调用链接器执行以下命令:
gcc file.o -o file
#2
40
gcc
can 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'), thengcc
calls the assembler. - 如果文件名以“”结尾。然后gcc调用汇编程序。
- If the file name ends with "
.S
" (uppercase 'S'), thengcc
applies the C preprocessor on the source file (i.e. it recognizes directives such as#if
and replaces macros), and then calls the assembler on the result. - 如果文件名以“”结尾。然后,gcc在源文件上应用C预处理器(也就是说,它识别诸如#if和替换宏的指令),然后在结果上调用汇编程序。
So, on a general basis, you want to do things like this:
所以,总的来说,你想做这样的事情:
gcc -S file.c -o file.s
gcc -c file.s
#3
7
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中声明的变量进行交互。
- eax shall be foo
- eax应当foo
- ebx shall be bar
- ebx应当酒吧
- the value in eax shall be stored in foo after the assembly code executed
- eax中的值应该在执行汇编代码之后存储在foo中
\n
\ n
int 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;
}
#4
3
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++编译器、汇编器、链接器等的“驱动”程序。)
#5
3
You can use GAS, which is gcc's backend assembler:
您可以使用GAS,这是gcc的后端汇编程序:
http://linux.die.net/man/1/as
#6
2
If you have main.s file. you can generate object file by GCC
and also as
如果你有主。年代文件。您可以通过GCC和as生成对象文件
# gcc -c main.s
# as main.s -o main.o
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/
#7
0
nasm -f bin -o 2_hello 2_hello.asm
#1
67
Yes, You can use gcc to compile your asm code. Use -c for compilation like this:
是的,您可以使用gcc来编译asm代码。使用-c编译如下:
gcc -c file.S -o file.o
This will give object code file named file.o. To invoke linker perform following after above command:
这将给出名为file.o的对象代码文件。调用链接器执行以下命令:
gcc file.o -o file
#2
40
gcc
can 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'), thengcc
calls the assembler. - 如果文件名以“”结尾。然后gcc调用汇编程序。
- If the file name ends with "
.S
" (uppercase 'S'), thengcc
applies the C preprocessor on the source file (i.e. it recognizes directives such as#if
and replaces macros), and then calls the assembler on the result. - 如果文件名以“”结尾。然后,gcc在源文件上应用C预处理器(也就是说,它识别诸如#if和替换宏的指令),然后在结果上调用汇编程序。
So, on a general basis, you want to do things like this:
所以,总的来说,你想做这样的事情:
gcc -S file.c -o file.s
gcc -c file.s
#3
7
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中声明的变量进行交互。
- eax shall be foo
- eax应当foo
- ebx shall be bar
- ebx应当酒吧
- the value in eax shall be stored in foo after the assembly code executed
- eax中的值应该在执行汇编代码之后存储在foo中
\n
\ n
int 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;
}
#4
3
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++编译器、汇编器、链接器等的“驱动”程序。)
#5
3
You can use GAS, which is gcc's backend assembler:
您可以使用GAS,这是gcc的后端汇编程序:
http://linux.die.net/man/1/as
#6
2
If you have main.s file. you can generate object file by GCC
and also as
如果你有主。年代文件。您可以通过GCC和as生成对象文件
# gcc -c main.s
# as main.s -o main.o
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/
#7
0
nasm -f bin -o 2_hello 2_hello.asm