使用Bochs运行程序

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 07c00h
mov   ax, cs
mov   ds, ax
mov   es, ax
call  DispStr
jmp   $

DispStr:
mov   ax, BootMessage
mov   bp, ax
mov   cx, 16
mov   ax, 01301h
mov   bx, 000ch
mov   dl, 0
int   10h
ret

BootMessage: db "Hello, OS"
times  510 - ($-$$) db 0
dw     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 c589
0000010 10b9 b800 1301 0cbb b200 cd00 c310 6548
0000020 6c6c 2c6f 4f20 0053 0000 0000 0000 0000
0000030 0000 0000 0000 0000 0000 0000 0000 0000
*
00001f0 0000 0000 0000 0000 0000 0000 0000 aa55
0000200

用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] 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



使用hexdump查看生成的映象文件格式,其实映象文件是一个空文件,内容被填充为0:
$ hexdump a.img 
0000000 0000 0000 0000 0000 0000 0000 0000 0000
*
0168000
zhaxia@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=notrunc
1+0 records in
1+0 records out
512 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 c589
0000010 10b9 b800 1301 0cbb b200 cd00 c310 6548
0000020 6c6c 2c6f 4f20 0053 0000 0000 0000 0000
0000030 0000 0000 0000 0000 0000 0000 0000 0000
*
00001f0 0000 0000 0000 0000 0000 0000 0000 aa55
0000200 0000 0000 0000 0000 0000 0000 0000 0000
*
0168000

bochs配置文件:
$ cat bochsrc 
###############################################################
# Configuration file for Bochs
###############################################################

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

# filename of ROM images
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/vgabios/vgabios.bin

# 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
display_library: sdl

# disable the mouse
mouse: 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 
 运行成果,出现画面:
使用Bochs运行程序_第1张图片







你可能感兴趣的:(linux)