使用Bochs运行程序

来源:互联网 发布:c语言24关键字 编辑:程序博客网 时间:2024/06/04 17:44

1. 概述

   看《一个操作系统的实现》的时候,书中的第一个列子就是写了一个简单的代码,然后用bochs去仿真。然后觉得很有必要自己亲自动手去实现体验一下,以便加深理解。

2.安装 bochs

 在linux中安装bochs十分简单,我自己用的是ubuntu-14.04,直接使用命令:
$ sudo apt-get install vgabios boots bximage

用apt-get命令安装的bochs只能运行程序,不能调试程序。需要调试程序的话就要用源码安装。

安装汇编NASM编译器:
$sudo apt-get install nasm

3.编译程序

书中的第一个程序就是一个现实字符串的程序boot.asm:
org 07c00hmov   ax, csmov   ds, axmov   es, axcall  DispStrjmp   $DispStr:mov   ax, BootMessagemov   bp, axmov   cx, 16mov   ax, 01301hmov   bx, 000chmov   dl, 0int   10hretBootMessage: db "Hello, OS"times  510 - ($-$$) db 0dw     0xaa55

用NASM编译boot.asm:
nasm boot.asm -o boot.bin

用hexdump查看boot.bin格式(hexdump 命令参考:http://www.tutorialspoint.com/unix_commands/hexdump.htm):
$ hexdump boot.bin 0000000 c88c d88e c08e 02e8 eb00 b8fe 7c1e c5890000010 10b9 b800 1301 0cbb b200 cd00 c310 65480000020 6c6c 2c6f 4f20 0053 0000 0000 0000 00000000030 0000 0000 0000 0000 0000 0000 0000 0000*00001f0 0000 0000 0000 0000 0000 0000 0000 aa550000200

用bximage生成一个软盘映象:
$ 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] fdChoose 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.44I will create a floppy image with  cyl=80  heads=2  sectors per track=18  total sectors=2880  total bytes=1474560What 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



使用hexdump查看生成的映象文件格式,其实映象文件是一个空文件,内容被填充为0:
$ hexdump a.img 0000000 0000 0000 0000 0000 0000 0000 0000 0000*0168000zhaxia@zhaxia:~/zhaxia/linux/test$ ls -l-rw-rw-r-- 1 zhaxia zhaxia 1474560 May 13 22:40 a.img

将boot.bin写入映象文件中(dd命令的用法参考:https://www.lifewire.com/dd-linux-command-4095971):
$ dd if=boot.bin of=a.img bs=512 count=1 conv=notrunc1+0 records in1+0 records out512 bytes (512 B) copied, 0.000273614 s, 1.9 MB/s

再此查看a.img文件的格式,发现boot.bin已经被写入a.img的开始处:
$ hexdump a.img 0000000 c88c d88e c08e 02e8 eb00 b8fe 7c1e c5890000010 10b9 b800 1301 0cbb b200 cd00 c310 65480000020 6c6c 2c6f 4f20 0053 0000 0000 0000 00000000030 0000 0000 0000 0000 0000 0000 0000 0000*00001f0 0000 0000 0000 0000 0000 0000 0000 aa550000200 0000 0000 0000 0000 0000 0000 0000 0000*0168000

bochs配置文件:
$ cat bochsrc ################################################################ Configuration file for Bochs################################################################ how much memory the emulated machine will havemegs: 32# filename of ROM imagesromimage: file=/usr/share/bochs/BIOS-bochs-latestvgaromimage: file=/usr/share/vgabios/vgabios.bin# what disk images will be usedfloppya: 1_44=a.img, status=inserted# choose the boot disk.boot: floppy# where do we send log messages?# log: bochsout.txtdisplay_library: sdl# disable the mousemouse: enabled=0# enable key mapping, using US layout as default.#keyboard_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map


运行bochs:
$ bochs -f bochsrc 
 运行成果,出现画面:








0 0
原创粉丝点击