俺啥也不懂-不知道什么时候才会编写操作系统-再写Hello World-第一版makefile

俺啥也不懂-不知道什么时候才会编写操作系统-先写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

效果相同

俺啥也不懂-不知道什么时候才会编写操作系统-再写Hello World-第二版makefile

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值