OS自作入門をGASでやる 1日目

helloos0とhelloos1はすっ飛ばし,helloos2からやることにした.

コンパイル方法

# アセンブリをコンパイルする.
# -nostartfilesオプション:標準のスタートアップルーチンを使わない
# -nodefaltlibs:標準のライブラリを使わない
%gcc -nostartfiles -nodefaultlibs helloos2.s -o helloos2.elf
# このままではelf形式なので,生バイナリに変換
# -R section:sectionを削除
# -O bfdnameオプション:指定したバイナリフォーマットデータに変換
%objcopy -R .note.gnu.build-id -S -O binary helloos2.elf helloos2
# qemuで実行
# -fda img オプション:フロッピーディスクのイメージを指定する
% qemu-system-x86_64 -fda helloos2

nasm->gas変換表

nasm gas
DB 0x11 .byte 0x11
DW 0x1111 .word 0x1111
DD 0x11111111 .long 0x11111111
RESB 100 .org .+100
DB "String" .ascii "String"

ソースコード

# helloos2
# GNU Assembler 版

# ELF用エントリポイントをグローバル宣言
.globl start

# 0番地開始にする.
.org 0

# 一旦ELFファイルにコンパイルするためエントリポイントを記述(使わない)
start:

# FAT32フロッピーのための記述
.byte 0xeb, 0x4e, 0x90
.ascii "HELLOIPL"
.word 512
.byte 1
.word 1
.byte 2
.word 224
.word 2880
.byte 0xf0
.word 9
.word 18
.word 2
.long 0
.long 2880
.byte 0,0,0x29
.long 0xffffffff
.ascii "HELLO-OS   "
.ascii "FAT12   "
.org .+18

# プログラム本体
.byte 0xb8, 0x00, 0x00, 0x8e, 0xd0, 0xbc, 0x00, 0x7c
.byte 0x8e, 0xd8, 0x8e, 0xc0, 0xbe, 0x74, 0x7c, 0x8a
.byte 0x04, 0x83, 0xc6, 0x01, 0x3c, 0x00, 0x74, 0x09
.byte 0xb4, 0x0e, 0xbb, 0x0f, 0x00, 0xcd, 0x10, 0xeb
.byte 0xee, 0xf4, 0xeb, 0xfd

# メッセージ部分
.byte 0x0a, 0x0a
.ascii "hello, world"
.byte 0x0a
.byte 0

.org 0x1fe # 0x001feまで飛ばす

.byte 0x55, 0xaa

# ブートセクタ以外の部分の記述
.byte 0xf0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00
.org .+4600
.byte 0xf0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00
.org .+1469432