`

(第一、二章)安装nasm,bochs并运行helloworld

阅读更多
1、安装nasm和vgabios:[hadoop@sam1 /]$ su root
Password:
[root@sam1 /]# yum install nasm
...[root@sam1 /]# yum install vgabios
...

注意:bochs需要依赖vgabios安装的东西/usr/share/vgabios/VGABIOS-lgpl-latest.bin,所以vgabios需要先安装!

2、安装可调试版bochs(从源代码安装):

注意:我先开始尝试安装Bochs 2.6等新版本都会出问题,最后还是安装了书上提到的bochs 2.3.5,configure/make/make install就一切正常了。

1)到bochs官方主页http://bochs.sourceforge.net/ ,有个Older News条目,进入当前最新版本Bochs 2.4.6
下载源代码包bochs-2.4.6.tar.gz

2)
tar vxzf bochs-2.4.6.tar.gz  -->解压
cd  bochs-2.4.6
./configure --enable-debugger --enable-disasm   -->开启调试和反汇编功能
make
make install  -->出现make: *** [install_bin] Error 1
(解决方法:命令前加上sudo就行,就是sudo make install)

2、制作引导扇区boot.asm:

1)桌面上(/home/hadoop/Desktop)新建OSImpl文件夹,在OSImpl下新建文件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 world!"

times 510-($-$$) db 0

dw 0xaa55

2)汇编boot.asm文件

[hadoop@sam1 OSImpl]$ pwd
/home/hadoop/Desktop/OSImpl
[hadoop@sam1 OSImpl]$ ls
bochs-2.4.6  boot.asm
[hadoop@sam1 OSImpl]$ nasm boot.asm -o boot.bin
[hadoop@sam1 OSImpl]$ ls
bochs-2.4.6  boot.asm  boot.bin

3、创建软盘镜像文件a.img,把boot.bin逐字节拷贝进去
[hadoop@sam1 OSImpl]$ cd bochs-2.4.6/
[hadoop@sam1 bochs-2.4.6]$ ./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]

Writing: [] Done.

I wrote 1474560 bytes to a.img.

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

[hadoop@sam1 bochs-2.4.6]$ dd if=../boot.bin of=./a.img bs=512 count=1 conv=notrunc
-->虚拟机中需要指定conv这项,真实的机器中软盘不会被截断
1+0 records in
1+0 records out
512 bytes (512 B) copied, 6.3313e-05 s, 8.1 MB/s

4、创建配置文件.bochsrc,修改相关配置

如果使用yum install vgabios bochs bochs-x bximage 方式安装bochs,bochsrc-sample.txt作为配置文件的模板会被安装[hadoop@sam1 OSImpl]$ rpm -ql bochs | grep  bochsrc-sample.txt


我这里用源代码包安装的,按照《Orange'S 一个操作系统的实现》P11配置即可:

[hadoop@sam1 bochs-2.4.6]$ gedit .bochsrc         -->在工程文件夹下创建.bochsrc
####################################.bochsrc-start###################################

megs:32
romimage:file=/usr/share/bochs/BIOS-bochs-latest


#书上使用这个: vgaromimage:file=/usr/share/vgabios/vgabios.bin
#但通过ls -l /usr/share/vgabios/vgabios.bin发现并不存在,换成/usr/share/vgabios/VGABIOS-lgpl-latest.bin即可


vgaromimage:file=/usr/share/vgabios/VGABIOS-lgpl-latest.bin

floppya: 1_44=a.img, status=inserted

boot:floppy

log:bochsout.txt
#######################################.bochsrc-end#################################

5、启动bochs,运行Hello,OS world示例

[hadoop@sam1 bochs-2.4.6]$ ./bochs
========================================================================
                       Bochs x86 Emulator 2.4.6
             Build from CVS snapshot, on February 22, 2011
                   Compiled at May 19 2011, 22:34:01
========================================================================
00000000000i[     ] reading configuration from .bochsrc
------------------------------
Bochs Configuration: Main Menu
------------------------------

This is the Bochs Configuration Interface, where you can describe the
machine that you want to simulate.  Bochs has already searched for a
configuration file (typically called bochsrc.txt) and loaded it if it
could be found.  When you are satisfied with the configuration, go
ahead and start the simulation.

You can also start bochs with the -q option to skip these menus.

1. Restore factory default configuration
2. Read options from...
3. Edit options
4. Save options to...
5. Restore the Bochs state from...
6. Begin simulation
7. Quit now

Please choose one: [6] 2
00000000000i[     ] Screen mode changed to none

What is the configuration file name?
To cancel, type 'none'. [.bochsrc]
00000000000i[     ] reading configuration from .bochsrc
------------------------------
Bochs Configuration: Main Menu
------------------------------

This is the Bochs Configuration Interface, where you can describe the
machine that you want to simulate.  Bochs has already searched for a
configuration file (typically called bochsrc.txt) and loaded it if it
could be found.  When you are satisfied with the configuration, go
ahead and start the simulation.

You can also start bochs with the -q option to skip these menus.

1. Restore factory default configuration
2. Read options from...
3. Edit options
4. Save options to...
5. Restore the Bochs state from...
6. Begin simulation
7. Quit now

Please choose one: [6]

*********************************************************************************
00000000000i[     ] installing x module as the Bochs GUI
00000000000i[     ] using log file bochsout.txt
Next at t=0
(0) [0x00000000fffffff0] f000:fff0 (unk. ctxt): jmp far f000:e05b         ; ea5be000f0
<bochs:1> b 0x7c00
<bochs:2> c
(0) Breakpoint 1, 0x00007c00 in ?? ()
Next at t=12943100
(0) [0x0000000000007c00] 0000:7c00 (unk. ctxt): mov ax, cs                ; 8cc8

<bochs:3> info cpu
eax: 0x0000aa55 43605
ecx: 0x00090000 589824
edx: 0x00000000 0
ebx: 0x00000000 0
esp: 0x0000ffd6 65494
ebp: 0x00000000 0
esi: 0x000e34e0 931040
edi: 0x0000ffac 65452
eip: 0x00007c00
eflags 0x00000082: id vip vif ac vm rf nt IOPL=0 of df if tf SF zf af pf cf
status  word: 0x0000: b c3 TOS0 c2 c1 c0 es sf pe ue oe ze de ie
control word: 0x0040: inf RC_NEAREST PC_32 pm um om zm dm im
tag word:     0x5555
operand:      0x0000
fip:          0x00000000
fcs:          0x0000
fdp:          0x00000000
fds:          0x0000
=>FP0 ST0(0):        raw 0x0000:0000000000000000 (0.0000000000) (ZERO)
  FP1 ST1(0):        raw 0x0000:0000000000000000 (0.0000000000) (ZERO)
  FP2 ST2(0):        raw 0x0000:0000000000000000 (0.0000000000) (ZERO)
  FP3 ST3(0):        raw 0x0000:0000000000000000 (0.0000000000) (ZERO)
  FP4 ST4(0):        raw 0x0000:0000000000000000 (0.0000000000) (ZERO)
  FP5 ST5(0):        raw 0x0000:0000000000000000 (0.0000000000) (ZERO)
  FP6 ST6(0):        raw 0x0000:0000000000000000 (0.0000000000) (ZERO)
  FP7 ST7(0):        raw 0x0000:0000000000000000 (0.0000000000) (ZERO)
MXCSR: 0x00001f80: ule fuz RC:0 PM UM OM ZM DM IM daz pe ue oe ze de ie
XMM[00]: 00000000:00000000:00000000:00000000
XMM[01]: 00000000:00000000:00000000:00000000
XMM[02]: 00000000:00000000:00000000:00000000
XMM[03]: 00000000:00000000:00000000:00000000
XMM[04]: 00000000:00000000:00000000:00000000
XMM[05]: 00000000:00000000:00000000:00000000
XMM[06]: 00000000:00000000:00000000:00000000
XMM[07]: 00000000:00000000:00000000:00000000

<bochs:4> x /64xb 0x7c00
[bochs]:
0x00007c00 <bogus+       0>: 0x8c 0xc8 0x8e 0xd8 0x8e 0xc0 0xe8 0x02
0x00007c08 <bogus+       8>: 0x00 0xeb 0xfe 0xb8 0x1e 0x7c 0x89 0xc5
0x00007c10 <bogus+      16>: 0xb9 0x10 0x00 0xb8 0x01 0x13 0xbb 0x0c
0x00007c18 <bogus+      24>: 0x00 0xb2 0x00 0xcd 0x10 0xc3 0x48 0x65
0x00007c20 <bogus+      32>: 0x6c 0x6c 0x6f 0x2c 0x20 0x4f 0x53 0x20
0x00007c28 <bogus+      40>: 0x77 0x6f 0x72 0x6c 0x64 0x21 0x00 0x00
0x00007c30 <bogus+      48>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00007c38 <bogus+      56>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

<bochs:5> n
Next at t=12943101
(0) [0x0000000000007c02] 0000:7c02 (unk. ctxt): mov ds, ax                ; 8ed8


<bochs:6> trace-reg on
Register-Tracing enabled for CPU0
<bochs:7> n
Next at t=12943102
eax: 0x00000000 0
ecx: 0x00090000 589824
edx: 0x00000000 0
ebx: 0x00000000 0
esp: 0x0000ffd6 65494
ebp: 0x00000000 0
esi: 0x000e34e0 931040
edi: 0x0000ffac 65452
eip: 0x00007c04
eflags 0x00000082: id vip vif ac vm rf nt IOPL=0 of df if tf SF zf af pf cf
(0) [0x0000000000007c04] 0000:7c04 (unk. ctxt): mov es, ax                ; 8ec0

<bochs:8> c

此时出现Hello, OS world! 的红色字样!
分享到:
评论

相关推荐

    Bochs入门教程[操作系统第一步]

    在80x86 IBM PC 兼容计算机上用一张软盘启动引导程序,最后打印出下面的字样: Hello world! The current time is hh:mm:ss.(动态输出时间) ...5、是写操作系统的第一步!! 6、可执行源码和详细教程。

    自己动手写操作系统

    第1章 马上动手写一个最小的"操作系统"1 1.1 准备工作1 1.2 10分钟完成的操作系统1 1.3 Boot Sector3 1.4 代码解释3 1.5 水面下的冰山5 1.6 回顾6 第2章 搭建你的工作环境7 2.1 虚拟计算机(Virtual PC)7 ...

    自己动手写操作系统 电子工业出版社 pdf

    第1章 马上动手写一个最小的“操作系统”1 1.1 准备工作1 1.2 10分钟完成的操作系统1 1.3 Boot Sector3 1.4 代码解释3 1.5 水面下的冰山5 1.6 回顾6 第2章 搭建你的工作环境7 2.1 虚拟计算机(Virtual PC)7 2.1.1 ...

    第1章 实验入门1

    在实验中,你需要安装Bochs模拟器,并学会使用它来创建硬盘配置和运行程序。Bochs提供了强大的调试功能,通过debug指令可以深入理解程序执行的每一步。编写Hello World程序不仅可以帮助你熟悉汇编语法,还能让你直观...

    自己动手写操作系统 pdf

    第1章 马上动手写一个最小的“操作系统”1 1.1 准备工作1 1.2 10分钟完成的操作系统1 1.3 Boot Sector3 1.4 代码解释3 1.5 水面下的冰山5 1.6 回顾6 第2章 搭建你的工作环境7 2.1 虚拟计算机(Virtual PC)7 2.1.1 ...

    很详细的保护模式编程

    引导程序是在计算机启动过程中由BIOS加载的第一个程序,用于初始化系统并加载操作系统内核。其特点包括: 1. **大小限制**:引导程序必须恰好512字节大小。 2. **结束标志**:引导程序的最后两个字节必须是AA55H...

    boot-sector:这是用于操作系统启动的引导扇区汇编代码!

    这是书中的源代码:“如何制作一个简单的...Eg: nasm boot_helloworld.asm -f bin -o boot_helloworld.bin 中银国际使用: QEMU 用法: qemu-system-i386 [bin_filename] Eg: qemu-system-i386 boot_helloworld.bin

    一个操作系统的实现借鉴.pdf

    使用NASM编译`boot.asm`生成`boot.bin`,然后利用`dd`或`rawrite`将生成的二进制文件写入软盘的第一个扇区,从而创建了一个引导盘。 5. **运行与验证**: - 完成上述步骤后,将软盘插入软驱,重新启动计算机,...

    一个操作系统的实现

    NASM编译器将汇编代码编译成二进制格式的boot.bin文件,然后通过软盘绝对扇区读写工具将该文件写入软盘的第一个扇区,制作成一个可以引导的软盘。 完成引导扇区的编写和制作后,可以将这个引导盘放入计算机的软驱中...

    x86 x64体系探索及编程part1

    目录 第一篇 x86 基础 第1 章数与数据类型2 1.1 数 2 1.1.1 数字 2 1.1.2 二进制数 3 1.1.3 二进制数的排列 3 1.1.4 十六进制数 5 1.1.5 八进制数与十进制数 5 1.2 数据类型 6 1.2.1 integer 数 6 1.2.2 floating-...

    x86 x64体系探索及编程part3

    目录 第一篇 x86 基础 第1 章数与数据类型2 1.1 数 2 1.1.1 数字 2 1.1.2 二进制数 3 1.1.3 二进制数的排列 3 1.1.4 十六进制数 5 1.1.5 八进制数与十进制数 5 1.2 数据类型 6 1.2.1 integer 数 6 1.2.2 floating-...

    x86 x64体系探索及编程part4

    目录 第一篇 x86 基础 第1 章数与数据类型2 1.1 数 2 1.1.1 数字 2 1.1.2 二进制数 3 1.1.3 二进制数的排列 3 1.1.4 十六进制数 5 1.1.5 八进制数与十进制数 5 1.2 数据类型 6 1.2.1 integer 数 6 1.2.2 floating-...

    x86 x64体系探索及编程 part2

    目录 第一篇 x86 基础 第1 章数与数据类型2 1.1 数 2 1.1.1 数字 2 1.1.2 二进制数 3 1.1.3 二进制数的排列 3 1.1.4 十六进制数 5 1.1.5 八进制数与十进制数 5 1.2 数据类型 6 1.2.1 integer 数 6 1.2.2 floating-...

    驱动扇区代码, 写自己的操作系统

    - **引导块**:通常位于硬盘的第一个扇区,包含一段小程序,用于检测操作系统所在的分区并加载相应的引导加载程序。 - **魔法数字**:在MBR中有一个特定的“魔法数字”(通常是`0xAA55`),用来确认该扇区是否为有效的...

    汇编语言程序设计知识点总结

    - **示例1:简单的Hello World程序** 1. 初始化数据段,设置输出字符串。 2. 使用`mov`指令将字符串地址传给寄存器。 3. 调用`int 21h`(DOS中断)来显示字符串。 4. 使用`ret`指令结束程序。 - **示例2:实现两...

    一个操作系统的实现Playing.with.OS

    在计算机系统启动时,首先读取硬盘上的第一个扇区,即引导扇区(也称为启动扇区或引导记录)。这一扇区包含了引导加载程序(Bootloader)的代码,其主要任务是将操作系统内核加载到内存中并执行。 在《Playing.with...

    汇编编程环境

    6. **模拟器/虚拟机**:为了在不同硬件平台上测试汇编代码,模拟器如QEMU或Bochs能够模拟处理器和其他硬件,使得汇编程序能够在非目标平台上运行。 7. **汇编语言基础**:学习汇编语言时,需要理解基本的指令集,如...

    汇编语言教程、案例代码、建议及注意项

    - **Bochs**:用于模拟PC运行环境。 #### 五、教学资料 - **OSDev Wiki**:包含有关操作系统开发的相关资源,其中也包含了汇编语言的教学内容。 #### 六、建议与注意项 1. **实践**:汇编语言的学习需要大量的...

Global site tag (gtag.js) - Google Analytics