1个人开发操作系统-初篇

开发操作系统一直被认为是高不可攀的事,的确,开发一个安全的,完整的,健全的OS是非常复杂的工作,不是一两个人能完成的。但是一个简易的操作系统是可以由一个人在很短的时间开发出来的。我将陆续发表开发简易操作系统的全过程,尽力提供完整的源代码,参考资料和文字说明,我也是OS开发的初学者,希望能得到各位读者的技术支持。该简易操作系统我称为Colimas Simple OS,它包括引导程序,图形界面,鼠标和键盘驱动,内存管理,计时器,多任务处理,控制台,Shell命令,API

1. 开发环境

本文使用Qemu虚拟机,可以在Windows XP内虚拟软盘镜像文件。Qemu是开源软件,可以在http://fabrice.bellard.free.fr/qemu/下载。C编译器使用Cygwin下的GCC,汇编编译器使用Nasm,可以在http://sources.redhat.com/cygwin/下载。

操作系统开发在FAT12文件系统的软盘里,FAT12文件系统格式参考http://en.wikipedia.org/wiki/FAT32#FAT12Design部分

2. 引导程序

;boot.s

;Colimas Simple OS

org 0x<chmetcnv w:st="on" unitname="C" sourcevalue="7" hasspace="False" negative="False" numbertype="1" tcsc="0">7c</chmetcnv>00 ;程序始位置

;Fat12文件系格式参考 http://en.wikipedia.org/wiki/FAT32#FAT12

; |offset|Length|Descripton

jmp entry

db 0x90 ; 0x00 3 Jump instruction(to skip over header on boot)

db "COLIMAS " ; 0X03 8 OEM Name

db 512 ; 0x0b 2 Bytes per sector. The BIOS Parmeter Block starts here.

db 1 ; 0x0d 1 Sectors per cluster

db 1 ; 0x0e 2 Reserved sector count(including boot sector)

db 2 ; 0x10 1 Number of file allocation tables

db 224 ; 0x11 2 Maximum number o root directory entries

db 2880 ; 0x13 2 Total sector:80 tracks * 18 sectors * 2 sides=2880

db 0xf0 ; 0x15 1 Media descriptor

db 9 ; 0x16 2 Sectors per File Allocation Table

db 18 ; 0x18 2 Sectors per track

db 2 ; 0x<chmetcnv w:st="on" unitname="a" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1a</chmetcnv> 2 Number of heads

db 0 ; 0x<chmetcnv w:st="on" unitname="C" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1c</chmetcnv> 4 Hidden sectors

db 2880 ; 0x20 4 Total sectors again

db 0 ; 0x24 1 Physical drive number

db 0 ; 0x25 1 Reserved("current head")

db 0x29 ; 0x26 1 Signature

db 0xffffffff ; 0x27 4 ID(serial number)

db "Colimas OS "; 0x2b 11 Volume Label

db "FAT12 " ; 0x36 8 FAT file system type, FAT12

resb 18 ; 了安全添加18 bytes0

entry:

movax,0 ;寄存器初始化

mov ss,ax

mov sp,0x<chmetcnv w:st="on" unitname="C" sourcevalue="7" hasspace="False" negative="False" numbertype="1" tcsc="0">7c</chmetcnv>00 ;针赋为0x<chmetcnv w:st="on" unitname="C" sourcevalue="7" hasspace="False" negative="False" numbertype="1" tcsc="0">7c</chmetcnv>00,既引程序初始地址

mov ds,ax

mov es,ax

movsi,msg ;source indexmsg第一个字符地址

putloop:

moval,[si] ;第一个字符->al

add si,1 ;si+1

cmp al,0 ;0找最后一个字符,msg之后的byte0

je fin ;如果等于0fin

;video bios参考http://en.wikipedia.org/wiki/BIOS_interrupt_call

mov ah,0x0e ;示字符

mov bx,15 ;黑色

int 0x10 ;video bios中断

jmp putloop

fin:

hlt;cpu停止

jmp fin ;死循

msg:

db0x<chmetcnv w:st="on" unitname="a" sourcevalue="0" hasspace="False" negative="False" numbertype="1" tcsc="0">0a</chmetcnv>,0x<chmetcnv w:st="on" unitname="a" sourcevalue="0" hasspace="False" negative="False" numbertype="1" tcsc="0">0a</chmetcnv> ;

