保护模式下的一段代码,但找不到bug(高手请进)
%include "pm.inc "
org 0100h
jmp LABEL_BEGIN
[SECTION .gdt]
LABEL_GDT: Descriptor 0, 0, 0
LABEL_DESC_CODE32: Descriptor 0, SegCode32Len-1, DA_C+DA_32
LABEL_DESC_STACK: Descriptor 0, TopOfStack-1, DA_DRWA+DA_32
LABEL_DESC_DATA: Descriptor 0, DataLen-1, DA_DRWA+DA_32
LABEL_DESC_RING3: Descriptor 0, SegRing3Len-1, DA_C+DA_32+DA_DPL3
LABEL_DESC_STACK3: Descriptor 0, TopOfStack3-1, DA_DRWA+DA_32+DA_DPL3
LABEL_DESC_VEDIO : Descriptor 0B8000h, 0ffffh , DA_DRW+DA_DPL3
GdtLen equ $-LABEL_GDT
GdtPtr dw GdtLen -1
dd 0
SelectorCode32 equ LABEL_DESC_CODE32 -LABEL_GDT
SelectorStack equ LABEL_DESC_STACK -LABEL_GDT
SelectorData equ LABEL_DESC_DATA -LABEL_GDT
SelectorRing3 equ LABEL_DESC_RING3 -LABEL_GDT+SA_RPL3
SelectorStack3 equ LABEL_DESC_STACK3 -LABEL_GDT+SA_RPL3
SelectorVedio equ LABEL_DESC_VEDIO -LABEL_GDT+SA_RPL3
[SECTION .data1]
ALIGN 32
[BITS 32]
LABEL_DATA:
PMMessage db "In Protect Model ", 0
PMMessageOffset equ PMMessage-$$
DataLen equ $-LABEL_DATA
;END OF [SECTION .data1]
[SECTION .ss1]
ALIGN 32
[BITS 32]
LABEL_STACK :
times 512 db 0
TopOfStack equ $-LABEL_STACK
;END OF [SECTION .SS1]
[SECTION .ss2]
ALIGN 32
[BITS 32]
LABEL_STACK3:
times 512 db 0
TopOfStack3 equ $-LABEL_STACK3
;END OF [SECTION .ss2]
LABEL_BEGIN :
mov ax, cs
mov ds, ax
mov es, ax
mov ss, ax
mov sp,0100h
movax, cs
movzxeax, ax
shl eax,4
add eax,LABEL_CODE32
mov word [LABEL_DESC_CODE32+2],AX
shr eax,16
mov byte [LABEL_DESC_CODE32+4],AH
mov byte [LABEL_DESC_CODE32+7],AL
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],AH
mov byte [LABEL_DESC_STACK+7],AL
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],AH
mov byte [LABEL_DESC_DATA+7],AL
xor eax,eax
mov ax,cs
shl eax,4
add eax,LABEL_RING3
mov word [LABEL_DESC_RING3+2],AX
shr eax,16
mov byte [LABEL_DESC_RING3+4],AH
mov byte [LABEL_DESC_RING3+7],AL
xor eax,eax
mov ax,ds
shl eax,4
add eax,LABEL_STACK3
mov word [LABEL_DESC_STACK3+2],AX
shr eax,16
mov byte [LABEL_DESC_STACK3+4],AH
mov byte [LABEL_DESC_STACK3+7],AL
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[SECTION .c32]
ALIGN 32
[BITS 32]
LABEL_CODE32:
mov ax, SelectorStack
mov ss, ax
mov ax,SelectorVedio
mov gs,ax
mov ax,SelectorData
mov ds,ax
mov esp, TopOfStack
xor edi,edi
xor esi,esi
mov edi,(80*10+0)*2
mov esi,PMMessageOffset
mov ah,0ch
cld
.1:
lodsb
test al,al
jz .2
mov [gs:edi],ax
add edi,2
jmp .1
.2:
push SelectorStack3
push TopOfStack3
push SelectorRing3
push 0
retf
SegCode32Len equ $-LABEL_CODE32
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[SECTION .ring3]
ALIGN 32
[BITS 32]
LABEL_RING3:
mov ax,SelectorVedio
mov gs,ax
mov edi,(80*12+0)*2
mov ah,0ch
mov al, '3 '
mov [gs:edi],ax
jmp $
SegRing3Len equ $-LABEL_RING3
这段代码是首先进入保护模式,然后再进入低特权级,找了一天的bug,可编译(编译器:nasm),但不能执行,高手帮我找一下bug。
[解决办法]
mov byte [LABEL_DESC_CODE32+4],AH
mov byte [LABEL_DESC_CODE32+7],AL
填充代码段描述符时,基地址的高2字节填充错误,顺序颠倒了,更正如下:
mov byte [LABEL_DESC_CODE32+4],AL
mov byte [LABEL_DESC_CODE32+7],AH