Ubuntu安裝NASM和簡單的使用教程


1. 安裝

sudo apt-get install nasm

這樣nasm就安裝好了,終端輸入命令:

nasm -version

輸出版本信息就說明安裝成功

2. 使用

創建"hello.asm"文件:

touch hello.asm
gedit hello.asm

在文件中輸入下面的匯編代碼

section .data
  hello:     db 'Hello world!',10    ; 'Hello world!' plus a linefeed character
  helloLen:  equ $-hello             ; Length of the 'Hello world!' string
                                     ; (I'll explain soon)
 
section .text
  global _start
 
_start:
  mov eax,4            ; The system call for write (sys_write)
  mov ebx,1            ; File descriptor 1 - standard output
  mov ecx,hello        ; Put the offset of hello in ecx
  mov edx,helloLen     ; helloLen is a constant, so we don't need to say
                       ;  mov edx,[helloLen] to get it's actual value
  int 80h              ; Call the kernel
 
  mov eax,1            ; The system call for exit (sys_exit)
  mov ebx,0            ; Exit with return code of 0 (no error)
  int 80h

保存后退出。
編譯

nasm -f elf64 hello.asm

如果是32位系統就把elf64改為elf32
鏈接

ld -s -o hello hello.o

運行

./hello

終端輸出“Hello,world!”就沒問題了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM