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

来源:互联网 发布:淘宝店铺订单风险违规 编辑:程序博客网 时间:2024/05/16 09:32

开发操作系统一直被认为是高不可攀的事,的确,开发一个安全的,完整的,健全的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 0x7c00    ;程序始位置

;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            ; 0x1a   2     Number of heads

       db  0          ; 0x1c   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,0x7c00 ;针赋为0x7c00,既引程序初始地址

       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

fin:

       hlt            ;cpu停止

       jmp   fin      ;死循

      

msg:

       db   0x0a,0x0a ;

       db   "Colimas Simple OS Initialize..."

       db   0x0a      ;

       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

运行结果

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 0x7c00    ;程序始位置

;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            ; 0x1a   2     Number of heads

       db  0          ; 0x1c   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,0x7c00 ;针赋为0x7c00,既引程序初始地址

       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

       mov  ah,0x02   ;status of reading disk sector

       mov  al,1      ;number of sectors read

       mov  bx,0      ;0x0820(es) * 16 + 0(bx)=0x8200, 0x7e00~0x9fbff

       mov  dl,0x00   ;A drive

       int  0x13        ;Read

       jc   error     ;on error goto label error

      

       mov    si,msg2    ;source index msg2第一个字符地址

putloop2:

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

       add  si,1      ;si+1

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

       je   fin       ;

       mov  ah,0x0e   ;示字符

       mov  bx,15     ;灰色

       int  0x10      ;video bios中断

       jmp  putloop2

error:

       mov    ax,0

       mov    es,ax

       mov    si,errmsg

putloop3:

       mov    al,[si]  

       add  si,1    

       cmp  al,0     

       je   fin      

       mov  ah,0x0e 

       mov  bx,15   

       int  0x10    

       jmp  putloop3

;完后,休眠

fin:

       hlt                     ;cpu停止

       jmp   fin      ;死循

msg:

       db     0x0a,0x0a ;

       db   "Colimas Simple OS Initialize..."

       db   0x0a      ;

       db   "Reading disk..."

       db   0x0a      ;

       db   0

      

msg2:

       db   "1 sector was read."

       db   0x0a

       db   0

errmsg:

       db   "load error"

       db   0x0a

       db   0

      

       resb 0x1fe-($-$$)

       db   0x55,0xaa ;

编译与运行:

$ nasm boot.s -o boot.bin

$ cp boot.bin ../qemu

$ ./qemu-win.bat

(未完)-C语言的开始,编写图形显示程序。

 
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大嫂被翻天了佐佐木b希中文7 美丽的大嫂中文字幕影迅雷下载 邻居的妻子中文字幕下载 神马电影院电影中文 神马电影院理论中文 女儿的朋友5中文神马电影院 97手机2019电影院专用版中文 厨房里进入朋友的老婆 中文版电影院 神马电影院 中文 儿子的妻子中文字幕 下载 樱桃中文版电影院 大富豪电影院韩国中文 老婆的闺蜜们喝醉了在家 中文潮人影院您手中的电影院 朋友不在晚上去他家干 趁兄弟喝醉上他女朋友在线播放 神马电影院午伦中文 朋友喝醉上其妻 我朋友的妻子韩语中文2018 在朋友家趁朋友喝醉上他老婆 日本朋友的妻子和母亲中文版 偷朋友的妻子在线中文播放 邻居的妻子日本中文2018 朋友的妻子日本中文版7 朋友的妈l妈中文字电影 母亲在美国被黑人证服 妻子报恩献身张局长加强版2 日本朋友的家教妻子中文字幕 妻陪领导睡全文阅读 看着妻子被领导玩电影 看着领导糟蹋妻子 带妻子群 p 原千岁的牛仔裤剪开裆 俩黑人夹击得我直嗤尿液故事 ktv妻子被下药 健太与正太原千岁 强睡大嫂图片 和大嫂睡 播放 晚上和大嫂睡视频 余罪跟大嫂睡在哪一集