Nik*_*ntz 20 compiler-construction x86 assembly gcc
gcc可以生成程序集但是如何用gcc或其他编译器编译纯程序集?我知道x86汇编难,另一个指令集比MIPS和的Nios我一直在寻找,但现在我想尝试直接编译ASM 86.有关于如何操作的说明,但是包含了一个C文件,我不需要一个C文件用于我的第一个最基本的编译.
gcc -o test_asm asm_functions.S test_asm.c
Run Code Online (Sandbox Code Playgroud)
有创建.o文件的步骤
gcc -c asm_functions.S
gcc -c test_asm.c
gcc -o test_asm asm_functions.o test_asm.o
Run Code Online (Sandbox Code Playgroud)
但我没有看到我可以用gcc直接编译x86 asm的步骤.还有另一个名为GNU as(GNU Assembler)的程序,它可以用于将x86程序集转换为机器代码吗?
代码(32.s)
.globl _start
.text
_start:
movl $len, %edx
movl $msg, %ecx
movl $1, %ebx
movl $4, %eax
int $0x80
movl $0, %ebx
movl $1, %eax
int $0x80
.data
msg:
.ascii "Hello, world!\n"
len = . - msg
Run Code Online (Sandbox Code Playgroud)
脚步
$ gcc -c 32.s
$ ls 32*
32.o 32.s
$ gcc -o 32 32.o
32.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
Run Code Online (Sandbox Code Playgroud)
所以似乎它可能混合了32位和64位,我必须告诉编译器程序集是32位还是64位指令?
这个测试适用于gcc.
$ cat hello.s
.data
.globl hello
hello:
.string "Hi World\n"
.text
.global main
main:
pushq %rbp
movq %rsp, %rbp
movq $hello, %rdi
call puts
movq $0, %rax
leave
ret
$ gcc hello.s -o hello
$ ./hello
Hi World
Run Code Online (Sandbox Code Playgroud)
Die*_*Epp 19
你已经在做了.
gcc -c asm_functions.S
Run Code Online (Sandbox Code Playgroud)
该步骤生成一个目标文件asm_functions.o.目标文件是包含机器代码的"可链接"(而不是"可加载")文件,以及链接器在链接时如何修改代码的一些额外说明.该gcc程序本身只是一个驱动程序,它运行as在幕后为您生产asm_functions.o.所以你可以选择as直接运行,但通常运行gcc前端会更容易.
| 归档时间: |
|
| 查看次数: |
33879 次 |
| 最近记录: |