アセンブリ言語
命令 | |
---|---|
ORG |
プログラムの読み込み先メモリを指定(このプログラム中のメモリ位置の原点) |
JE |
CMPの結果が等しかったらジャンプ(jump if equal) |
INT |
割り込み。BIOS内の関数を呼ぶ |
HLT |
CPUを待機状態にさせる。入力装置の操作で解除する |
1 | MOV BYTE [123] 456 |
レジスタ
レジスタ名 | |
---|---|
AX |
アキュムレータ |
CX |
カウンタ |
DX |
データ |
BX |
ベース |
SP |
スタックポインタ |
BP |
ベースポインタ |
SI |
ソースインデックス |
DI |
デスティネーションインデックス |
ES |
エクストラセグメント |
CS |
コードセグメント |
SS |
スタックセグメント |
DS |
データセグメント |
FS |
おまけ |
GS |
おまけ |
アセンブリを使って書き直す(02_day/helloos3)
1 | DB 0xb8, 0x00, 0x00, 0x8e, 0xd0, 0xbc, 0x00, 0x7c |
これを人間が読めるようにすると・・・
1 | entry: |
C言語でなんとなく書いてみるとこんな感じ。レジスタや関数呼び出しはテキトウなので動かない。
1 | char* msg = "hello, world"; |
HLTの効果(02_day/helloos3)
あれっ!no bootable device!?
58行目を編集して
1 | ;RESB 0x7dfe-$ |
とりあえずいつもどおりアセンブルして起動しようとすると、プードデバイスがないと言われた。

プーとセクタのバグっぽい。その原因は
1 | ORG 0x7c00 |
プログラム全体の基準を決めてしまっているので58行目を相対アドレスにしなければならない。著者作のnaskだとなぜ動くのか不明だが、修正しておく。0x7dfe - 0x7c00 = 0x01fe
より、
1 | RESB 0x01fe-($-$$) |
これで正常に動いた。
HLTなし
タスクマネージャを見るとCPUの第14スレッドが使用率100%になっていた。

HLTあり

HLTの効果はたしかにあるようだ。
ブートセクタだけを作るようにする
今後の拡張のために、ブートセクタとOS本体のコードを切り分けて、あとからまとめるようにするらしい。
本書では著者作のedimg.exe
を使用しているが、甘えずに一般的なやり方を探す。
やっていることは本章では空のディスクイメージを作成し、そこにアセンブルしたブートセクタを書き込んでいるようだ。
どうやらmtoolsにあるmformatコマンドでイメージファイルを作成できるらしい。
https://www.gnu.org/software/mtools/manual/mtools.html
4.13 Mformat
mformat [-t cylinders|-T tot_sectors] [-h heads] [-s sectors]
[-f size] [-1] [-4] [-8]
[-v volume_label]
[-F] [-S sizecode]
[-M software_sector_size]
[-N serial_number] [-a]
[-C] [-H hidden_sectors] [-I fsVersion]
[-r root_sectors] [-L fat_len]
[-B boot_sector] [-k]
[-m media_descriptor]
[-K backup_boot]
[-R nb_reserved_sectors]
[-c clusters_per_sector]
[-d fat_copies]
[-X] [-2 sectors_on_track_0] [-3]
[-0 rate_on_track_0] [-A rate_on_other_tracks]
drive:
The following options are supported:
f
Specifies the size of the DOS file system to format. Only a certain number of predefined sizes are supported by this flag; for others use the -h/-t/-s flags. The following sizes are supported:
1440
1440K, double-sided, 18 sectors per track, 80 cylinders (for 3 1/2 HD)
C
creates the disk image file to install the MS-DOS file system on it. Obviously, this is useless on physical devices such as floppies and hard disk partitions, but is interesting for image files.
B
Use the boot sector stored in the given file or device, instead of using its own. Only the geometry fields are updated to match the target disks parameters.
また、フォーマット対象のドライブを指定するには-i
オプションを使用すれば良いと書いてある。
2.2 Drive letters
The drive letter : (colon) has a special meaning. It is used to access image files which are directly specified on the command line using the -i options.
よくわからなかったので、mtoolsのソースコードを見てみると、
1 | // config.c |
のようになっている。デフォルトのドライブレターが:
なので、-i
オプションのオプションは::
。
1 | -i <image-file> <drive> |
よって、
1 | mformat -f 1440 -C -B ipl.bin -i helloos.img :: |
よって、これらをMakefileにすると、
1 | ipl: |