1.1 运行一个操作系统
使用Bz1621.lzh工具,这是一个二进制编辑器,输入指定内容:
保存为helloos.img,接下来我们有两种方式运行此操作系统。
PC模拟器
在一个文件中,放入img文件和!cons_9x.bat和!cons_nt.bat文件,这两个文件有一个能打开,然后写入一个文件run.bat。
Copy helloos.img …\z_tools\qemu\fdimage0.bin
…\z_tools\make.exe -C …\z_tools\qemu
该文件的路径应该为
G:\操作系统\tolset\helloos0
双击!cons_nt.bat,打开之后,输入命令run,即可运行。
VM虚拟机
首先创建虚拟机。
然后导入软盘img文件。
开启虚拟机即可。
1.2 使用汇编语言
这里使用的汇编语言编译器是nask,是日本川河秀实写的。
写入如下汇编语言:
; hello-os
; TAB=4
; 以下这段是标准的FAT12格式软盘专用代码
DB 0xeb, 0x4e, 0x90
DB "HELLOIPL" ; 启动区的名称可以是任意8字节的字符串
DW 512 ; 每个扇区的大小必须为512字节
DB 1 ; 簇的大小必须为一个扇区
DW 1 ; FAT的起始位置
DB 2 ; FAT的个数(必须为2)
DW 224 ; 根目录的大小(一般设为224项)
DW 2880 ; 该磁盘的大小(必须是2880扇区)
DB 0xf0 ; 磁盘的种类(必须是9扇区)
DW 9 ; FAT的长度(必须是九扇区)
DW 18 ; 一个磁道(track)有几个扇区(必须是18)
DW 2 ; 磁头数(必须是2)
DD 0 ; 不使用分区,必须是0
DD 2880 ; 重写一次磁盘大小
DB 0,0,0x29 ; 意义不明,固定
DD 0xffffffff ; 可能是卷标号码
DB "HELLO-OS " ; 磁盘的名称(11字节)
DB "FAT12 " ; 磁盘格式名称(8字节)
RESB 18 ; 空出18字节
; 程序主体
DB 0xb8, 0x00, 0x00, 0x8e, 0xd0, 0xbc, 0x00, 0x7c
DB 0x8e, 0xd8, 0x8e, 0xc0, 0xbe, 0x74, 0x7c, 0x8a
DB 0x04, 0x83, 0xc6, 0x01, 0x3c, 0x00, 0x74, 0x09
DB 0xb4, 0x0e, 0xbb, 0x0f, 0x00, 0xcd, 0x10, 0xeb
DB 0xee, 0xf4, 0xeb, 0xfd
; 信息显示部分
DB 0x0a, 0x0a ; 两个换行
DB "hello, world"
DB 0x0a ; 换行
DB 0
RESB 0x1fe-$ ; 填写0x00,直到0x001fe
DB 0x55, 0xaa
; 以下是启动区以外部分的输出
DB 0xf0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00
RESB 4600
DB 0xf0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00
RESB 1469432
编写asm.bat将汇编代码编译成img。
…\z_tools\nask.exe helloos.nas helloos.img
1.3 汇编与Makefile
启动区内容的装载地址。
:::info
0x00007c00-0x00007dff
:::
这里我们只用nask制作512字节的启动区。ipl.nas:
; hello-os
; TAB=4
ORG 0x7c00 ; このプログラムがどこに読み込まれるのか
; 以下は標準的なFAT12フォーマットフロッピーディスクのための記述
JMP entry
DB 0x90
DB "HELLOIPL"
DW 512
DB 1
DW 1
DB 2
DW 224
DW 2880
DB 0xf0
DW 9
DW 18
DW 2
DD 0
DD 2880
DB 0,0,0x29
DD 0xffffffff
DB "HELLO-OS "
DB "FAT12 "
RESB 18
entry:
MOV AX,0 ; 寄存器初始化
MOV SS,AX
MOV SP,0x7c00
MOV DS,AX
MOV ES,AX
MOV SI,msg
putloop:
MOV AL,[SI]
ADD SI,1 ; SI加1。
CMP AL,0
JE fin
MOV AH,0x0e ; 单字符显示功能
MOV BX,15 ; 指定字符颜色
INT 0x10 ; 调用显卡BIOS
JMP putloop
fin:
HLT ; 让CPU停止,等待指令
JMP fin ; 无限循环
msg:
DB 0x0a, 0x0a ; 换行两次
DB "hello, world"
DB 0x0a ; 换行
DB 0
RESB 0x7dfe-$ ; 填充0直到到0x7dfe
DB 0x55, 0xaa
Makefile:
# 默认动作
default :
../z_tools/make.exe img
# 文件生成规则
ipl.bin : ipl.nas Makefile
../z_tools/nask.exe ipl.nas ipl.bin ipl.lst
helloos.img : ipl.bin Makefile
../z_tools/edimg.exe imgin:../z_tools/fdimg0at.tek \
wbinimg src:ipl.bin len:512 from:0 to:0 imgout:helloos.img
# 命令
asm :
../z_tools/make.exe -r ipl.bin
img :
../z_tools/make.exe -r helloos.img
run :
../z_tools/make.exe img
copy helloos.img ..\z_tools\qemu\fdimage0.bin
../z_tools/make.exe -C ../z_tools/qemu
install :
../z_tools/make.exe img
../z_tools/imgtol.com w a: helloos.img
clean :
-del ipl.bin
-del ipl.lst
src_only :
../z_tools/make.exe clean
-del helloos.img
批处理文件make.bat:
…\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9
只需要输入
make run
它会首先生成ipl.bin,然后生成helloos.img文件,然后使用make.exe(make.exe 会让Makefile文件发挥作用),然后启动模拟器。
“make clean”:删除掉除最终成果意外的所有中间生成文件,把硬盘清理干净。
“make src_only”:把源程序以外的文件全部都删除干净。
“make”:不带参数时,默认动作是“make img”。
1.4 总结
最终我们的文件中需要有如下文件。
前两个是终端自然不必多说。
ipl.nas中的内容为:
; hello-os
; TAB=4
ORG 0x7c00 ; 指明程序的装在位置
; 以下的计述用于标准FAT12格式的软盘
JMP entry
DB 0x90
DB "dayedaye" ; 启动区的名称可以是任意字符串(8字节)
DW 512 ; 每个扇区的大小(必须为512字节)
DB 1 ; 簇的大小(必须为一个扇区)
DW 1 ; FAT的起始位置(一般从第一个扇区开始)
DB 2 ; FAT的个数(必须为2)
DW 224 ; 根目录的大小(一般设成224项)
DW 2880 ; 该磁盘的大小(必须是2880扇区)
DB 0xf0 ; 磁盘的种类(必须是0xf0)
DW 9 ; FAT的长度(必须是9扇区)
DW 18 ; 一个磁道(track)有几个扇区(必须是18)
DW 2 ; 磁头数(必须是2)
DD 0 ; 不使用分区,必须是0
DD 2880 ; 重写一次磁盘大小
DB 0,0,0x29 ; 意义不明,固定
DD 0xffffffff ; 可能是卷标号码
DB "daye-OS " ; 磁盘的名称(10字节)
DB "FAT12 " ; 磁盘格式名称(字节)
RESB 18 ; 先空出18字节
; 程序主体
entry:
MOV AX,0 ; 初始化寄存器
MOV SS,AX
MOV SP,0x7c00
MOV DS,AX
MOV ES,AX
MOV SI,msg
putloop:
MOV AL,[SI]
ADD SI,1 ; 给SI+1
CMP AL,0
JE fin
MOV AH,0x0e ; 显示一个文字
MOV BX,15 ; 指定字符颜色
INT 0x10 ; 调用显卡BIOS
JMP putloop
fin:
HLT ; 让CPU停止,等待指令
JMP fin ; 无限循环
msg:
DB 0x0a, 0x0a ; 换行两次
DB "hello, world"
DB 0x0a ; 换行
DB 0
RESB 0x7dfe-$ ; 填写0x00,直到0x7dfe
DB 0x55, 0xaa
Makefile里面则是一些我们希望实现的功能的命令。
# 默认执行
default :
../tools/make.exe img
# 文件生成规则
ipl.bin : ipl.nas Makefile
../tools/nask.exe ipl.nas ipl.bin ipl.lst
helloos.img : ipl.bin Makefile
../tools/edimg.exe imgin:../tools/fdimg0at.tek \
wbinimg src:ipl.bin len:512 from:0 to:0 imgout:helloos.img
# 命令
asm :
../tools/make.exe -r ipl.bin
img :
../tools/make.exe -r helloos.img
run :
../tools/make.exe img
copy helloos.img ..\tools\qemu\fdimage0.bin
../tools/make.exe -C ../tools/qemu
install :
../tools/make.exe img
../tools/imgtol.com w a: helloos.img
clean :
-del ipl.bin
-del ipl.lst
src_only :
../tools/make.exe clean
-del helloos.img
看到这里我们还需要注意一件事,就是我们的操作系统的文件目录中,还需要有一个tools文件,文件中存放了一些工具,例如make.exe,nask.exe,edimg.exe,fdimg0at.tek和fdimage0.bin等。<br />make.bat则是批处理文件。
..\tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9