最近一直在忙着考试所以很少写博客了
今天想写一点关于前面一段时间再看的《30天编写自己的操作系统》这本书,这本书目前看书看到了17天,大概340多页,自己按照作者代码的思路然后加上一点自己的思路也算是写了自己的一个版本的os,目前整个进度是到了这本书的16天的进度,其中写了很多关于自己的东西,因为我发觉其实作者在这本书中屏蔽了很多操作系统的知识,虽然还是可以学到很多知识,这个的话可能也是作者为了让本书成为操作系统编写的入门书籍吧。在跟着书籍的进度来编写os的同时,我自己也在看linux0.01的内核代码,发现了部分的相似之处,也有很多的不同,所以建议在看这本书的程序猿们可以同时看一看linux0.01的内核代码,代码不是特别的复杂,还是能看懂的。
今天和大家讨论一下其中的ipl.nas 和linux0.01 中的boot.s
boot.s
|
| boot.s is loaded at 0x7c00 by the bios-startup routines, and moves itself
| out of the way to address 0x90000, and jumps there.(boot.s将被BIOS启动例程自动加载到内存的0x07c00处,然后将自己移动到内存地址0x90000处,然后跳转该处的go标签继续运行。PS:看下面的jmpigo,INITSEG代码)
|
| It then loads the system at 0x10000, using BIOS interrupts. Thereafter
| it disables all interrupts, moves the system down to 0x0000, changes
| to protected mode, and calls the start of system. System then must
| RE-initialize the protected mode in it's own tables, and enable
| interrupts as needed.
{这个时候利用bios的中断,加载系统到0x10000,紧接着屏蔽所有的中断,移动系统到0x0000,然后切换到保护模式,然后调用系统的启动(main)函数,系统必须在自己的描述符表中重新初始化保护模式,需要的时候允许中断}
|
| NOTE! currently system is at most 8*65536 bytes long. This should be no
| problem, even in the future. I want to keep it simple. This 512 kB
| kernel size should be enough - in fact more would mean we'd have to move
| not just these start-up routines, but also do something about the cache-
| memory (block IO devices). The area left over in the lower 640 kB is meant
| for these. No other memory is assumed to be "physical", ie all memory
| over 1Mb is demand-paging. All addresses under 1Mb are guaranteed to match
| their physical addresses.
{注意:当前系统的大小最大是8*65536 字节,即使是在将来,这个应该没有什么问题。我希望能使它保持简单。512KB字节的内核应该足够了——实际上更大的内核意味着内核不仅仅只有这些启动例程,同时也包含了块设备的缓存相关的代码。余下的640KB可以包含像块设备缓存等类似的这些代码。其他的内存都没有被假设是物理意义上的(也就是说可以直接访问的),所有超过1M的内存都必须要映射。所有1M以下的内存地址都与物理地址一一对应。}
|
| NOTE1 abouve is no longer valid in it's entirety. cache-memory is allocated
| above the 1Mb mark as well as below. Otherwise it is mainly correct.
|
| NOTE 2! The boot disk type must be set at compile-time, by setting
| the following equ. Having the boot-up procedure hunt for the right
| disk type is severe brain-damage.
| The loader has been made as simple as possible (had to, to get it
| in 512 bytes with the code to move to protected mode), and continuos
| read errors will result in a unbreakable loop. Reboot by hand. It
| loads pretty fast by getting whole sectors at a time whenever possible.
| 1.44Mb disks:
sectors = 18
| 1.2Mb disks:
| sectors = 15
| 720kB disks:
| sectors = 9
.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text
BOOTSEG = 0x07c0 | BIOS启动例程加载boot.s所在的内存地址
INITSEG = 0x9000 | 移动后的boot.s的内存地址
SYSSEG = 0x1000 | system loaded at 0x10000 (65536).系统被加载到0x10000(64KB处)
ENDSEG = SYSSEG + SYSSIZE | 系统在内存的末地址
entry start |移动boot.s到0x90000处
start:
mov ax,#BOOTSEG
mov ds,ax
mov ax,#INITSEG
mov es,ax
mov cx,#256|循环256次
sub si,si |初始化源基地址寄存器
sub di,di|初始化目的基地址寄存器
rep
movw | 移动字(=2byte)也就是说256*2=512byte
jmpi go,INITSEG | 跳转到0x90000:go执行(注意是已经复制到0x9000的程序的go)
go: mov ax,cs |程序移动到0x90000之后从0x90000:go标签开始执行
mov ds,ax
mov es,ax
mov ss,ax
mov sp,#0x400| arbitrary value >>512
mov ah,#0x03| read cursor pos
xor bh,bh
int 0x10
mov cx,#24
mov bx,#0x0007| page 0, attribute 7 (normal)
mov bp,#msg1| 打印msg1对应的信息"Loading system!"
mov ax,#0x1301| write string, move cursor
int 0x10
| ok, we've written the message, now | 加载系统到0x10000处
| we want to load the system (at 0x10000)
mov