本文主要是介绍Orange'S学习笔记(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前段时间一直忙于其他事情, 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 string10 mov cx, 16 ; CX = length of string11 mov ax, 01301h ; AH = 13, AL = 01h12 mov bx, 000ch ; page num = 0 (BH = 0), red word with black backgroud (BL = 0ch, highlight)13 movdl, 014 int 10h ; break at 10h15 ret16 BootMessage: db "Hello, OS World!"17 times 510-($-$$) db 0 ; fill the remaining space to the number of the binary code reach 512 bytes18 dw 0xaa55 ; end
要用NASM编译上述代码,首先要安装NASM,然后编译。
sudo aptitude install nasmnasm 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-latest10 vgaromimage: file=/usr/local/share/bochs/VGABIOS-lgpl-latest #/usr/share/vgabios/vgabios.bin11 12 # what disk images will be used13 floppya: 1_44=a.img, status=inserted14 15 # choose the boot disk.16 boot: floppy17 18 # where do we send log messages?19 # log: bochsout.txt20 21 # disable the mouse22 mouse: enabled=023 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.6Build from CVS snapshot, on February 22, 2011Compiled at Jun 10 2011, 18:44:49========================================================================00000000000i[ ] reading configuration from bochsrc00000000000p[ ] >>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 bit00000000000i[CPU0 ] SS.d_b = 16 bit00000000000i[CPU0 ] | EAX=00000000 EBX=00000000 ECX=00000000 EDX=0000000000000000000i[CPU0 ] | ESP=00000000 EBP=00000000 ESI=00000000 EDI=0000000000000000000i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf zf af pf cf00000000000i[CPU0 ] | SEG selector base limit G D00000000000i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D00000000000i[CPU0 ] | CS:0000( 0000| 0| 0) 00000000 00000000 0 000000000000i[CPU0 ] | DS:0000( 0000| 0| 0) 00000000 00000000 0 000000000000i[CPU0 ] | SS:0000( 0000| 0| 0) 00000000 00000000 0 000000000000i[CPU0 ] | ES:0000( 0000| 0| 0) 00000000 00000000 0 000000000000i[CPU0 ] | FS:0000( 0000| 0| 0) 00000000 00000000 0 000000000000i[CPU0 ] | GS:0000( 0000| 0| 0) 00000000 00000000 0 000000000000i[CPU0 ] | EIP=00000000 (00000000)00000000000i[CPU0 ] | CR0=0x00000000 CR2=0x0000000000000000000i[CPU0 ] | CR3=0x00000000 CR4=0x00000000bx_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: PANICDevice: [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 executionalwayscont - continue execution, and don't ask again.This affects only PANIC events from device [MEM0 ]die - stop execution nowabort - dump coredebug - continue and return to bochs debuggerChoose 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/10/23/2221565.html。
这篇关于Orange'S学习笔记(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!