NASM汇编:NASM 版 Hello World

转载:http://sleepycat.org/tech/os/nasm-hello-world


目录:

  • 本文简介
  • 1 代码及注释
  • 2 int 80h ( Interrupt 80h )
  • 3 $ 与 $$
  • 4 关于 label
  • 5 关于 db/dw/dd/dq/dt... 及 RESB/RESW/RESD...
  • 6 参考文档

本文简介

1 代码及注释

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
section .data
     msg:     db   "Hello World!!", 0ah, 0dh ;注:0ah(ASCII码:换行符),odh(ASCII码:回车符)
     msglen:  equ  $ - msg                   ;注: msg 标志的是字符串 "Hello World!!",0ah,0dh 的起始位置。
                                             ;    $ 则表示当前行的内存位置。$ - msg 于是即为 msg 的长度。
 
section .text
     global main
 
main:
     mov eax, 4    ; 80h 中断:函数,4 对应 sys_write
     mov ebx, 1    ; 80h 中断:函数第一个参数, File descriptor 1 - standard output
     mov ecx, msg  ; 80h 中断:函数第二个参数,要输出的字符串的偏移地址
     mov edx, 14   ; 80h 中断:函数第三个参数,字符串长度
     int 80h
     
     mov eax, 1   ; The exit syscall number
     mov ebx, 0   ; Have an exit code of 0
     int 80h

2 int 80h ( Interrupt 80h )

int 80h 是 linux system call. 其预备工作如下:

eax 放置函数
ebx 第一个参数
ecx 第二个参数
edx 第三个参数

不同的函数及其参数,请参考:Linux System Call Table

3 $ 与 $$

$ => 当前行被编译后的地址

$$ => 当前 section 被编译后的首行地址

$ - $$ => 表示本行距离本section开始处的相对长度

4 关于 label

message: db ’hello, world’
message  db ’hello, world'
问:区别? 
答:没有区别,完全一样。message 返回的是字符串的开始地址。
(The colon after a label is also optional. )

5 关于 db/dw/dd/dq/dt... 及 RESB/RESW/RESD...

db = declaring byte
dw = declaring word
dd = declaring double words
dq = declaring quadruple words
dt = declaring ten words

RESB = reserving space byte
RESW = reserving space word
RESD = reserving space double words

6 参考文档

http://docs.cs.up.ac.za/programming/asm/derick_tut/
http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值