学习环境
Ubuntu 11.04 + Bochs 2.4.6 + NASM
主要工作
1. 第一章 马上动手写一个最小的“操作系统”
代码如下:
1 org 07c00h ; tell the compiler that the program is loaded at 7c00
2 mov ax, cs
3 mov ds, ax
4 mov es, ax
5 call DispStr ; call the example about string display
6 jmp $ ; unlimited loop
7 DispStr:
8 mov ax, BootMessage
9 mov bp, ax ; ES:BP = address of string
10 mov cx, 16 ; CX = length of string
11 mov ax, 01301h ; AH = 13, AL = 01h
12 mov bx, 000ch ; page num = 0 (BH = 0), red word with black backgroud (BL = 0ch, highlight)
13 mov dl, 0
14 int 10h ; break at 10h
15 ret
16 BootMessage: db "Hello, OS World!"
17 times 510-($-$$) db 0 ; fill the remaining space to the number of the binary code reach 512 bytes
18 dw 0xaa55 ; end
要用NASM编译上述代码,首先要安章NASM,然后编译。
sudo aptitude install nasm
nasm boot.asm -o boot.bin
指令部分说明
- org: 伪指令,用来规定目标程序存放单元的偏移量。比如,如果在源程序的第一条指令前用了如下指令: org 200h,那么,汇编程序会把指令指针的ip的值设成200h,即目标程序的第一个字节放在200h处,后面的 内容则顺序存放,除非遇上另一个org 语句;
- $: 当前被编译的地址;
- $$: 一个section开始被编译的地址。
其他nasm指令参见nasm中文手册(有需要的,可以留下e-mail)或登录http://www.mouseos.com/assembly/nasm03.html查看。
2. 第二章 搭建你的工作环境
通过安装Bochs来搭建我们的工作环境。目前Bochs最新的版本是2.4.6,下载地址是http://sourceforge.net /projects/bochs/files/bochs/2.4.6/。
(1)Bochs安装及使用步骤
- 下载源程序;
- 安装build-essential,xorg-dev,libgtk2.0-dev;
sudo apt-get install build-essential xorg-dev libgtk2.0-dev
- 编译安装;
$ ./configure --enable-debugger --enable-disasm
$ make
$ sudo make install
- 利用bximage生成虚拟软盘。在输入bximage命令后,在第一步提示选择fd即可;
- 将引导扇区写入软盘;
dd if=boot.bin of=a.img bs=512 count=1 conv=notrunc
- 配置bochsrc,具体内容如下,
1 ###############################################################
2 # Configuration file for Bochs
3 ###############################################################
4
5 # how much memory the emulated machine will have
6 megs: 32
7
8 # filename of ROM images
9 romimage: file=/usr/local/share/bochs/BIOS-bochs-latest #/usr/share/bochs/BIOS-bochs-latest
10 vgaromimage: file=/usr/local/share/bochs/VGABIOS-lgpl-latest #/usr/share/vgabios/vgabios.bin
11
12 # what disk images will be used
13 floppya: 1_44=a.img, status=inserted
14
15 # choose the boot disk.
16 boot: floppy
17
18 # where do we send log messages?
19 # log: bochsout.txt
20
21 # disable the mouse
22 mouse: enabled=0
23
24 # enable key mapping, using US layout as default.
25 keyboard_mapping: enabled=1, map=/usr/local/share/bochs/keymaps/x11-pc-us.map #/usr/share/bochs/keymaps/x11-pc-us.map
- 运行Bochs,显示效果如下图所示,
(2)错误列表
在安装过程中和使用书带的光盘中的bochsrc时,出现了一些错误,解决方案列举如下,
- Error 1:安装Bochs时,出现错误”install: cannot stat `./bochsdbg': No such file or directory“
Solution:将源文件中bochs的文件夹名称改为bochsdbg即可。
- Error 2: 运行bochs时,出现错误”bochsrc:10: vgaromimage directive malformed,“
Solution: 将bochsrc文件中第10行vgaromimage: /usr/share/vgabios/vgabios.bin改为
vgaromimage: file=/usr/share/vgabios/vgabios.bin
具体错误输出如下:
bochs -f bochsrc
========================================================================
Bochs x86 Emulator 2.4.6
Build from CVS snapshot, on February 22, 2011
Compiled at Jun 10 2011, 18:44:49
========================================================================
00000000000i[ ] reading configuration from bochsrc
00000000000p[ ] >>PANIC<< bochsrc:10: vgaromimage directive malformed.
00000000000e[CTRL ] notify called, but no bxevent_callback function is registered
========================================================================
Bochs is exiting with the following message:
[ ] bochsrc:10: vgaromimage directive malformed.
========================================================================
00000000000i[CPU0 ] CPU is in real mode (active)
00000000000i[CPU0 ] CS.d_b = 16 bit
00000000000i[CPU0 ] SS.d_b = 16 bit
00000000000i[CPU0 ] | EAX=00000000 EBX=00000000 ECX=00000000 EDX=00000000
00000000000i[CPU0 ] | ESP=00000000 EBP=00000000 ESI=00000000 EDI=00000000
00000000000i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf zf af pf cf
00000000000i[CPU0 ] | SEG selector base limit G D
00000000000i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00000000000i[CPU0 ] | CS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | DS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | SS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | ES:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | FS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | GS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | EIP=00000000 (00000000)
00000000000i[CPU0 ] | CR0=0x00000000 CR2=0x00000000
00000000000i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
bx_dbg_read_linear: physical memory read error (phy=0x0000000000000000, lin=0x00000000)
00000000000i[CTRL ] quit_sim called with exit code 1
- Error 3: 运行bochsrc时,出现错误”ROM: couldn't open ROM image file '/usr/share/bochs/BIOS-bochs-latest'.“
Solution: 将bochsrc文件中第9行romimage: file=/usr/share/bochs/BIOS-bochs-latest改为
romimage: file=/usr/local/share/bochs/BIOS-bochs-latest #/usr/share/bochs/BIOS-bochs-latest
具体错误输出如下:
Event type: PANIC
Device: [MEM0 ]
Message: ROM: couldn't open ROM image file '/usr/share/bochs/BIOS-bochs-latest'.
A PANIC has occurred. Do you want to:
cont - continue execution
alwayscont - continue execution, and don't ask again.
This affects only PANIC events from device [MEM0 ]
die - stop execution now
abort - dump core
debug - continue and return to bochs debugger
Choose one of the actions above: [die]
- Error 4: 运行bochsrc时,出现关于Keymap的错误,见下图,
Solution: 将bochsrc文件中最后一行keyboard_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map改为
keyboard_mapping: enabled=1, map=/usr/local/share/bochs/keymaps/x11-pc-us.map #/usr/share/bochs/keymaps
/x11-pc-us.map
- Error 5: 调试引导扇区程序是,出现错误”syntax error at 'dump_cpu'“
Solution: None. bochs 2.3.5 以上的版本没有dump_cpu了,可以用r,fp,mmx,sse,dreg,sreg,creg命令代替。
附录
希望大家多多指正,如有转载,请注明出处http://www.cnblogs.com/JohnShao/archive/2011/06/11/2078419.html。
继续学习,更多惊喜!