Jay*_*rod 58
GNU汇编程序可能已经安装在您的系统上.尝试man as查看完整的使用信息.as如果你真的真的想要,你可以用来编译单个文件和ld链接.
然而,海湾合作委员会是一个伟大的前端.它可以为您组装.s文件.例如:
$ cat >hello.s <<"EOF"
.section .rodata # read-only static data
.globl hello
hello:
.string "Hello, world!" # zero-terminated C string
.text
.global main
main:
push %rbp
mov %rsp, %rbp # create a stack frame
mov $hello, %edi # put the address of hello into RDI
call puts # as the first arg for puts
mov $0, %eax # return value = 0. Normally xor %eax,%eax
leave # tear down the stack frame
ret # pop the return address off the stack into RIP
EOF
$ gcc hello.s -no-pie -o hello
$ ./hello
Hello, world!
Run Code Online (Sandbox Code Playgroud)
上面的代码是x86-64.如果你想制作一个与位置无关的可执行文件(PIE),你需要lea hello(%rip), %rdi和call puts@plt.
非PIE可执行文件(位置相关)可以对静态数据使用32位绝对寻址,但PIE应使用RIP相对LEA.(另请参阅x86-64中的movq和movabsq之间的差异既不是movq也不movabsq是一个好选择.)
如果要编写32位代码,则调用约定不同,并且RIP相对寻址不可用.(所以你push $hello在电话会议之前,然后弹出堆栈args.)
如果你好奇某些东西是如何工作的,你也可以直接将C/C++代码编译成汇编:
$ cat >hello.c <<EOF
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}
EOF
$ gcc -S hello.c -o hello.s
Run Code Online (Sandbox Code Playgroud)
另请参见如何从GCC /铿锵声组件输出中删除"噪音"?有关查看编译器输出以及编写将编译为有趣输出的有用小函数的更多信息.
Geo*_*rge 44
GNU汇编程序(gas)和NASM都是不错的选择.但是,它们有一些差异,最重要的是你操作的顺序和操作数.
gas使用AT&T语法(指南:https://stackoverflow.com/tags/att/info):
mnemonic source, destination
Run Code Online (Sandbox Code Playgroud)
nasm使用英特尔风格(指南:https://stackoverflow.com/tags/intel-syntax/info):
mnemonic destination, source
Run Code Online (Sandbox Code Playgroud)
任何一个人都可能做你需要的.GAS还有一个Intel语法模式,它很像MASM,而不是NASM.
试试这个教程:http: //asm.sourceforge.net/intro/Assembly-Intro.html
另请参阅Stack Overflow的x86标记wiki中指南和文档的更多链接
Jus*_*tin 20
如果您使用NASM,命令行就是
nasm -felf32 -g -Fdwarf file.asm -o file.o
Run Code Online (Sandbox Code Playgroud)
其中'file.asm'是您的汇编文件(代码),'file.o'是您可以链接的对象文件gcc -m32或ld -melf_i386.(使用nasm -felf64将使用64位目标文件进行组装,但下面的hello world示例使用32位系统调用,并且不能在PIE可执行文件中使用.)
这是一些更多信息:
http://www.nasm.us/doc/nasmdoc2.html#section-2.1
您可以使用以下命令在Ubuntu中安装NASM:
apt-get install nasm
Run Code Online (Sandbox Code Playgroud)
这是Linux程序集中的基本Hello World,以满足您的胃口:
http://web.archive.org/web/20120822144129/http://www.cin.ufpe.br/~if817/arquivos/asmtut/index.html
我希望这就是你要问的......
小智 8
还有适用于Linux的FASM.
format ELF executable
segment readable executable
start:
mov eax, 4
mov ebx, 1
mov ecx, hello_msg
mov edx, hello_size
int 80h
mov eax, 1
mov ebx, 0
int 80h
segment readable writeable
hello_msg db "Hello World!",10,0
hello_size = $-hello_msg
Run Code Online (Sandbox Code Playgroud)
它汇集了
fasm hello.asm hello
Run Code Online (Sandbox Code Playgroud)
我的建议是从头开始编程:
http://nongnu.askapache.com/pgubook/ProgrammingGroundUp-1-0-booksize.pdf
这是在linux下进入汇编程序编程的一个非常好的起点,它解释了入门时需要了解的许多基础知识.