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


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

failwest9527

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值