上一節已經搭建好了OS開發環境,現在我們來寫個簡單的引導代碼吧
先講一下作業系統的引導過程:
PC加電自檢後,會把引導裝置的0磁頭,0磁軌,1扇區的內容載入到記憶體 0x7c00 處,然後就跳轉到該處執行引導代碼。我只是簡單地說了下,詳情請自行Google。boot.S
1 .code16
2 .text
3 .globl _start:
4 _start:
5 cli
6 ljmp $0x7c00, $0
7 go:
8 movw %cs, %ax
9 movw %ax, %ds
10 movw %ax, %ss
11 movw $0xff00, %sp
12 sti
13
14 movw $0x0007, %bx
15 movw $16, %cx
16 movw $0, %dx
17 movw $msg, %bp
18 movw $0x1301, %ax
19 int $0x10
20
21 jmp .
22 msg: .ascii "Hello, OS World!"
23 .org 510
24 .word 0xaa55
boot.S採用的是AT&T格式的彙編文法,執行的工作就是先設定好相關段寄存器的值,再調用10h中斷顯示字串
為了把它載入到bochs,我們還要用make將其編譯為純二進位代碼,並且寫好相應的設定檔Makefile
1 AS=as
2 LD=ld
3 OBJCOPY=objcopy
4
5 all: floppy.img
6
7 floppy.img: boot
8 dd if=/dev/zero/ of=floppy.img bs=1024 count=1440
9 dd if=boot of=floppy.img bs=512 count=1 conv=notrunc
10
11 boot: boot.o
12 $(LD) -N -e _start -Ttext 0 -o $@.elf $^
13 $(OBJCOPY) -S -O binary $@.elf $@
14
15 boot.o: boot.S
16 $(AS) -o $@ $^
17
18 clean:
19 rm -rf *.o *.img *.elf bochs.log boot
bochs設定檔的解釋請Google 1 megs: 32
2 romimage: file=$BXSHARE/BIOS-bochs-latest #, address=0xf0000
3 vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest
4 floppya: 1_44=floppy.img, status=inserted
5 boot: a