从头写一个操作系统 12

in #code5 years ago (edited)

你可能需要google: kernel, ELF format, makefile

目标: 写一个简单的kernel,让bootsect启动它

The kernel

我们用C语言写的内核只能做一点点事,就是在屏幕左上角打印一个'X',打开这个kernel.c

你会发现第一个函数中什么都没写,这个函数创建了指向main函数的内核入口。

i386-elf-gcc -ffreestanding -c kernel.c -o kernel.o

调用main的程序是 kernel_entry.asm。打开学习学习汇编中[extern]声明的用法。编译这个文件的elf格式生成kernel.o,注意,这次的最终文件不是.bin的机器码文件。

nasm kernel_entry.asm -f elf -o kernel_entry.o

The linker

链接器是非常有用的工具,我们刚才只使用到它一点点能力。

要链接两个.obj文件,并且解析标签,运行:

i386-elf-ld -o kernel.bin -Ttext 0x1000 kernel_entry.o kernel.o --oformat binary

注意,内核代码可不是被放在内存的0x0位置上,而是放在了0x1000,(译注:进入保护模式后会跳转到这个地址执行内核代码,指定地址后,链接器会将所有跳转(段内)指令的目标地址加上0x1000)。引导程序需要知道这个地址。这和lesson10中bootsect.asm很像。

The bootsector

编译它:
nasm bootsect.asm -f bin -o bootsect.bin

Putting it all together

现在我们分别由两个文件,bootsectokernel
可以直接link它们吗?哈哈,当然,把他们接在一起就可以:

cat bootsect.bin kernel.bin > os-image.bin

Run!

You can now run os-image.bin with qemu.
用qemu运行!

Remember that if you find disk load errors you may need to play with the disk numbers or qemu parameters (floppy = 0x0, hdd = 0x80). I usually use qemu-system-i386 -fda os-image.bin
注意,如果你遇到硬盘载入错误之类的情况,可能需要调整硬盘的编号或者qemu的参数,我通常这么做:
qemu-system-i386 -fda os-image.bin

如果成功,会看到如下信息:

  • "Started in 16-bit Real Mode"
  • "Loading kernel into memory"
  • (Top left) "Landed in 32-bit Protected Mode"
  • (Top left, overwriting previous message) "X"

Congratulations!

THE ORIGIN ARTICALE IN GITHUB:[^1]

Sort:  

Source
Copying/Pasting full or partial texts without adding anything original is frowned upon by the community. Repeated copy/paste posts could be considered spam. Spam is discouraged by the community, and may result in action from the cheetah bot.

More information and tips on sharing content.

If you believe this comment is in error, please contact us in #disputes on Discord

希望你还读得到我的留言。就是这些事,我才没有把你带过去。有时人怕出名猪怕肥。功课写得美,可能就有人眼红。我希望你还继续写,不过我觉得这个课题暂时搁置了比较好。

Posted using Partiko Android

嘿嘿,看到了!前几天是搞得我很烦,本来就想当个博客更新,谁知道天天有机器人给我留言,看来拉哥早就料到这些事所以才叮嘱我不要在乎点赞和留言。说实话,对这里的环境有点意外,不过也无所谓,有文章我继续发。跟我想的一样,这个课题暂时不在这里更新了,换个别的,可能是开发树莓派的系统之类的,我先准备两天

主要是,如果加了那些“团体”的标签,比较容易被那些眼红的家伙看到。所以我一直都是默默支持着,在找机会把你弄大。既然你回覆了,我还比较放心些。下次弄好了,艾特我一下。我一天看几百篇,有时可能会看漏。

Posted using Partiko Android

好的,那几天我也是莫名其妙,不知道哪里出了问题,这还真是个有意思的事呢,到哪都是江湖!
我不会消失的,这几天正在修炼内功,写出来第一时间一定艾特你!嘿嘿

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://github.com/cfenollosa/os-tutorial/blob/master/13-kernel-barebones/README.md

Congratulations @geyu! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!