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