nasm安裝與helloworld

1.    下載源碼包,例如最新的 nasm-2.11.06.tar.gz //http://www.nasm.us/pub/nasm/releasebuilds/2.11.06/

2.    解壓,並在終端下進入安裝包文件夾

3.    如果當前OS沒有安裝編程系列工具軟件,則需 sudo apt-get build-dep ”軟件名",此步驟是爲了下載編譯環境所需要的一些文檔和工具軟件。

4.   ./configure

5.    make

6.    sudo make install

編譯鏈接linux下的helloworld彙編程序,注意這裏用的是nasm,這個彙編語法和AT&T彙編有較大區別,當然nasm是跨越平臺的,不同於windows下的PE文件格式,LINUX下的可執行文件格式是elf。

  編譯時:nasm -f elf myfile.asm
  將asm文件編譯成myfile.o(obj文件);

  nasm -f bin myfile.asm -o myfile.com 
  把文件'myfile.asm'彙編成純二進制格式的文件'myfile.com';

  nasm -f coff myfile.asm -l myfile.lst 
  以十六進制代碼的形式產生列表文件輸出,並讓代碼顯示在源代碼的左側, 
  使用'-l'選項並給出列表文件名
之後,我們便可以鏈接生成可執行文件
   ld -s hello.o -o hello
  連接成可執行的elf文件,當然此時我們也可直接用gcc來鏈接:gcc hello.o    //鏈接生成默認文件名爲a.out的的可執行文件

之後,可以用objdump -d  hello反彙編查看結果:(當然是gcc彙編格式)


最後給出nasm語法的hellworld:

;  hello.asm  a first program for nasm for Linux, Intel, gcc
;
; assemble:	nasm -f elf hello.asm -o hello.o
; link:		gcc -o hello  hello.o
; run:	        hello 
; output is:	Hello World 

	SECTION .data		; data section
msg:	db "Hello World",10	; the string to print, 10=cr
len:	equ $-msg		; "$" means "here"
				; len is a value, not an address

	SECTION .text		; code section
        global main		; make label available to linker 
main:				; standard  gcc  entry point
	
	mov	edx,len		; arg3, length of string to print
	mov	ecx,msg		; arg2, pointer to string
	mov	ebx,1		; arg1, where to write, screen
	mov	eax,4		; write command to int 80 hex
	int	0x80		; interrupt 80 hex, call kernel
	
	mov	ebx,0		; exit code, 0=normal
	mov	eax,1		; exit command to kernel
	int	0x80		; interrupt 80 hex, call kernel


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章