作者:帅得不敢出门 C++爱好者灌水天堂群 3503799 转载请保留此信息
我的系统为redhat9
首先去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 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 是说内存是32M
floppya: 1_44=a.img, status=inserted 指定了软盘映像文件
boot: floppy 从软盘启动。
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
进入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 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!