db "Colimas Simple OS Initialize..."

db 0x<chmetcnv w:st="on" unitname="a" sourcevalue="0" hasspace="False" negative="False" numbertype="1" tcsc="0">0a</chmetcnv> ;

db 0

resb 0x1fe-($-$$) ;510 bytes1止均设为0

db 0x55,0xaa ;sector

编译与运行:

$ nasm boot.s -o boot.bin

$ cp boot.bin ../qemu

$ ./qemu-win.bat

其中qemu-win.bat的内容是

@set SDL_VIDEODRIVER=windib

@set QEMU_AUDIO_DRV=none

@set QEMU_AUDIO_LOG_TO_MONITOR=0

qemu.exe -L . -m 32 -localtime -std-vga -fda boot.bin

运行结果

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 324pt; HEIGHT: 202.5pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CDOCUME~1%5C%E8%B6%99%E7%A3%8A%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.png"></imagedata></shape>

3引导程序2

上文已经作了简单的引导程序,引导程序利用了软盘的第一个Sector作为引导sector,下面开始读取软盘第2Sector

读取磁盘需要使用Disk Biosint 13中断,参考http://en.wikipedia.org/wiki/BIOS_interrupt_call#INT_13h_AH.3D02h:_Read_Sectors_From_Drive

;boot.s

;Colimas Simple OS

org 0x<chmetcnv w:st="on" unitname="C" sourcevalue="7" hasspace="False" negative="False" numbertype="1" tcsc="0">7c</chmetcnv>00 ;程序始位置

;Fat12文件系格式参考 http://en.wikipedia.org/wiki/FAT32#FAT12

; |offset|Length|Descripton

jmp entry

db 0x90 ; 0x00 3 Jump instruction(to skip over header on boot)

db "COLIMAS " ; 0X03 8 OEM Name

db 512 ; 0x0b 2 Bytes per sector. The BIOS Parmeter Block starts here.

db 1 ; 0x0d 1 Sectors per cluster

db 1 ; 0x0e 2 Reserved sector count(including boot sector)

db 2 ; 0x10 1 Number of file allocation tables

db 224 ; 0x11 2 Maximum number o root directory entries

db 2880 ; 0x13 2 Total sector:80 tracks * 18 sectors * 2 sides=2880

db 0xf0 ; 0x15 1 Media descriptor

db 9 ; 0x16 2 Sectors per File Allocation Table

db 18 ; 0x18 2 Sectors per track

db 2 ; 0x<chmetcnv w:st="on" unitname="a" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1a</chmetcnv> 2 Number of heads

db 0 ; 0x<chmetcnv w:st="on" unitname="C" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1c</chmetcnv> 4 Hidden sectors

db 2880 ; 0x20 4 Total sectors again

db 0 ; 0x24 1 Physical drive number

db 0 ; 0x25 1 Reserved("current head")

db 0x29 ; 0x26 1 Signature

db 0xffffffff ; 0x27 4 ID(serial number)

db "Colimas OS "; 0x2b 11 Volume Label

db "FAT12 " ; 0x36 8 FAT file system type, FAT12

resb 18 ; 了安全添加18 bytes0

entry:

mov ax,0 ;寄存器初始化

mov ss,ax

mov sp,0x<chmetcnv w:st="on" unitname="C" sourcevalue="7" hasspace="False" negative="False" numbertype="1" tcsc="0">7c</chmetcnv>00 ;针赋为0x<chmetcnv w:st="on" unitname="C" sourcevalue="7" hasspace="False" negative="False" numbertype="1" tcsc="0">7c</chmetcnv>00,既引程序初始地址

mov ds,ax

mov es,ax

mov si,msg ;source indexmsg第一个字符地址

putloop:

mov al,[si] ;第一个字符->al

add si,1 ;si+1

cmp al,0 ;0找最后一个字符,msg之后的byte0

je fin ;如果等于0fin

;video bios参考http://en.wikipedia.org/wiki/BIOS_interrupt_call

mov ah,0x0e ;示字符

mov bx,15 ;灰色

int 0x10 ;video bios中断

jmp putloop

;取磁2Sector数据

reading:

mov ax,0x0820

mov es,ax ;0x0820(es) * 16=0x8200

mov ch,0 ;track/cylinder number

mov dh,0 ;head number

mov cl,2 ;sector number

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值