在 Linux 中编译/运行汇编程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3314919/
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
Compile/run assembler in Linux?
提问by Rafe Kettler
I'm fairly new to Linux (Ubuntu 10.04) and a total novice to assembler. I was following some tutorials and I couldn't find anything specific to Linux. So, my question is, what is a good package to compile/run assembler and what are the command line commands to compile/run for that package?
我对 Linux (Ubuntu 10.04) 相当陌生,并且是汇编程序的新手。我正在学习一些教程,但找不到任何特定于 Linux 的内容。所以,我的问题是,编译/运行汇编程序的好包是什么,为该包编译/运行的命令行命令是什么?
采纳答案by George
The GNU assembler (gas) and NASM are both good choices. However, they have some differences, the big one being the order you put operations and their operands.
GNU 汇编器(gas)和 NASM 都是不错的选择。但是,它们有一些区别,最大的区别是您放置操作及其操作数的顺序。
gas uses AT&T syntax (guide: https://stackoverflow.com/tags/att/info):
gas 使用 AT&T 语法(指南:https: //stackoverflow.com/tags/att/info):
mnemonic source, destination
nasm uses Intel style (guide: https://stackoverflow.com/tags/intel-syntax/info):
nasm 使用 Intel 风格(指南:https: //stackoverflow.com/tags/intel-syntax/info):
mnemonic destination, source
Either one will probably do what you need. GAS also has an Intel-syntax mode, which is a lot like MASM, not NASM.
任何一个都可能会做你需要的。GAS 也有一个 Intel 语法模式,它很像 MASM,而不是 NASM。
Try out this tutorial: http://asm.sourceforge.net/intro/Assembly-Intro.html
试试这个教程:http: //asm.sourceforge.net/intro/Assembly-Intro.html
See also more links to guides and docs in Stack Overflow's x86 tag wiki
另请参阅 Stack Overflow 的x86 标记 wiki 中指向指南和文档的更多链接
回答by Recursion
The assembler(GNU) is as(1)
汇编程序(GNU)是作为(1)
回答by Justin
If you are using NASM, the command-line is just
如果您使用的是 NASM,则命令行只是
nasm -felf32 -g -Fdwarf file.asm -o file.o
where 'file.asm' is your assembly file (code) and 'file.o' is an object file you can link with gcc -m32
or ld -melf_i386
. (Assembling with nasm -felf64
will make a 64-bit object file, but the hello world example below uses 32-bit system calls, and won't work in a PIE executable.)
其中“file.asm”是您的程序集文件(代码),“file.o”是您可以使用gcc -m32
或链接的目标文件ld -melf_i386
。(与 with 组合nasm -felf64
将生成 64 位目标文件,但下面的 hello world 示例使用 32 位系统调用,并且无法在 PIE 可执行文件中工作。)
Here is some more info:
以下是更多信息:
http://www.nasm.us/doc/nasmdoc2.html#section-2.1
http://www.nasm.us/doc/nasmdoc2.html#section-2.1
You can install NASM in Ubuntu with the following command:
您可以使用以下命令在 Ubuntu 中安装 NASM:
apt-get install nasm
Here is a basic Hello World in Linux assembly to whet your appetite:
以下是 Linux 汇编中的基本 Hello World,可以激发您的胃口:
http://web.archive.org/web/20120822144129/http://www.cin.ufpe.br/~if817/arquivos/asmtut/index.html
http://web.archive.org/web/20120822144129/http://www.cin.ufpe.br/~if817/arquivos/asmtut/index.html
I hope this is what you were asking...
我希望这就是你要问的...
回答by Jay Conrod
The GNU assembler is probably already installed on your system. Try man as
to see full usage information. You can use as
to compile individual files and ld to link if you really, really want to.
GNU 汇编器可能已经安装在您的系统上。尝试man as
查看完整的使用信息。as
如果您真的非常想要,您可以使用编译单个文件和 ld 进行链接。
However, GCC makes a great front-end. It can assemble .s files for you. For example:
然而,GCC 是一个很棒的前端。它可以为您组装 .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 $ cat >hello.c <<EOF
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}
EOF
$ gcc -S hello.c -o hello.s
, %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!
The code above is x86-64. If you want to make a position-independent executable (PIE), you'd need lea hello(%rip), %rdi
, and call puts@plt
.
上面的代码是 x86-64。如果要制作与位置无关的可执行文件 (PIE),则需要lea hello(%rip), %rdi
, 和call puts@plt
.
A non-PIE executable (position-dependent) can use 32-bit absolute addressing for static data, but a PIE should use RIP-relative LEA. (See also Difference between movq and movabsq in x86-64neither movq
nor movabsq
are a good choice.)
非 PIE 可执行文件(位置相关)可以对静态数据使用 32 位绝对寻址,但 PIE 应该使用 RIP 相对 LEA。(另请参阅x86-64 中 movq 和 movabsq 之间的差异既不是movq
也不movabsq
是一个好的选择。)
If you wanted to write 32-bit code, the calling convention is different, and RIP-relative addressing isn't available. (So you'd push $hello
before the call, and pop the stack args after.)
如果您想编写 32 位代码,调用约定是不同的,并且 RIP 相对寻址不可用。(所以你push $hello
在调用之前,然后弹出堆栈参数。)
You can also compile C/C++ code directly to assembly if you're curious how something works:
如果您对某些东西的工作原理感到好奇,您还可以将 C/C++ 代码直接编译为程序集:
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
See also How to remove "noise" from GCC/clang assembly output?for more about looking at compiler output, and writing useful small functions that will compile to interesting output.
另请参阅如何从 GCC/clang 程序集输出中去除“噪音”?有关查看编译器输出以及编写将编译为有趣输出的有用小函数的更多信息。
回答by Artur Artamonov
There is also FASM for Linux.
还有适用于 Linux 的 FASM。
fasm hello.asm hello
It comiles with
它与
nasm -f elf64 example.asm # assemble the program
ld -s -o example example.o # link the object file nasm produced into an executable file
./example # example is an executable file
回答by gilligan
My suggestion would be to get the book Programming From Ground Up:
我的建议是从头开始编程这本书:
http://nongnu.askapache.com/pgubook/ProgrammingGroundUp-1-0-booksize.pdf
http://nongnu.askapache.com/pgubook/ProgrammingGroundUp-1-0-booksize.pdf
That is a very good starting point for getting into assembler programming under linux and it explains a lot of the basics you need to understand to get started.
这是在 linux 下进行汇编程序编程的一个很好的起点,它解释了许多入门所需的基础知识。
回答by plan9assembler
3 syntax (nasm, tasm, gas ) in 1 assembler, yasm.
1 个汇编器中的 3 个语法(nasm、tasm、gas),yasm。