%include "F:\虚拟机\玩转文件\pm.inc"
org 0100h
jmp Code16
[SECTION .Gdtdes]
LABLE_GDT :Descriptor 0,0,0
LABEL_DESC_NORMAL:Descriptor 0, 0ffffh,92h;
LABLE_CODE32 :Descriptor 0,SEGCODE32-1,4098H;
LABLE_DATA32 :Descriptor 0,SEGDATA32-1,92H;
LABEL_DESC_VIDEO :Descriptor 0B8000h,0ffffh, 92H;
LABLE_CODE16 :Descriptor 0,0ffffh,98H;
LABLE_STACK :Descriptor 0,TopofStack,4093H;
;LABLE_CALL_GATE: Gate seclectorcallcode32,0,0,8ch;
LABEL_LDT_VIDEO :Descriptor 0B8000h,0ffffh, 0f2H;
LABLE_LDT :Descriptor 0,LDTLen-1,82H;
LDT_DESC_TSS :Descriptor 0,TSSLen - 1, 89h; TSS
GdtLen equ $-LABLE_GDT
GdtPtr dw GdtLen-1
dd 0
SelectorTSS equ LDT_DESC_TSS - LABLE_GDT
SelectorNormal equ LABEL_DESC_NORMAL - LABLE_GDT
seclectorcode32 equ LABLE_CODE32 - LABLE_GDT
seclectorcode16 equ LABLE_CODE16 - LABLE_GDT
seclectordata32 equ LABLE_DATA32 - LABLE_GDT
SelectorVideo equ LABEL_DESC_VIDEO - LABLE_GDT
seclectorstack equ LABLE_STACK - LABLE_GDT
seclectorLDT equ LABLE_LDT - LABLE_GDT
;Selectorcallgate equ LABLE_CALL_GATE-LABLE_GDT;---------27+1
SelectorLDTVideo equ LABEL_LDT_VIDEO - LABLE_GDT
[SECTION .data]
ALIGN 32 ;
[BITS 32]
Data32 db "In protect model Now.LYHL",0;--------------------30
DataF equ Data32 - $$
Data16 db "In real model Now。LYHL ",0
DataN equ Data16 - $$
SEGDATA32 equ $-Data32
[SECTION .stack];------------------------------34+2+2
ALIGN 32
[BITS 32]
Stack:
times 512 db 0
TopofStack equ $-Stack-1
[SECTION .ccode16]
ALIGN 32
[BITS 16];----------------------------------43
Code16:
MOV AX,CS
MOV DS,AX
mov es,ax
mov [ww+3],ax
XOR EAX,EAX
;--------------------------------------------50
mov ax,cs
SHL EAX,4
ADD EAX,Data32
mov [LABLE_DATA32+2],ax
shr eax,16
mov [LABLE_DATA32+4],al
mov [LABLE_DATA32+7],ah
;---------------------58
XOR EAX,EAX
mov ax,cs
SHL EAX,4
ADD EAX,Code32
mov [LABLE_CODE32+2],ax
shr eax,16
mov [LABLE_CODE32+4],al;---------------------65
mov [LABLE_CODE32+7],ah
;----------------------------------------67
XOR EAX,EAX
mov ax,cs
SHL EAX,4
ADD EAX,LDTStack
mov [LDT_STACK+2],ax
shr eax,16
mov [LDT_STACK+4],al
mov [LDT_STACK+7],ah
XOR EAX,EAX
mov ax,cs
SHL EAX,4
ADD EAX,UseofStack
mov [LABLE_CALL_CODE32+2],ax
shr eax,16
mov [LABLE_CALL_CODE32+4],al
mov [LABLE_CALL_CODE32+7],ah
XOR EAX,EAX
mov ax,cs
SHL EAX,4
ADD EAX,MCode16
mov [LABLE_CODE16+2],ax
shr eax,16
mov [LABLE_CODE16+4],al
mov [LABLE_CODE16+7],ah
;-------------------------------------------77+10+9
XOR EAX,EAX
mov ax,cs
SHL EAX,4
ADD EAX,Stack
mov [LABLE_STACK+2],ax
shr eax,16
mov [LABLE_STACK+4],al;--------------84
mov [LABLE_STACK+7],ah
;---------------------------------86
mov ax,cs
SHL EAX,4
ADD EAX,LDT_LDT
mov [LABLE_LDT+2],ax
shr eax,16
mov [LABLE_LDT+4],al
mov [LABLE_LDT+7],ah
mov ax,cs
SHL EAX,4
ADD EAX,Cjum
mov [LDT_CODE32+2],ax
shr eax,16
mov [LDT_CODE32+4],al
mov [LDT_CODE32+7],ah
mov ax,cs
SHL EAX,4
ADD EAX,LDTTSS
mov [LDT_DESC_TSS+2],ax
shr eax,16
mov [LDT_DESC_TSS+4],al
mov [LDT_DESC_TSS+7],ah
xor eax,eax
mov ax,ds
shl eax,4
add eax,LABLE_GDT;---------------99+9
mov dword [GdtPtr+2],eax
lgdt [GdtPtr]
cli
;;----------------------------------95
in al,92h
or al,00000010b
out 92h,al
mov eax,cr0;--------------------------------100
or eax,1
mov cr0,eax
jmp dword seclectorcode32:0
LABEL_REAL:
mov ax,cs
mov ds, ax
mov es, ax
mov ss, ax
;--------------------------------------------111
IN AL,92H
AND AL,11111101b
OUT 92H,AL
sti;
mov ax,0B800h
mov es,ax
mov cx,10
mov edi,(80*5+5)*2
mov ah,0Ch
mov esi,Data16
sv: ;----------------------------125
mov al,[ds:esi]
test al,al
jz ddos
mov [es:edi],ax
inc esi
add edi,2
;-------------------------------------------------133
jmp sv
ddos:
MOV AX,4C00H
INT 21H
[SECTION .code32]
ALIGN 32
[BITS 32]
Code32:
;------------------------------------------144
xor eax,eax
mov ax,seclectordata32
mov ds,ax
xor edi,edi
mov edi,(80*10+0)*2
xor esi,esi
mov esi,DataF
;------------------------------------------153
mov ax,seclectorstack
mov ss,ax
mov esp,TopofStack
mov ax,SelectorVideo
mov gs,ax
mov ah,0Ch
xy:
mov al,[esi]
test al,al;-----------164+1
jz qq
mov [gs:edi],ax
inc esi
add edi,2
;--------------------------------------169
jmp xy
qq:
mov ax,seclectorLDT
lldt ax
jmp LDTseclectorLDT:0
Mjup:
jmp seclectorcode16:0
Mojup equ Mjup-Code32
SEGCODE32 equ $ - Code32
[SECTION .callcode32]
ALIGN 32
[BITS 32]
UseofStack:
mov ax,SelectorLDTVideo
mov gs,ax
mov ah,0Ch
mov edi,(80*20+0)*2
xor ecx,ecx
mov cx,10
kk:
mov al,6
mov [gs:edi],ax;
add edi,2
loop kk
call Selectorcallgate:0;;;;;;;;;;;;;;;;;;;调用门返回ring0,TSS发挥重要作用
SEGCALLCODE32 equ $ - UseofStack
[SECTION .code16]
ALIGN 32
[BITS 16]
MCode16:
mov ax,SelectorNormal
mov ds,ax;---------------------199+2+2+3+1
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
mov eax,cr0
and al,11111110b
mov cr0,eax
ww:
jmp 0:LABEL_REAL
[SECTION .Ldtdes];
ALIGN 32
LDT_LDT:
LDT_CODE32 :Descriptor 0,LCode32-1,4098h
LABLE_CALL_CODE32:Descriptor0,SEGCALLCODE32-1,40f8H;******** LDT_STACK :Descriptor 0,TopofLDTStack,40f3H;
LABLE_CALL_GATE: Gate seclectorcode32,Mojup,0,0ech;
;LDT_DESC_TSS :Descriptor 0,TSSLen - 1, 89h; TSS
LDTLen equ $-LDT_LDT
LDTseclectorLDT equ LDT_CODE32 - LDT_LDT+4;
seclectorcallcode32 equ LABLE_CALL_CODE32 - LDT_LDT+7;-------------------------257
LDTseclectorStack equ LDT_STACK - LDT_LDT+7;
Selectorcallgate equ LABLE_CALL_GATE-LDT_LDT+7;
;SelectorTSS equ LDT_DESC_TSS-LDT_LDT+4
[SECTION .LdtCode32]
ALIGN 32
[BITS 32]
Cjum:
;call seclectorcallcode32:0
mov ax,SelectorTSS
ltr ax
push LDTseclectorStack;
push TopofLDTStack;
push seclectorcallcode32
push 0
retf
;jmp seclectorcode32:Mojup
Cojum equ Cjum-$$
LCode32 equ $-Cjum
[SECTION .ldtstack];
ALIGN 32
[BITS 32]
LDTStack:
times 512 db 0
TopofLDTStack equ $-LDTStack-1
[SECTION .tss]
ALIGN 32
[BITS 32]
LDTTSS:
DD 0 ; Back
DD TopofStack ; 0 级堆栈
DD seclectorstack ;
DD 0 ; 1 级堆栈
DD 0 ;
DD 0 ; 2 级堆栈
DD 0 ;
DD 0 ; CR3
DD 0 ; EIP
DD 0 ; EFLAGS
DD 0 ; EAX
DD 0 ; ECX
DD 0 ; EDX
DD 0 ; EBX
DD 0 ; ESP
DD 0 ; EBP
DD 0 ; ESI
DD 0 ; EDI
DD 0 ; ES
DD 0 ; CS
DD 0 ; SS
DD 0 ; DS
DD 0 ; FS
DD 0 ; GS
DD 0 ; LDT
DW 0 ; 调试陷阱标志
DW $ - LDTTSS + 2 ; I/O位图基址
DB 0ffh ; I/O位图结束标志
TSSLen equ $ - LDTTSS