1. 程式人生 > >CentOS7寫匯編並編譯運行匯編代碼

CentOS7寫匯編並編譯運行匯編代碼

str 配置 call try rri fir use 下載地址 輸出

1.下載nasm編譯器

  下載地址是https://www.nasm.us/pub/nasm/releasebuilds/

wget https://www.nasm.us/pub/nasm/releasebuilds/2.14/nasm-2.14.tar.gz

2.解壓安裝nasm

tar -xvzf nasm-2.14.tar.gz 

3.進入到nasm的解壓目錄中編譯並安裝nasm

cd nasm-2.14         #進入nasm的解壓目錄

./configure             #配置

make       #編譯

make install   #安裝
    

4.一段可以輸出Hello World的匯編代碼

section .data                           ;section declaration
msg     db      "Hello, world!",0xA     ;our dear string
len     equ     $ - msg                 ;length of our dear string
section .text                           ;section declaration
                       ;we must export the entry point to the ELF linker or
   global _start       ;loader. They conventionally recognize _start as their
                       ;entry point. Use 
ld -e foo to override the default. _start: ;write our string to stdout mov eax,4 ;system call number (sys_write) mov ebx,1 ;first argument: file handle (stdout) mov ecx,msg ;second argument: pointer to message to write mov edx,len ;third argument: message length
int 0x80 ;call kernel ;and exit mov eax,1 ;system call number (sys_exit) xor ebx,ebx ;first syscall argument: exit code int 0x80 ;call kernel

  將它保存為HelloWorld.s文件。

5.編譯該匯編代碼

nasm -f elf64 HelloWorld.s -o HelloWorld.o

6.鏈接生成可執行文件

ld -s HelloWorld.o -o HelloWorld.out

7.執行程序

./HelloWorld.out

8.執行結果如下

[root@CentOs64-7 Assembly]# ./HelloWorld.out 
Hello, world!

  轉載自:https://blog.csdn.net/weiyuanzhuo/article/details/52382611

CentOS7寫匯編並編譯運行匯編代碼