因为在实模式下不能改变段属性,我们预先设置一个Normal描述符,设置其属性,在返回实模式之前,将对应选择子SelectorNormal加载到ds,es,ss,使得返回实模式后段属性符合实模式要求.
头文件:pm.inc
%macro Descriptor 3
dw %2 & 0FFFFh
dw %1 & 0FFFFh
db (%1>>16) & 0FFh
dw ((%2>>8) & 0F00h) | (%3 & 0F0FFh)
db (%1>>24) & 0FFh
%endmacro
DA_32 EQU 4000h
DA_C EQU 98h
DA_DRW EQU 92h
DA_DRWA EQU 93h
%include "pm.inc"
;org 07c00h
org 0100h
jmp LABEL_BEGIN
[SECTION .gdt]
LABEL_GDT: Descriptor 0, 0, 0
LABEL_DESC_NORMAL: Descriptor 0, 0ffffh, DA_DRW
LABEL_DESC_CODE32: Descriptor 0, SegCode32Len - 1, DA_C + DA_32
LABEL_DESC_CODE16: Descriptor 0, 0ffffh, DA_C
LABEL_DESC_DATA: Descriptor 0, DataLen-1, DA_DRW
LABEL_DESC_STACK: Descriptor 0, TopOfStack, DA_DRWA+DA_32
LABEL_DESC_TEST: Descriptor 0500000h, 0ffffh, DA_DRW
LABEL_DESC_VIDEO: Descriptor 0B8000h, 0ffffh, DA_DRW
GdtLen equ $ - LABEL_GDT
GdtPtr dw GdtLen - 1
dd 0
SelectorNormal equ LABEL_DESC_NORMAL - LABEL_GDT
SelectorCode32 equ LABEL_DESC_CODE32 - LABEL_GDT
SelectorCode16 equ LABEL_DESC_CODE16 - LABEL_GDT
SelectorData equ LABEL_DESC_DATA - LABEL_GDT
SelectorStack equ LABEL_DESC_STACK - LABEL_GDT
SelectorTest equ LABEL_DESC_TEST - LABEL_GDT
SelectorVideo equ LABEL_DESC_VIDEO - LABEL_GDT
[SECTION .data1] ;数据段
;ALIGN 32
[BITS 32]
LABEL_DATA:
SPValueInRealMode dw 0
PMMessage: db "In Protect Mode now. ^-^", 0
OffsetPMMessage equ PMMessage - $$
StrTest: db "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0
OffsetStrTest equ StrTest - $$
DataLen equ $ - LABEL_DATA
[SECTION .gs] ;堆栈段
;ALIGN 32
[BITS 32]
LABEL_STACK:
times 512 db 0
TopOfStack equ $ - LABEL_STACK - 1
[SECTION .s16]
[BITS 16]
LABEL_BEGIN:
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
mov sp,0100h
mov [LABEL_GO_BACK_TO_REAL+3], ax
mov [SPValueInRealMode], sp
mov ax,cs ;初始化16位代码段描述符
movzx eax,ax
shl eax,4
add eax,LABEL_SEG_CODE16
mov word [LABEL_DESC_CODE16 + 2],ax
shr eax,16
mov byte [LABEL_DESC_CODE16 + 4],al
mov byte [LABEL_DESC_CODE16 + 7],ah
xor eax,eax ;初始化32位段描述符
mov ax,cs
shl eax,4
add eax,LABEL_SEG_CODE32
mov word [LABEL_DESC_CODE32 + 2],ax
shr eax,16
mov byte [LABEL_DESC_CODE32 + 4],al
mov byte [LABEL_DESC_CODE32 + 7],ah
xor eax,eax ;初始化数据段描述符
mov ax,ds
shl eax,4
add eax,LABEL_DATA
mov word [LABEL_DESC_DATA + 2],ax
shr eax,16
mov byte [LABEL_DESC_DATA + 4],al
mov byte [LABEL_DESC_DATA + 7],ah
xor eax,eax
mov ax,ds
shl eax,4
add eax,LABEL_STACK
mov word [LABEL_DESC_STACK + 2],ax
shr eax,16
mov byte [LABEL_DESC_STACK + 4],al
mov byte [LABEL_DESC_STACK + 7],ah
xor eax,eax
mov ax,ds
shl eax,4
add eax,LABEL_GDT
mov dword [GdtPtr + 2],eax
lgdt [GdtPtr]
cli
in al,92h
or al,00000010b
out 92h,al
mov eax,cr0
or eax,1
mov cr0,eax
jmp dword SelectorCode32:0
LABEL_REAL_ENTRY:
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
mov sp,[SPValueInRealMode]
in al,92h
and al,11111101b
out 92h,al
sti
mov ax,4c00h
int 21h
[SECTION .s32]
[BITS 32]
LABEL_SEG_CODE32:
mov ax,SelectorData
mov ds,ax
mov ax,SelectorTest
mov es,ax
mov ax,SelectorVideo
mov gs,ax
mov ax,SelectorStack
mov ss,ax
mov esp,TopOfStack
mov ah,0Ch
xor esi,esi
xor edi,edi
mov esi,OffsetPMMessage
mov edi,(80 * 10 + 0) * 2
cld
.1:
lodsb
test al,al
jz .2
mov [gs:edi],ax
add edi,2
jmp .1
.2:
call DispReturn
call TestRead
call TestWrite
call TestRead
jmp SelectorCode16:0
TestRead:
xor esi,esi
mov ecx,8
.loop:
mov al,[es:esi]
call DispAL
inc esi
loop .loop
call DispReturn
ret
TestWrite:
push esi
push edi
xor esi,esi
xor edi,edi
mov esi,OffsetStrTest
cld
.1:
lodsb
test al,al
jz .2
mov [es:edi],al
inc edi
jmp .1
.2:
pop edi
pop esi
ret
DispAL:
push ecx
push edx
mov ah,0Ch
mov dl,al
shr al,4
mov ecx,2
.begin:
and al,01111b
cmp al,9
ja .1
add al,'0'
jmp .2
.1:
sub al,0Ah
add al,'A'
.2:
mov [gs:edi],ax
add edi,2
mov al,dl
loop .begin
add edi,2
pop edx
pop ecx
ret
DispReturn:
push eax
push ebx
mov eax,edi
mov bl,160
div bl
and eax,0FFh
inc eax
mov bl,160
mul bl
mov edi,eax
pop ebx
pop eax
ret
SegCode32Len equ $ - LABEL_SEG_CODE32
[SECTION .s16code]
;ALIGN 32
[BITS 16]
LABEL_SEG_CODE16:
mov ax,SelectorNormal
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
mov eax,cr0
and al,11111110b
mov cr0,eax
LABEL_GO_BACK_TO_REAL:
jmp 0:LABEL_REAL_ENTRY
Code16Len equ $ - LABEL_SEG_CODE16
最后补充一点LDT方面的知识:
1:LDT:本地描述符表,与全局描述符表的区别是在selector选择子结构中,需要设置TI位为1,即在设置选择子时加上SA_TIL属性. 另外需要注意,类似gdtr寄存器,对于ldt也有相应的ldtr寄存器,但存放的是选择子,gdtr存放的是gdt的基地址.LABEL_GDT需要有全0的描述符描述,但LABEL_LDT不需要描述.
mov ax,SelectorLDT
lldt ax
我们可以把一个单独的任务所用到的所有东西封装在一个LDT中,这种思想是以后章节中的多任务处理的一个雏形.
[SECTION .gdt]
LABEL_GDT: Descriptor 0, 0, 0
LABEL_DESC_NORMAL: Descriptor 0, 0ffffh, DA_DRW
LABEL_DESC_CODE32: Descriptor 0, SegCode32Len - 1, DA_C + DA_32
LABEL_DESC_CODE16: Descriptor 0, 0ffffh, DA_C
LABEL_DESC_DATA: Descriptor 0, DataLen-1, DA_DRW
LABEL_DESC_STACK: Descriptor 0, TopOfStack, DA_DRWA+DA_32
LABEL_DESC_TEST: Descriptor 0500000h, 0ffffh, DA_DRW
LABEL_DESC_VIDEO: Descriptor 0B8000h, 0ffffh, DA_DRW
LABEL_DESC_LDT: Descriptor 0, LDTLen-1, DA_LDT
SelectorLDT equ LABEL_DESC_LDT - LABEL_GDT
;初始化LDT在GDT中的描述符
xor eax,eax
mov ax,ds
shl eax,4
add eax,LABEL_LDT
mov word [LABEL_DESC_LDT + 2],ax
shr eax,16
mov byte [LABEL_DESC_LDT + 4],al
mov byte [LABEL_DESC_LDT + 7],ah
;初始化LDT中的描述符
xor eax,eax
mov ax,ds
shl eax,4
add eax,LABEL_CODE_A
mov word [LABEL_LDT_DESC_CODEA + 2],ax
shr eax,16
mov byte [LABEL_LDT_DESC_CODEA + 4],al
mov byte [LABEL_LDT_DESC_CODEA + 7],ah
在保护模式中装载ldtr寄存器,跳转到自己的代码去运行
call DispReturn
mov ax,SelectorLDT
lldt ax
jmp SelectorLDTCodeA:0
初始化本地描述符表以及本地描述符表项对应的代码,即CODE_A
[SECTION .ldt]
LABEL_LDT:
LABEL_LDT_DESC_CODEA: Descriptor 0, CodeALen-1, DA_C + DA_32
LDTLen equ $ - LABEL_LDT
SelectorLDTCodeA equ LABEL_LDT_DESC_CODEA - LABEL_LDT + SA_TIL
[SECTION .la]
[BITS 32]
LABEL_CODE_A:
mov ax,SelectorVideo
mov gs,ax
mov edi,(80*12 + 0) *2
mov ah,0Ch
mov al,'L'
mov [gs:edi],ax
jmp SelectorCode16:0
CodeALen equ $ - LABEL_CODE_A
程序运行结果如下: