您的位置:首页 > 其它

如何编写自己的操作系统(3)

2011-12-03 10:38 344 查看
这里我们简单介绍一下Stage2.asm这个程序。

整个程序代码如下:

[code] ; Note: Here, we are executed like a normal COM program, but we are still in
; Ring 0. We will use this loader to set up 32 bit mode and basic exception
; handling
; This loaded program will be our 32 bit kernal.
; We do not have the limitation of 512 bytes here, so we can add anything we
; want here!
 org 0x0 ; offset to 0, we will set segments later
 bits 16 ; we are still in real mode
; we are loaded at linear address 0x10000
jmp main
;*********************************
 ; Prints a String
 ; DS=>SI: 0 terminated string
;*********************************
Print:
 lodsb
 or al, al
 jz PrintDone
 mov ah, 0eh
 int 10h
 jmp Print
PrintDone:
 ret
 
;********************************
; Second Stage Loader Entry Point
;********************************
main:
 cli
 push cs
 pop ds
 ;xor ax, ax ; org 0x0500
 ;mov ds, ax ; org 0x0500
 
 mov si, Msg
 call Print
 
 cli
 hlt
 
;********************************
; Data section
;********************************
Msg db "Preparing to load operating system...",13,10,0
[/code]
这个程序非常简单,我们就不全部介绍了。只介绍第10行的这一句话。

由于我们在调用Print这个函数的时候,会使用DS:SI 来进行寻址,第10行把整个程序的偏移地址设为0,而在第38、39行重新设定了DS,所以不会产生问题(注意,Stage2.asm编译之后的二进制文件被加载到内存的0x0500处(即0x0050:0x0000))。

如果我们把第10行改成“org 0x0500”,那么就要在main函数中把DS设置为0,这样才能正确的打印出字符。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: