#hello.s
.data # 数据段声明
msg : .string "Hello, world!\n" # 要输出的字符串
len = . - msg # 字串长度
.text # 代码段声明
.global _start # 指定入口函数
_start: # 在屏幕上显示一个字符串
movl $len, %edx # 参数三:字符串长度
movl $msg, %ecx # 参数二:要显示的字符串
movl $1, %ebx # 参数一:文件描述符(stdout)
movl $4, %eax # 系统调用号(sys_write)
int $0x80 # 调用内核功能
# 退出程序
movl $0,%ebx # 参数一:退出代码
movl $1,%eax # 系统调用号(sys_exit)
int $0x80 # 调用内核功能
控制台的操作:
wangck@ubuntu:~$ as -o hello.o hello.s
wangck@ubuntu:~$ ld -s -o hello hello.o
wangck@ubuntu:~$ ./hello
Hello, world!
wangck@ubuntu:~$ vim hello.o
wangck@ubuntu:~$ ld
ld: no input files
wangck@ubuntu:~$ objdump -s -d hello.o
hello.o: file format elf64-x86-64
Contents of section .text:
0000 ba100000 00b90000 0000bb01 000000b8 ................
0010 04000000 cd80bb00 000000b8 01000000 ................
0020 cd80 ..
Contents of section .data:
0000 48656c6c 6f2c2077 6f726c64 215c6e00 Hello, world!
.
Disassembly of section .text:
0000000000000000 <_start>:
0: ba 10 00 00 00 mov $0x10,%edx
5: b9 00 00 00 00 mov $0x0,%ecx
a: bb 01 00 00 00 mov $0x1,%ebx
f: b8 04 00 00 00 mov $0x4,%eax
14: cd 80 int $0x80
16: bb 00 00 00 00 mov $0x0,%ebx
1b: b8 01 00 00 00 mov $0x1,%eax
20: cd 80 int $0x80
wangck@ubuntu:~$