我最近忙于阅读《oranges一个操作系统的实现》,感觉作者功力还算不错,可以作为长期编写OS的参考书,目前已经读了30几页,读到了进入保护模式的一段代码,不知怎么的有些感动,因此提前更新,将这段代码放在下面:
I have been reading ‘Oranges to realize a operating system’, and I suppose the writer is quite knowledgeable. Since this could be regarded a reference book for me to write the OS. And I have read for 20 pages, the content is about protected mode. After comprehending the code about jumping to protected mode, I was moved by it unreasonably…thus I would like to put those code at here:
%include "pm.inc"
org 07c00h
jmp LABEL_BEGIN
[Section .GDT]
;GDT
; 段基址, 段界限, 属性
LABEL_GDT: Descriptor 0, 0, 0 ;空描述符
LABEL_DESC_CONDE32: Descriptor 0, SegCode32Len - 1, DA_C + DA_32 ;非一致代码段
LABEL_DESC_VIDEO: Descriptor 0B8000h, 0ffffh, DA_DRW ;显存首地址
;GDT结束
GdtLen equ $ - LABEL ;GDT长度
Gdtptr dw GdtLen - 1 ;GDT界限
????
;GDT选择子
SelectorCode32 equ LABEL_DESC_CONDE32 - LABEL_GDT
SelectorVideo equ LABEL_DESC_VIDEO - LABEL_GDT
;END of [SECTION .gdt]
[SECTION .s16]
[BITS 16]
LABEL_BEGIN:
mov ax, cs
mov ds, ax
mov es, as
mov ss, ax
mov sp, 0100h
;初始化32位代码段描述符
xor eax, eax
mov ax, cx
shl eax, 4
add eax, LABEL_DESC_CONDE32
shr word [LABEL_DESC_CONDE32 + 4], al
mov byte [LABEL_DESC_CONDE32 + 7], ah
;为加载GDTR做准备
xor eax, eax
mov ax, ds
shl eax, 4
add eax, LABEL_GDT ;eax <- gdt基地址
mov dword [GdtPrt + 2], eax ;[GdtPtr + 2] <- gdt基地址
lgdt [GdtPtr]
;关中断
cli
;打开地址线A20
in al, 92h
or al, 00000010b
;准备切换到保护模式
mov eax, cr0
or eax, 1
mov cr0, eax
;真正进入保护模式
jmp dword SelectorCode ;执行这一句会把SelectorCode32装入cs
;END of [SECTION .s16]
[SECTION .s32]
[BITS 32]
mov ax, SelectorVideo
mov gs, ax ;视频段选择子(目的)
mov edi, (80 * 11 + 79) * 2 ;屏幕第11行,第79列
mov ah, 0ch ;0000:黑底 1100:红字
mov al, 'P'
mov [gs:edi], ax
;到此停止
jmp $
SegCode32Len equ $ - LABEL_SEG_CODE32
;END of [SECTION .s32]
And I also done some works about Assembly language in recent, but have not been compiled well, so I would not post it this time.