[原]linux下bochs創(chuàng)建最簡(jiǎn)單的OS(一)
2009-05-21 10:31:55
分類(lèi): LINUX
作者:帥得不敢出門(mén) C++愛(ài)好者灌水天堂群 3503799 轉(zhuǎn)載請(qǐng)保留此信息
我的系統(tǒng)為redhat9
首先去bochs的官網(wǎng) http://bochs./ 下載最新的程序,,我直接下的是bochs-2.4.tar.gz tar zxvf bochs-2.4.tar.gz ./configure 如果要調(diào)試功能的話(huà),,可查看configure文件做相應(yīng)調(diào)整 --enable-debugger compile in support for Bochs internal debugger --enable-disasm compile in support for disassembler --enable-debugger-gui compile in support for Bochs internal debugger GUI --with-sdl use SDL libraries 網(wǎng)上說(shuō)要加最后一個(gè)暫時(shí)沒(méi)有明白其意。 make ./bochs 運(yùn)行
BOCHS運(yùn)行需要.bochsrc文件,在LINUX下以.開(kāi)頭的文件都是隱藏文件
l. 可查看這些文件,。 vi .bochsrc # how much memory the emulated machine will have megs: 32
# filename of ROM images
romimage:file=$BXSHARE/BIOS-bochs-latest #注意上面這一行,,2.3.5以后的后面不能加,address=0xf0000 否則會(huì)出現(xiàn) #Message: ROM: System BIOS must end at 0xfffff 錯(cuò)誤的 這個(gè)后面會(huì)解釋 #romimage: file=mybios.bin, address=0xfff80000 # 512k at memory top
vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest
# what disk images will be used floppya: 1_44=hello.img, status=inserted
#ata0-master: type=disk, mode=flat, path="30M.sample"
# hard disk
#ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 #ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15 #ata2: enabled=0, ioaddr1=0x1e8, ioaddr2=0x3e0, irq=11 #ata3: enabled=0, ioaddr1=0x168, ioaddr2=0x360, irq=9
boot: floppy
#boot: disk
megs: 32 是說(shuō)內(nèi)存是32M
floppya: 1_44=a.img, status=inserted 指定了軟盤(pán)映像文件 boot: floppy 從軟盤(pán)啟動(dòng)。
從http://bochs./下載nasm
我下的是nasm-2.05.01.tar.gz tar zxvf nasm-2.05.01.tar.gz cd nasm-2.05.01 ./configure make make install
mkdir helloworld
cd helloworld vi hello.asm ;hello.asm org 07c00h LABEL_START: mov ax,cs mov ds,ax mov es,ax mov ax,0b800h mov gs,ax mov ah,0ch mov al,'H' mov [gs:0],ax jmp $
./nasm ./helloworld/hello.asm -o ./helloworld/hello.bin
進(jìn)入bochs目錄
./bximage ======================================================================== bximage Disk Image Creation Tool for Bochs $Id: bximage.c,v 1.34 2009/04/14 09:45:22 sshwarts Exp $ ======================================================================== Do you want to create a floppy disk image or a hard disk image? Please type hd or fd. [hd] fd Choose the size of floppy disk image to create, in megabytes. Please type 0.16, 0.18, 0.32, 0.36, 0.72, 1.2, 1.44, 1.68, 1.72, or 2.88. [1.44] I will create a floppy image with cyl=80 heads=2 sectors per track=18 total sectors=2880 total bytes=1474560 What should I name the image? [a.img] hello.img Writing: [] Done. I wrote 1474560 bytes to hello.img. The following line should appear in your bochsrc: floppya: image="hello.img", status=inserted
把bin寫(xiě)入img中
dd if=/nasm-2.05.01/helloworld/hello.bin of=hello.img bs=512 count=1 conv=notrunc 讀入了 0+1 個(gè)塊 輸出了 0+1 個(gè)塊
bochs -q -f .bochsrc 運(yùn)行我們的OS
出現(xiàn)如下錯(cuò)誤 Bochs is exiting with the following message: [BIOS ] No bootable device.
查看生成的hello.bin大小
ls -al hello.bin -rw-r--r-- 1 root 900 22 5月 20 11:45 hello.bin 大小為22byte 我們要把它們弄成512byte的 在hello.asm最后添加 times 510-($-$$) db 0 ; 填充剩下的空間,,使生成的二進(jìn)制代碼恰好為512字節(jié) dw 0xaa55 ;這個(gè)是結(jié)束標(biāo)志符 再用nasm編譯生成hello.bin 再寫(xiě)入到hello.img便可以了,。 然后執(zhí)行 bochs -q -f .bochsrc 便可以看到紅色的H字樣了
ps:
若出現(xiàn)Message: ROM: System BIOS must end at 0xfffff 錯(cuò)誤的原因如下: In previous versions of Bochs the BIOS size was 64k and you always had to specify the start address at 0xf0000. Starting with release 2.2.5 Bochs supports BIOS images up to 512k and it's no longer necessary to specify the start address if the BIOS ends at memory top 就是不要再指定address了。
當(dāng)然要顯示Hello world可用以下這個(gè)
org 07c00h mov ax, cs;數(shù)據(jù)傳送指令,,將代碼段寄存器cs的內(nèi)容賦給通用寄存器ax mov ds, ax;使數(shù)據(jù)段與代碼段在同一個(gè)段 mov es, ax;使附加段與代碼段在同一個(gè)段 call DispStr jmp $ ;$表示當(dāng)前地址,無(wú)限循環(huán) DispStr: mov ax, BootMessage mov bp, ax ;es:bp=串地址 mov cx, 15 ;串長(zhǎng)度 mov ax, 01301h ;ah=13h, al=01h 視頻中斷13h號(hào)功能:寫(xiě)字符串,;AL=01H,表示寫(xiě)完字符串后,,更新光標(biāo)位置 mov bx, 000ch ;頁(yè)號(hào)為0(bh=0) 黑底紅字(bl=0ch,高亮) mov dl, 0 ;DH,、DL=寫(xiě)串的光標(biāo)位置,,DH=行號(hào),,DL=列號(hào) int 10h ret BootMessage: db "Hello,World OS!" times 510-($-$$) db 0 ; 填充剩下的空間,使生成的二進(jìn)制代碼恰好為512字節(jié) dw 0xaa55
times 510-($-$$) db 0
This reads: Times 510-(Start of this Instruction - Start of program) with 0's $ stands for start of the instruction $$ stands for start of the program dw 0xAA55
For some reason the signature has to be written this way round! This fills the last to bytes of the boot loader with 55AA (this is a hex number) Without this signature the BIOS won't recognise this as a bootable disk! |
|
來(lái)自: android之情殤 > 《linux》