mozyのかきおき

mozyの読書感想文や思考置き場

30日でできるOS自作入門【12日目】

f:id:mozy_ok:20181130220523p:plain

概要

これはMozyの1人アドベントカレンダー 2018 の12日目の記事です。

adventar.org

書こうとした経緯は、こちら

mozy-ok.hatenablog.com

今日の内容

  • ついに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
  • bootpack.gasからbootpack.nasを作る
    bootpack.nas
[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を作る

    f:id:mozy_ok:20181212233956p:plain
    bootpack.obj

  • bootpack.objからbootpack.bimを作る

    f:id:mozy_ok:20181212234020p:plain
    bootpack.bim

  • bootpack.bimからbootpack.hrbを作る

    f:id:mozy_ok:20181212234043p:plain
    bootpack.hrb

  • これで機械語になったのでcopyコマンドでasmhead.binとbootpack.hrbをくっつけて

  • haribote.sysとする
    f:id:mozy_ok:20181212234127p:plain
    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.

真っ黒ー。起動はした。

f:id:mozy_ok:20181212234212p:plain
make run結果

ハマりポイント

それぞれの機械語の意味や変換していく部分を追っていったらこんな時間になってしまった。

知ったこと

C言語アセンブラ、バイナリ、機械語、オブジェクトファイル、イメージファイル。たくさん出てきてどんどんごちゃごちゃになってきた。。
明日はここら辺を整理して進めたい。