WriteOS: 制作一个 Boot Sector 并使用 Bochs 进行调试
2013-03-31 20:32
218 查看
转载:http://sleepycat.org/tech/os/nasm-boot-sector
1 Boot Sector 的 NASM 程序
1.1 org 0x7c00
1.2 mov ax, cs/mov es, ax
1.3 call DisplayString
1.4 times 510 - ($ -$$) db 0
1.5 dw 0xaa55
1.6 cli hlt
2 使用 bochs 进行调试
2.1 配置 bochs 虚拟机
2.2 启动与调试
3 参考文档
版本:Linux Mint 13(Ubuntu 11.10), nasm v2.09.08, gcc v4.6.1,
日期:2012-07-25
永久链接:http://sleepycat.org/tech/os/nasm-boot-sector
然而,有的BIOS对此的处理稍有不同,即:CS = 0x7c0,IP = 0x0, 结合起来,代码所在的内存地址为:0x7c0:0x0 = 0x7c00,内存位置结果不变。此种情况稍后讨论。)
The BIOS does not load the boot sector to a random spot in memory. The BIOS will always load the boot sector starting at the memory location 0x7C00. After loading the boot sector into memory the BIOS sets the IP register to 0x7C00 and sets the CS register
to 0x0 (or just 0). Together this memory address looks like 0x0:0x7C00. We use a colon to denote that the left side is the segment and the right side is the offset. The IP register points to the next instruction to be executed so the first thing our boot sector
must contain is an instruction that we want to be executed right away. However, not all BIOSs will load our boot sector using the address 0x0:0x7C00. Some BIOSs will load the boot sector to 0x7C0:0x0 which means the CS register is set to 0x7C0 and the offset
is 0x0. Remembering that the first four bits of the segment are implicitly set to zeros, this memory address is the same address as 0x0:0x7C00 so no matter which way the BIOS loads your boot sector, it is in the same physical location in memory. Because not
knowing the exact way the BIOS loaded our boot sector, we will force our boot sector to use the 0x0:0x7C00 address.
es:bp 用于指向中断 int 10h 中用于显示的字符串的地址。参考 DisplayString 调用。
mov es, cs 非法。故通过 ax 中转。
http://en.wikipedia.org/wiki/INT_10H
Remember that our boot sector must be one sector large, which is 512 bytes, and it must contain 0xAA55 in the last two bytes.
cli 指令,表示 Clear Interrupts,关闭所有的硬件中断。
hlt 指令,表示停止 CPU 运行。
The cli instruction stands for CLear Interrupts. It disables all hardware interrupts from occurring. The second instruction, hlt, causes the CPU to completely stop processing. The only way for the processor to begin processing after the hlt instruction is
called is if a hardware interrupt occurs. By calling cli before hlt we are ensured that no hardware interrupts will be accepted and the CPU will be halted until reboot.
?
目录:
本文简介1 Boot Sector 的 NASM 程序
1.1 org 0x7c00
1.2 mov ax, cs/mov es, ax
1.3 call DisplayString
1.4 times 510 - ($ -$$) db 0
1.5 dw 0xaa55
1.6 cli hlt
2 使用 bochs 进行调试
2.1 配置 bochs 虚拟机
2.2 启动与调试
3 参考文档
本文简介
概要:从零制作一个基于 NASM 的 Boot Sector,并调试版本:Linux Mint 13(Ubuntu 11.10), nasm v2.09.08, gcc v4.6.1,
日期:2012-07-25
永久链接:http://sleepycat.org/tech/os/nasm-boot-sector
1 Boot Sector 的 NASM 程序
?1.1 org 0x7c00
控制BIOS,将代码起始位置放置于: 0x7c00 处。(CS = 0x0,IP = 0x7c00, 结合起来,代码所在的内存地址为:0x0:0x7c00 = 0x7c00)然而,有的BIOS对此的处理稍有不同,即:CS = 0x7c0,IP = 0x0, 结合起来,代码所在的内存地址为:0x7c0:0x0 = 0x7c00,内存位置结果不变。此种情况稍后讨论。)
The BIOS does not load the boot sector to a random spot in memory. The BIOS will always load the boot sector starting at the memory location 0x7C00. After loading the boot sector into memory the BIOS sets the IP register to 0x7C00 and sets the CS register
to 0x0 (or just 0). Together this memory address looks like 0x0:0x7C00. We use a colon to denote that the left side is the segment and the right side is the offset. The IP register points to the next instruction to be executed so the first thing our boot sector
must contain is an instruction that we want to be executed right away. However, not all BIOSs will load our boot sector using the address 0x0:0x7C00. Some BIOSs will load the boot sector to 0x7C0:0x0 which means the CS register is set to 0x7C0 and the offset
is 0x0. Remembering that the first four bits of the segment are implicitly set to zeros, this memory address is the same address as 0x0:0x7C00 so no matter which way the BIOS loads your boot sector, it is in the same physical location in memory. Because not
knowing the exact way the BIOS loaded our boot sector, we will force our boot sector to use the 0x0:0x7C00 address.
1.2 mov ax, cs/mov es, ax
?mov es, cs 非法。故通过 ax 中转。
1.3 call DisplayString
调用 BIOS 中断 int 10h, 显示字符串到屏幕。详解:http://en.wikipedia.org/wiki/INT_10H
1.4 times 510 - ($ -$$) db 0
填充510个字符,剩余的部分为01.5 dw 0xaa55
Boot Sector 必须有一个扇区大,即512 bytes, 且最后两个字节必须为 0xAA55.Remember that our boot sector must be one sector large, which is 512 bytes, and it must contain 0xAA55 in the last two bytes.
1.6 cli hlt
有的程序用 jmp $ 代替此处。但 jmp $ 会使 CPU 不停地进行忙碌死循环。cli 指令,表示 Clear Interrupts,关闭所有的硬件中断。
hlt 指令,表示停止 CPU 运行。
The cli instruction stands for CLear Interrupts. It disables all hardware interrupts from occurring. The second instruction, hlt, causes the CPU to completely stop processing. The only way for the processor to begin processing after the hlt instruction is
called is if a hardware interrupt occurs. By calling cli before hlt we are ensured that no hardware interrupts will be accepted and the CPU will be halted until reboot.
2 使用 bochs 进行调试
2.1 配置 bochs 虚拟机
创建启动配置文件: bochsrc, 内容如下:?
2.2 启动与调试
写入软盘命令: $ dd if=boot.bin of=a.img bs=512 count=1 conv=notrunc 注: bs=字节 强迫 ibs=<字节> 及 obs=<字节> cbs=字节 每次转换指定的<字节> conv=关键字 根据以逗号分隔的关键字表示的方式来转换文件 count=块数目 只复制指定量<块数目>的输入数据 ibs=字节 每次读取指定的<字节> if=文件 读取<文件>内容而非标准输入的数据 iflag=标记 以指定标记取代逗号作为读入符号列表的分隔符 obs=字节 每次写入指定的<字节> of=文件 将数据写入<文件>而不在标准输出显示 notrunc 不截断输出文件 http://blog.csdn.net/littlehedgehog/article/details/2647846 启动命令: $ bochs -f bochsrc ======================================================================== Bochs x86 Emulator 2.5.1 Built from SVN snapshot on January 6, 2012 Compiled on Jul 3 2012 at 22:20:20 ======================================================================== 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] 默认选择 6, 然后开始调试。 n => 单步执行。遇函数跳过。 s => 单步执行。遇函数进入。 c => 执行,直到断点。 b 0x30400 => 在 0x30400 处设断点。 info break => break info info cpu => cpu info
2 参考文档
http://www.supernovah.com/Tutorials/BootSector2.php相关文章推荐
- 如何制作一个可以用Bochs调试的最新内核系统盘
- 使用Bochs和GDB对内核进行源代码级调试
- 一个使用模板函数进行遥感图像处理易犯但不易调试的错误
- 如何在macosx上通过源码安装bochs2.4.6并使用peter-bochs进行调试
- 使用bochs对内核进行调试
- 《一个操作系统的实现》(二):使用Bochs调试操作系统
- 多台Mac电脑使用同一个苹果开发者帐号进行调试
- Firefox/Chrome/Safari的中可直接使用$/$$函数进行调试
- 使用 Minidumps 和 Visual Studio .NET 进行崩溃后调试
- 使用 gdb 对程序进行汇编级调试
- 如何使用CubeMx制作一个基于SD卡的文件系统工程(2)
- ionic应用在mac上使用xcode7.0进行iphone真机调试
- 使用telnet来进行远程调试和开多终端
- 使用gdb在Android Emulator中进行调试
- 使用 Eclipse 平台进行调试
- (译)如何使用cocos2d制作一个Slide Image Game:第一部分
- 【DIY】使用竹签制作一个埃菲尔塔
- IDEA下使用Jetty进行Debug模式调试
- 使用kgdb进行Linux内核调试(未完成!)
- 使用VS进行远程调试