前段时间一直忙于其他事情, Orange'S的学习暂时搁浅了。现在可以更多时间学习编写操作系统了,把之前学过的重新温习一下。
学习环境
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
指令部分说明
其他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安装及使用步骤
sudo apt-get install build-essential xorg-dev libgtk2. 0 -dev
$ ./configure --enable-debugger --enable-disasm $ make $ sudo make install
dd if=boot.bin of=a.img bs= 512 count= 1 conv=notrunc
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
(2)错误列表
在安装过程中和使用书带的光盘中的bochsrc时,出现了一些错误,解决方案列举如下,
Solution:将源文件中bochs的文件夹名称改为bochsdbg即可。
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
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]
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
Solution: None. bochs 2.3.5 以上的版本没有dump_cpu了,可以用r,fp,mmx,sse,dreg,sreg,creg命令代替。
附录
希望大家多多指正,如有转载,请注明出处http://www.cnblogs.com/JohnShao/archive/2011/10/23/2221565.html。