30日でできるOS自作入門【12日目】
概要
これはMozyの1人アドベントカレンダー 2018 の12日目の記事です。
書こうとした経緯は、こちら
今日の内容
- ついにC言語の世界に突入した
今日やったこと
- bootに関する部分を作る
bootpack.c
void HariMain(void) { fin: /* ここにHLTを入れたいが、C言語ではHLTが使えない */ goto fin; }
機械語への変化のながれ
- bootpack.cからbootpack.gasを作る
bootpack.gas
.file "bootpack.c" .text .balign 2 .globl _HariMain .def _HariMain; .scl 2; .type 32; .endef _HariMain: pushl %ebp movl %esp, %ebp L2: jmp L2
[FORMAT "WCOFF"] [INSTRSET "i486p"] [OPTIMIZE 1] [OPTION 1] [BITS 32] [FILE "bootpack.c"] [SECTION .text] GLOBAL _HariMain _HariMain: PUSH EBP MOV EBP,ESP L2: JMP L2
bootpack.nasからbootpack.objを作る
bootpack.objからbootpack.bimを作る
bootpack.bimからbootpack.hrbを作る
これで機械語になったのでcopyコマンドでasmhead.binとbootpack.hrbをくっつけて
- haribote.sysとする
実行結果
$ make run make -r img make -r haribote.img ../../z_tools/nask ipl10.nas ipl10.bin ipl10.lst ../../z_tools/nask asmhead.nas asmhead.bin asmhead.lst ../../z_tools/gocc1 -I../../z_tools/haribote/ -Os -Wall -quiet -o bootpack.gas bootpack.c ../../z_tools/gas2nask -a bootpack.gas bootpack.nas ../../z_tools/nask bootpack.nas bootpack.obj bootpack.lst ../../z_tools/obj2bim @../../z_tools/haribote/haribote.rul out:bootpack.bim stack:3136k map:bootpack.map \ bootpack.obj ../../z_tools/bim2hrb bootpack.bim bootpack.hrb 0 cat asmhead.bin bootpack.hrb > haribote.sys ../../z_tools/edimg imgin:../../z_tools/fdimg0at.tek \ wbinimg src:ipl10.bin len:512 from:0 to:0 \ copy from:haribote.sys to:@: \ imgout:haribote.img cp haribote.img ../../z_tools/qemu/fdimage0.bin make -r -C ../../z_tools/qemu qemu-system-i386 -L . -m 32 -localtime -vga std -fda fdimage0.bin qemu-system-i386: -localtime: warning: This option is deprecated, use '-rtc base=localtime' instead. WARNING: Image format was not specified for 'fdimage0.bin' and probing guessed raw. Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. Specify the 'raw' format explicitly to remove the restrictions.
真っ黒ー。起動はした。
ハマりポイント
それぞれの機械語の意味や変換していく部分を追っていったらこんな時間になってしまった。
知ったこと
C言語、アセンブラ、バイナリ、機械語、オブジェクトファイル、イメージファイル。たくさん出てきてどんどんごちゃごちゃになってきた。。
明日はここら辺を整理して進めたい。