1. Hello World 級的引導代碼

來源:互聯網
上載者:User
上一節已經搭建好了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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.