《一个操作系统的实现》(零):搭建环境

环境:Ubuntu12.10 + bochs2.6.1

今天从图书馆借了本《一个操作系统的实现》,豆瓣评分挺高的,准备试试。拿到手后先翻了一下前几页,第一章1.1的准备工作里写着要一张软盘,顿时傻眼,现在找台有软驱的电脑都难。连忙用手机百度一下,百度知道有个回答是用U盘代替软盘,于是我果断去校门口电子产品店买了个U盘(好惭愧,其实U盘早就该买一个的,需要的时候一直用手机、MP3、MP4以及邮箱神马的代替)。回实验室之后搞起~看了下前两章,发现第二章开头有句话“如果每次我们编译好的东西都要写到软盘上,再重启计算机,不但费时费力,对自己的爱机简直是一种蹂躏。你一定不会满足于这样的现状,还好,我们有如此多的工具,比如前面提到过的Bochs。” 好吧...看来书上有教程,那就照着一步一步做~

首先安装Bochs。最简单的方法是sudo apt-get install vgabios bochs bochs-x bximage,不过我没用这种方法,书上说这种方法默认不开启调试功能,所以我选择从源代码安装。(吐槽一句,bochs.sourceforge.net上不去!!)我找到的最新版是2.6.1,找了个其他靠谱网站下载了下来。

tar vxzf bochs-2.6.tar.gz
cd bochs-2.6
./configure --enable-debugger --enable-disasm
make
sudo make install

make这步出问题了:gtk/gtk.h:没有那个文件或目录

解决方法是sudo apt-get install libgtk-3-dev libgtk2.0-dev (其实我也不知道这两个之中哪个安装上就可以解决,我都安了)

但是!!安完之后重新make后还是报错,make clean也不好使,所以需要make dist-clean,然后重新执行上面的./configure --enable-debugger --enable-disasm和make和sudo make install

幸运的是,然后就不报错了,接下来创建软盘映像:输入bximage,会有如下内容出现(需要用户输入的地方标为加粗,当然括号里的内容不是。。)

========================================================================
                                bximage
                  Disk Image Creation Tool for Bochs
          $Id: bximage.c 11315 2012-08-05 18:13:38Z vruppert $
========================================================================


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] 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] (这个是默认名称)


Writing: [] Done.


I wrote 1474560 bytes to a.img.


The following line should appear in your bochsrc:
  floppya: image="a.img", status=inserted


将引导扇区写入软盘:dd if=boot.bin of=a.img bs=512 count=1 conv=notrunc

哦忘了说boot.bin怎么生成的了。首先要安装NASM,这个使用sudo apt-get install nasm就可以,下面是boot.asm的代码

	org 07c00h		;告诉编译器程序加载到7c00处
	mov ax, cs
	mov ds, ax
	mov es, ax
	call DispStr		;调用显示字符串例程
	jmp $			;无限循环
DispStr:
	mov ax, BootMessage
	mov bp, ax		;ES:BP = 串地址
	mov cx, 16		;CX = 串长度
	mov ax, 01301h		;AH = 13, AL = 01h
	mov bx, 000ch		;页号为0(BH=0)黑底红字(BL=0Ch,高亮)
	mov dl, 0
	int 10h			;10h号中断
	ret
BootMessage:			db "Hello, OS world!"
	times 510-($-$$)	db 0	;填充剩下的空间,使生成的二进制代码恰好为512字节
	dw 0xaa55		;结束标志


将这段代码用NASM编译一下:nasm boot.asm -o boot.bin,然后就可以执行上面的dd命令了~执行命令会出现下面三行:

记录了1+0 的读入
记录了1+0 的写出
512字节(512 B)已复制,6.8461e-05 秒,7.5 MB/秒

这时要在当前目录下新建bochsrc文件,书上那段配置文件路径需要修改一下(不能把bochs文件夹下的.bochsrc文件拷来直接用,会报错unsupported CPU model option)

#how much memory the emulated machine will have
megs: 32

#filename of ROM images
romimage: file=/home/agul/others/bochs-2.6/bios/BIOS-bochs-latest
vgaromimage: file=/home/agul/others/bochs-2.6/bios/VGABIOS-lgpl-latest

#what disk images will be used
floppya: 1_44=a.img, status=inserted

#choose the boot disk
boot: floppy

#where do we send log messages?
log: bochsout.txt

#disable the mouse
mouse: enabled=0

#enable key mapping, using US layout as default.
keyboard_mapping: enabled=1, map=/home/agul/others/bochs-2.6/gui/keymaps/x11-pc-us.map

启动!!!boches -f bochsrc

接下来输入6,便可以打开bochs了~等了好半天,还是一片黑,怎么不出现Hello, OS world!呢?Bochs其实这时还没运行,在终端输入c然后按回车试试~~结果出现了

先写到这儿了,明天再把第一二章看一下,理解一下代码和怎么用Bochs调试操作系统~

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值