首先去bochs的官網
http://bochs.sourceforge.net/
下載最新的程序,我直接下的是bochs-2.4.tar.gz
tar zxvf bochs-2.4.tar.gz
./configure
若是要調試功能的話,可查看configure文件作相應調整
--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
網上說要加最後一個暫時沒有明白其意。
make
./bochs 運行BOCHS運行須要.bochsrc文件,在LINUX下以.開頭的文件都是隱藏文件
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 不然會出現
#Message: ROM: System BIOS must end at 0xfffff 錯誤的 這個後面會解釋
#romimage: file=mybios.bin, address=0xfff80000 # 512k at memory topvgaromimage: 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=9boot: floppy
#boot: diskmegs: 32 是說內存是32M
floppya: 1_44=a.img, status=inserted 指定了軟盤映像文件
boot: floppy 從軟盤啓動。從http://bochs.sourceforge.net/下載nasm
我下的是nasm-2.05.01.tar.gz
tar zxvf nasm-2.05.01.tar.gz
cd nasm-2.05.01
./configure
make
make installmkdir 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進入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寫入img中
dd if=/nasm-2.05.01/helloworld/hello.bin of=hello.img bs=512 count=1 conv=notrunc
讀入了 0+1 個塊
輸出了 0+1 個塊bochs -q -f .bochsrc 運行咱們的OS
出現以下錯誤
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 ; 填充剩下的空間,使生成的二進制代碼剛好爲512字節
dw 0xaa55 ;這個是結束標誌符
再用nasm編譯生成hello.bin
再寫入到hello.img即可以了。
而後執行
bochs -q -f .bochsrc
即可以看到紅色的H字樣了ps:
若出現Message: ROM: System BIOS must end at 0xfffff 錯誤的緣由以下:
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了。固然要顯示Hello world可用如下這個
org 07c00h
mov ax, cs;數據傳送指令,將代碼段寄存器cs的內容賦給通用寄存器ax
mov ds, ax;使數據段與代碼段在同一個段
mov es, ax;使附加段與代碼段在同一個段
call DispStr
jmp $ ;$表示當前地址,無限循環
DispStr:
mov ax, BootMessage
mov bp, ax ;es:bp=串地址
mov cx, 15 ;串長度
mov ax, 01301h ;ah=13h, al=01h 視頻中斷13h號功能:寫字符串;AL=01H,表示寫完字符串後,更新光標位置
mov bx, 000ch ;頁號爲0(bh=0) 黑底紅字(bl=0ch,高亮)
mov dl, 0 ;DH、DL=寫串的光標位置,DH=行號,DL=列號
int 10h
ret
BootMessage: db "Hello,World OS!"
times 510-($-$$) db 0 ; 填充剩下的空間,使生成的二進制代碼剛好爲512字節
dw 0xaa55times 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 0xAA55For 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!ios