簡體   English   中英

在Linux中編譯/運行匯編程序?

[英]Compile/run assembler in Linux?

我是Linux的新手(Ubuntu 10.04),也是匯編程序的新手。 我正在學習一些教程,但我找不到任何特定於Linux的內容。 所以,我的問題是,什么是編譯/運行匯編程序的好包,以及為該程序包編譯/運行的命令行命令是什么?

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!

上面的代碼是x86-64。 如果你想創建一個與位置無關的可執行文件(PIE),你需要lea hello(%rip), %rdicall puts@plt

非PIE可執行文件(位置相關 )可以對靜態數據使用32位絕對尋址,但PIE應使用RIP相對LEA。 (參見MOVQ和movabsq之間差異的x86-64既不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

另請參見如何從GCC /鏗鏘聲組件輸出中刪除“噪音”? 有關查看編譯器輸出以及編寫將編譯為有趣輸出的有用小函數的更多信息。

GNU匯編程序(gas)和NASM都是不錯的選擇。 但是,它們有一些差異,最重要的是你操作的順序和操作數。

gas使用AT&T語法(指南: https//stackoverflow.com/tags/att/info ):

mnemonic    source, destination

nasm使用英特爾風格(指南: https//stackoverflow.com/tags/intel-syntax/info ):

mnemonic    destination, source

任何一個人都可能做你需要的。 GAS還有一個Intel語法模式,它很像MASM,而不是NASM。


試試這個教程: http//asm.sourceforge.net/intro/Assembly-Intro.html

另請參閱Stack Overflow的x86標記wiki中指南和文檔的更多鏈接

如果您使用NASM,命令行就是

nasm -felf32 -g -Fdwarf file.asm -o file.o

其中'file.asm'是您的匯編文件(代碼),'file.o'是一個目標文件,您可以使用gcc -m32ld -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

這是Linux程序集中的基本Hello World,以滿足您的胃口:

http://web.archive.org/web/20120822144129/http://www.cin.ufpe.br/~if817/arquivos/asmtut/index.html

我希望這就是你要問的......

還有適用於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

它匯集了

fasm hello.asm hello

我的建議是從頭開始編程:

http://nongnu.askapache.com/pgubook/ProgrammingGroundUp-1-0-booksize.pdf

這是在linux下進入匯編程序編程的一個非常好的起點,它解釋了入門時需要了解的許多基礎知識。

匯編程序(GNU) 如(1)

1個匯編語言中的3個語法(nasm,tasm,gas),yasm。

http://www.tortall.net/projects/yasm/

對於Ubuntu 18.04安裝nasm 打開終端並輸入:

sudo apt install as31 nasm

nasm docs

用於編譯和運行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM