环境: WSL(window下linux子系统) Ubuntu18 使用Bochs来模拟系统
于渊老师的第一个程序差点就把我困住了 T_T
在这里做笔记, 文中加了❓的地方表示还没懂, 先不深究, "囫囵吞枣"一下, 看到后面再回来解决
代码
show me the code!
先写个汇编
; file_name: boot.asm
org 07c00h ; 告诉编译器程序加载到7c00处, 规定程序的起始地址(偏移地址)在7c00,
mov ax, cs
mov ds, ax ;
mov es, ax ; 代码段数据段附加段在同一个段 ?可以去掉吗
call DispStr ; 调用显示字符串
jmp $ ; 无限循环
; 以上就是主体部分了
DispStr:
mov ax, message
mov bp, ax ; ES:BP = 串地址
mov cx, 16 ; CX = 串长度
mov ax, 01301h ; AH = 13, AL = 01h
mov bx, 000ch ; 页号为0(BH = 0) 黑底红字(BL = 0Ch,高亮)
mov dl, 0
int 10h ; 10h 号中断
ret
message db "Hello, Js world!" ; 字符串+回车换行
times 510-($-$$) db 0 ; 填充剩下的空间,使得到这里二进制代码有510字节
dw 0xaa55 ; 结束标志, 最后两个字节是 aa55 才是符合要求的引导程序, 代码共计512字节
$
: 汇编后 当前行的地址$$
: 汇编后 一个节(section)开始处的地址$-$$
: 本行距离本节开始处的相对距离(这段代码只有一节, 所以就是程序的开头)
操作步骤
制作镜像文件
- 准备好
boot.asm
后, 编译成boot.bin
$ nasm boot.asm -o boot.bin
- 制作512字节的镜像文件
boot.img
$ dd if=boot.bin of=boot.img bs=512 count=1 conv=notrunc
写Bochs的配置文件
使用自己的bochs配置文件, 不用默认的
# file_name: bochsrc
##############################################################
# Configuration file for Bochs
###############################################################
# how much memory the emulated machine will have
megs: 32
# filename of ROM images
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/vgabios/vgabios.bin
# what disk images will be used
floppya: 1_44=boot.img, status=inserted
# choose the boot disk.
boot: floppy
# where do we send log messages?
log: bochsout.txt
# disable the mouse
mouse: enabled=0
# enable key mapping, using US layout as default.
keyboard_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map
- 注意
romimage
vgaromimage
路径要根据自身情况做修改, 否则可能会报错couldn't open ROM image file
floppya
之后的文件名也要按自己的情况修改
启动
- 在window中启动 VcXsrv (XLanuch) 安装看这里 link
- 在命令行中运行
$ bochs -f bochsrc
- 顺利的话可以看到
- 输入 b 0x7c00 (在0x7c00处设置断点)
- 输入 c (让代码执行), 再输入c
最后看到
成功了!
计算机做的事情
- 计算机检查软盘的0面0磁道1扇区, 如果发现它以0xaa55结束(一个特殊的标记), 则BIOS把它当成引导扇区
- 512字节的内容被转载到内存 0000:7c00 处 ?为什么是7c00
- 跳转到 0000:7c00 处, 将控制权交给这段引导代码, CPU开始执行代码逻辑
可能出现的错误情况
Message: dlopen failed for module ‘x’: file not found
没有图形界面的模块, 看这里 link
好命令和附录
- 反汇编
$ ndisasm -o 0x7c00 boot.bin >> disboot.asm
- bochs 调试指令
reference
- https://blog.csdn.net/hdxxjhuahuo/article/details/78234996
- <<oranges: 一个操作系统的实现>>