2日目

とりあえずhelloos.nasに相当するhelloos.Sを作成。
最初なんで全部掲載しときます。

#define INIT_SEG 0x07c0

.file "helloos.S"

.code16gcc

.section .rodata
msg1:	
	.string "hello,world"

.text
	ljmp $INIT_SEG, $main
main:
	cli
	movw $INIT_SEG, %ax
	movw %ax, %ds
	movw %ax, %es
	movw %ax, %sp
	movw $0, %ax
	movw %ax, %ss
	sti
	
	movl $msg1, %esi
putloop:
	movzbl (%esi), %eax
	cmpb $0, %al
	je fin
	incl %esi
	movb $0x0e, %ah
	movl $7, %ebx
	int $0x10
	jmp putloop

fin:
	hlt
	jmp fin	

.section .sign, "a"
sign:	.word 0xaa55

Interface誌2002年7月号(以下Interface誌)を参考にしながら書いたせいでいきなりljmpしたり、セグメントの設定値がhelloos.nasと異ってたりしますが、後ほど修正します。


セクション.signはブートシグネチャ用ですが、gasが認識しないセクションなので"a"(section is allocatable)の指定がないと無視されていまいます。
(gasが認識するセクション(.text等)に関してはFLAGSを省略したときの挙動はセクション名に依存するようですが、gasが認識しないセクションでFLAGSが省略されたときはFLAGSがないものと理解(要は無視)されるようです。)


リンカスクリプトhelloos.lsはこんな感じ。

OUTPUT_FORMAT("binary")			/* We want raw binary image */
OUTPUT_ARCH(i386)

/* Define memory layout */

MEMORY {
  body : org =   0, len = 510
  sign : org = 510, len = 2
 }

/* Specify input and output sections */

SECTIONS {
  .text   : { *(.text) }   > body	/* Executable codes */
  .data   : { *(.data) }   > body	/* Initialized data */
  .bss    : { *(.bss) }    > body	/* Uninitialized data */
  .rodata : { *(.rodata*) } > body	/* Constant data (R/O) */
  .sign   : { *(.sign) }  > sign	/* Boot signature */
}

ここもInterface誌を参考(というかこれに関しては丸写し)にしてます。
以下のコマンドでアセンブル&リンク。

$ gcc -fno-common -Wall -O2 -c -finhibit-size-directive -fno-ident helloos.S
$ ld -T helloos.ls -o helloos helloos.o -Map helloos.map --cre
$ dd if=/dev/zero of=helloos.bin bs=512 count=2880 &> /dev/null
$ dd if=helloos of=helloos.bin bs=512 count=1 &> /dev/null

あとはできたイメージ(helloos.bin)をbochs等のFDイメージとして起動してやれば無事"hello, world"が出力されるはず。