文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>引导扇区程序开发日志

引导扇区程序开发日志

时间:2007-06-13  来源:norn_larry

经过几天对boot sector的学习,终于把一个简单的boot程序写出来了,呵呵!

使用平台:
操作系统:ubuntu linux

使用工具:
bochs:一个很好的模拟器(emulator)
Gas:编译器
Ld:连接器
bximage:制作印象文件

程序功能:
在bios加电自检后,跳到存储器0000:07c00处,把控制权交给本断程序,打印出红色的“hello,OS”字符串

预备知识:
引导扇区的概念(在我的blog里面有)
AT&T汇编格式
gas与ld的参数使用

程序代码:
#boot.s
1    .text         #声明代码段
2    .globl _start  #声明外部标号_start
3    .code16         #声明使用16位操作数和地址,GAS默认是32位
4    _start:           #程序的入口
5    movw %cs,%ax
6    movw %ax,%ds
7    movw %ax,%es    #使ds,es和cs有同样的段开始地址,因为程序只声明了一个代码断。
8    call print             #print函数功能:打印红色字符串“Hello,OS”
9    jmp .                   #无限循环。 .代表当前地址
10  print:        
11             movw $BootMessage,%ax
12             movw %ax,%bp
13             movw $7,%cx
14             movw $0x1301,%ax
15             movw $0x000c,%bx
16             movb $0,%dl
17             int $0x10
18             ret
19  BootMessage:
20                .ascii  "Hello,OS"
21  .fill 510-(.-_start),1,0    #here also can use .org 0x1fe,0
22  .word 0xaa55              #引导扇区结束标志

关于.fill repeat,size,value的用法(程序的21行)
result, size and value are absolute expressions. This emits repeat copies of size bytes. Repeat may be zero or more. Size may be zero or more, but if it is more than 8, then it is deemed to have the value 8, compatible with other people's assemblers. The contents of each repeat bytes is taken from an 8-byte number. The highest order 4 bytes are zero. The lowest order 4 bytes are value rendered in the byte-order of an integer on the computer as is assembling for. Each size bytes in a repetition is taken from the lowest order size bytes of this number. Again, this bizarre behavior is compatible with other people's assemblers.

size and value are optional. If the second comma and value are absent, value is assumed zero. If the first comma and following tokens are absent, size is assumed to be 1.(参考:Using-AS)


关于.org new-lc,fill的用法(程序中的21行也可以用这个伪代码)
Advance the location counter of the current section to new-lc. new-lc is either an absolute expression or an expression with the same section as the current subsection. That is, you can't use .org to cross sections: if new-lc has the wrong section, the .org directive is ignored. To be compatible with former assemblers, if the section of new-lc is absolute, as issues a warning, then pretends the section of new-lc is the same as the current subsection.

.org may only increase the location counter, or leave it unchanged; you cannot use .org to move the location counter backwards.

Because as tries to assemble programs in one pass, new-lc may not be undefined. If you really detest this restriction we eagerly await a chance to share your improved assembler.

Beware that the origin is relative to the start of the section, not to the start of the subsection. This is compatible with other people's assemblers.

When the location counter (of the current subsection) is advanced, the intervening bytes are filled with fill which should be an absolute expression. If the comma and fill are omitted, fill defaults to zero.(参考:Using-AS)

生成bin文件

用as命令编译boot.s文件

as boot.s -o boot.o

用ld命令生成bin文件

ld --oformat binary -N -e _start -Ttext 0x7c00 -o boot.bin boot.o

注:ld可以被配置为支持多于一种的目标文件. binary表示没有程序头和其他信息,仅仅是一些裸数据。如果没有这个选项,将被默认链接为elf格式。-N把text和data节设置为可读写。- Ttext将text节起始地址设置为0x7c00(在jmp和数据引用等重定位链接时会用到这个参考值),所有的引用地址都是在7c00这个地址上加出 来的。-e选项指定程序入口点(参考:http//www.pangfan.com/u/shineyear/archives/2007/83.html)

用bximagez制作印象文件

1,命令行输入bximage(需有这个软件),出现下面的文字

========================================================================
                                bximage
                  Disk Image Creation Tool for Bochs
        $Id: bximage.c,v 1.32 2006/06/16 07:29:33 vruppert Exp $
========================================================================

Do you want to create a floppy disk image or a hard disk image?
Please type hd or fd. [hd]

2。回车,选择默认值,然后出现:

What kind of image should I create?
Please type flat, sparse or growing. [flat]

3。回车,选择默认值,出现:

What kind of image should I create?
Please type flat, sparse or growing. [flat]

4。回车,选择默认值,出现:

I will create a 'flat' hard disk image with
  cyl=20
  heads=16
  sectors per track=63
  total sectors=20160
  total size=9.84 megabytes

What should I name the image?
[c.img] 输入boot.img(可以选择其他文件名)

我们这下就做好了印象文件,现在我们要把boot.bin添加到boot.img里面,命令:

cp boot.bin boot.img

然后配置bochsrc.txt文件

ok,启动bochs,红色的hello,OS出现在屏幕上。





相关阅读 更多 +
排行榜 更多 +
爱是小事最新版

爱是小事最新版

休闲益智 下载
几何飞行内购修改版

几何飞行内购修改版

飞行射击 下载
别踩白块内购修改版

别踩白块内购修改版

休闲益智 下载