使用Bochs写Hello world

Day 1:配置Bochs以及调试模式

一 环境预备

Bochs的安装很简单,我们只需要去官网下载即可。http://bochs.sourceforge.net/

为了使用Linux命令行,我们可以安装WSL,也可以安装Cygwin(我的选用)

安装NASM。https://sourceforge.net/projects/nasm/

此外,为了完成写软盘的需求,安装了rawwritewin。http://www.chrysocome.net/rawwrite

安装完毕以后记得把软件添加到环境变量

二 写一个自己的Hello OS World

首先我们创建一个.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 ; 填充剩下的空间,使生成的二进制代码恰好为 
	dw 0xaa55 ; 结束标志

之后我们把这段代码用NASM编译一下,生成.bin文件

nasm boot.asm -o boot.bin

然后就是rawwritewin登场的时候了,我们要把.bin的内容写入软盘,不过这个时候不要急,我们先用bochs虚拟机新建一张软盘。命令如下:

bximage
//根据提示选择fd
//选择生成1.44的软盘
//新建的软盘名位a.img

生成软盘之后还别急,这时候我们还要配置一下Bochs虚拟机,大家可以搜索bochsrc文件(安装虚拟机时勾选了DLX Demo会自动创建),然后输入以下配置。注意 boot:,我们要将其修改为在软盘启动即boot:floppy,其他命令的解释也在注释里很清楚。

###############################################################
# bochsrc.txt file for DLX Linux disk image.
###############################################################

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

# filename of ROM images
romimage: file=../BIOS-bochs-latest
vgaromimage: file=../VGABIOS-lgpl-latest

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


# hard disk
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
ata0-master: type=disk, path="hd10meg.img", cylinders=306, heads=4, spt=17

# choose the boot disk.
# boot: c
boot: floppy
# default config interface is textconfig.
#config_interface: textconfig
#config_interface: wx

#display_library: x
# other choices: win32 sdl wx carbon amigaos beos macintosh nogui rfb term svga

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

# disable the mouse, since DLX is text only
mouse: enabled=0

# set up IPS value and clock sync
cpu: ips=15000000
clock: sync=both

# enable key mapping, using US layout as default.
#
# NOTE: In Bochs 1.4, keyboard mapping is only 100% implemented on X windows.
# However, the key mapping tables are used in the paste function, so 
# in the DLX Linux example I'm enabling keyboard_mapping so that paste 
# will work.  Cut&Paste is currently implemented on win32 and X windows only.

keyboard: keymap=../keymaps/x11-pc-us.map
#keyboard: keymap=../keymaps/x11-pc-fr.map
#keyboard: keymap=../keymaps/x11-pc-de.map
#keyboard: keymap=../keymaps/x11-pc-es.map

OK,虚拟机配置完了,就开始写软盘吧!

如果你使用linux命令:

dd if=boot.bin of=a.img bs=512 count=1 conv=notrunc

如果你使用windows平台,请为你的windows安装cygwin或者dd命令支持。

cygwin官网安装:http://www.cygwin.com/install.html

dd for Windows: http://www.chrysocome.net/dd

我强烈建议在windows上学习操作系统的同志安装WSL或者cygwin,这样就可以方便地使用强大的linux命令了!

OK,软盘写完以后,我们就启动虚拟机吧!

直接打开你的Powershell,输入以下命令

bochs

你也可以指定配置文件开启虚拟机:

bochs -f bochsrc.bxrc

开启虚拟机后你应该就能看见我们写好的Hello OS World了!效果如图:

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值