软件工程

本类阅读TOP10

·PHP4 + MYSQL + APACHE 在 WIN 系统下的安装、配置
·Linux 入门常用命令(1)
·Linux 入门常用命令(2)
·使用 DCPROMO/FORCEREMOVAL 命令强制将 Active Directory 域控制器降级
·DirectShow学习(八): CBaseRender类及相应Pin类的源代码分析
·基于ICE方式SIP信令穿透Symmetric NAT技术研究
·Windows 2003网络负载均衡的实现
·一网打尽Win十四种系统故障解决方法
·数百种 Windows 软件的免费替代品列表
·收藏---行百里半九十

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
怎样使用C语言开发操作系统 (3)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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.2LBA定位方式)扇区开始的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的强大功能。

欢迎提出宝贵意见:[email protected]




相关文章

相关软件