怎样使用C语言开发操作系统 (3)

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

5. 引导程序

我们自己的操作系统虽然写完了,但是还没有办法引导,使其拥有系统的控制权。你可以使用第三方的引导程序,只要它能够将我们的操作系统加载到内存的0x1000:0x0100,并且cpu工作在实模式下。如果实在找不到合适的引导程序,只有自己动手写一个了。

;

; bootsect.asm

; Copyright(C) 2004, Tian XiangYuan

;

.MODEL TINY,C

.386p

option expr32

option casemap:none

SYSSEG EQU 1000h

SYSOFF EQU 0100h

.CODE

ORG 7C00h

_start:

jmp begin

nop

DB 'BOOTSECT',0 ;magic

pack_size DB 16

DB 0 ;reserved

DW 60 ;sectors

DW SYSOFF ;buf_addr_off

DW SYSSEG ;buf_addr_seg

DD 2 ;sector_from

DD 0 ;sector_from_high

begin:

cli

mov ax,cs

mov ds,ax

mov es,ax

mov ss,ax

mov sp,0FFFFh

sti

mov cx,msg_load_len ;length

lea bp,msg_load ;es:bp

call display_msg

; read disk for my OS

lea si,pack_size

mov dl,80h

mov ax,4200h

int 13h ;使用LBA方式读硬盘

jc error

; test magic of my os

lea si,magic_test ;ds:si

mov ax,SYSSEG

mov es,ax

mov di,SYSOFF

add di,3 ;es:di

mov cx,magic_test_len

cld

test_again:

cmpsb

jnz error

loop test_again

push SYSSEG

push SYSOFF

retf ;转入操作系统执行

error:

mov ax,cs

mov es,ax

lea bp,msg_error ;es:bp

mov cx,msg_error_len

call display_msg

failure:

hlt

jmp failure

;cx : length of message

;es:bp : address of message

;void display_msg();

display_msg PROC NEAR C

;scrollup a line

push cx

push bp

mov ax,0601h

mov bh,07h

mov cx,0000h ;y/x

mov dx,184Fh ;y2/x2, 24/79

int 10h

pop bp

pop cx

;display message

mov ax,1301h

mov bx,000Ah

;mov cx,msg_error_len

mov dl,0 ;x

mov dh,24 ;y

;lea bp,msg_error ;es:bp

int 10h

ret

display_msg endp

.DATA

msg_load DB 'Loading......',0

msg_load_len DW $ - msg_load

msg_error DB 'NO BOOTER,please reboot!',0

msg_error_len DW $ - msg_error

magic_test DB 'TianXiangYuan',0

magic_test_len DW $ - magic_test

end _start

上面这段代码,是MBR的内容,他使用LBA方式读硬盘(支持大硬盘),将我们自己的操作系统读入0x1000:0x0100,然后转入操作系统执行。

很显然,我们的操作系统必须放在硬盘的No.2(LBA定位方式)扇区开始的60个扇区之内(实际上我们的操作系统远没有这么大),这时我们的引导程序的硬性规定,如果使用第三方的引导程序,也许会更方便。

可以自己开发安装程序,也可以使用WinHex工具将其写入硬盘。请一定注意数据安全,不要将硬盘分区搞坏,建议做好备份工作,或者使用没有重要数据的测试硬盘。顺便说一下,我是使用WinHex写入测试硬盘的。

这段引导程序的编译方式与操作系统一样,build.dat文件如下:

@echo off

set PATH=e:\masm615\bin;

set AS=e:\masm615\bin\ml.exe

set AFLAGS=/AT /W3 /WX /Gd /Zp1 /X /nologo

del *.obj *.com *.cod *.dbg *.pdb *.map

%AS% /c %AFLAGS% bootsect.asm

if errorlevel 1 goto error

%AS% %AFLAGS% /Fe"bootsect.com" bootsect.obj

if errorlevel 1 goto error

goto exit

:error

echo Failure......

:exit

pause

@echo on

6. 后记

我们自己的操作系统太原始了,连DOS都不如!在这里,主要介绍的是一种开发方法,旨在说明使用C语言如何开发像操作系统这样底层的软件。

闲暇时,将其移至Linux平台下开发,并且使用cpu的保护模式,毕竟这样才能发挥80x86系列cpu的强大功能。

欢迎提出宝贵意见:tianxiangyuan@sina.com.cn

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航