俺啥也不懂-不知道什么时候才会编写操作系统-先写Hello World
可能还要看一下gcc把
先贴上上一篇的Hello World,可以在centos下将以下代码保存为hello.asm
section .data
msg:
db "hello, world", 10
len equ $-msg
section .text
global main
main:
mov edx, len
mov ecx, msg
mov ebx, 1
mov eax, 4 ;直接使用sys_write系统调用
int 0x80
mov ebx, 0
mov eax, 1
int 0x80
俺们如何将其编译成可执行文件?使用makefile
文件夹目录结构
--任意文件夹
----hello.asm
----makefile
第一版的makefile:
buildo:
nasm -f elf64 hello.asm #这里必须使用tab开始这一行
buildExecute:
gcc -g -o main hello.o
执行命令如下:
[root@localhost test]# ls
hello.asm makefile
[root@localhost test]# make buildo
nasm -f elf64 hello.asm #这里必须使用tab开始这一行
[root@localhost test]# ls
hello.asm hello.o makefile
[root@localhost test]# make buildExecute
gcc -g -o main hello.o
[root@localhost test]# ls
hello.asm hello.o main makefile
[root@localhost test]# ./main
hello, world
[root@localhost test]#
makefile通常情况下是按照书写顺序执行的,除非遇到引用,所以,如果单独执行make命令,按照如上makefile的写法,相当于执行:make buildo
据说,linux下的可执行文件格式为elf,那么要想将asm生成可执行的文件,就需要上面的两步,nasm -f elf64负责编译,gcc -o负责链接,-g好像是多写了,是为gdb调试用的。
执行make buildExecute之后,就可以直接执行main文件了,执行./main,输出hello,world
当然,如此简单的命令,还要写一个makefile文件有点小题大做,可以直接执行命令
[root@localhost test]# ls
hello.asm makefile
[root@localhost test]# nasm -f elf64 hello.asm
[root@localhost test]# ls
hello.asm hello.o makefile
[root@localhost test]# gcc -o main hello.o
[root@localhost test]# ls
hello.asm hello.o main makefile
[root@localhost test]# ./main
hello, world
效果相同