转载: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 参考文档
本文简介
- 概要:NASM 版 Hello World 详细说明及注释
- 版本:Linux Mint 13(Ubuntu 11.10), nasm v2.09.08, gcc v4.6.1,
- 日期:2012-07-22
- 永久链接:http://sleepycat.org/tech/os/nasm-hello-world
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