%include "pm.inc"
org 0100h
jmp start
;enter pm,call data segment,ring0->ring3->ring0,check memory,clean,study page
[SECTION .gdt]
GDT_DESC: Descriptor 0,0,0
MAIN_DESC: Descriptor 0,mainlen-1,DA_C+DA_32
DATA_DESC: Descriptor 0,datalen-1,DA_DRW+DA_DPL3
STK_DESC: Descriptor 0,TopOfStk+2,DA_DRW
DISP_DESC: Descriptor 0b8000h,0ffffh,DA_DRW+DA_DPL3
CODE3_DESC: Descriptor 0,code3len-1,DA_C+DA_32+DA_DPL3
STK3_DESC: Descriptor 0,TopOfStk3+2,DA_DRW+DA_DPL3
CODE0_DESC: Descriptor 0,code0len-1,DA_C+DA_32
TSS_DESC: Descriptor 0,tsslen-1,DA_386TSS
CALL_GATE: Gate SEL_CODE0,0,0,DA_386CGate+DA_DPL3
gdtlim: dw $-$-1
dd 0
SEL_MAIN EQU MAIN_DESC-$
SEL_DATA EQU DATA_DESC-$+SA_RPL3
SEL_STK EQU STK_DESC-$
SEL_DISP EQU DISP_DESC-$+SA_RPL3
SEL_CODE3 EQU CODE3_DESC-$+SA_RPL3
SEL_STK3 EQU STK3_DESC-$+SA_RPL3
SEL_CODE0 EQU CODE0_DESC-$
SEL_TSS EQU TSS_DESC-$
SEL_CALL_GATE EQU CALL_GATE-$+SA_RPL3
[SECTION .data1]
data:
_Msg: db "PM Now!",0
_Titles: db "BaseAddrL BaseAddrH LengthLow LengthHigh Type",0ah,0ah,0
_MemMsg1: db "AVL RAM:",0
_MemMsg2: db "RAM Size:",0
_Cr: db 0ah,0
_ARDSCnt: dd 0
_DisPos: dd 160*2+2*0
_MemAVL: dd 0
_MemTotal: dd 0
_MemCache: times 512 db 0
DataOst Msg
DataOst Titles
DataOst MemMsg1
DataOst MemMsg2
DataOst Cr
DataOst ARDSCnt
DataOst DisPos
DataOst MemAVL
DataOst MemTotal
DataOst MemCache
datalen equ $-$
[SECTION .stk]
[BITS 32]
stk:
times 512 db 0
TopOfStk equ $-$-2
[SECTION .stk3]
[BITS 32]
stk3:
times 512 db 0
TopOfStk3 equ $-$-2
[SECTION .tss]
ALIGN 32
[BITS 32]
tss:
dd 0
dd TopOfStk
dd SEL_STK
times 22 dd 0
dw 0
dw $-tss+2
db 0ffh
tsslen equ $-$
[SECTION .start]
ALIGN 32
[BITS 16]
start:
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
mov sp,0100h
mov ebx,0
mov di,_MemCache
mov cx,20
mov edx,534d4150h
chk:
mov eax,0e820h
int 15h
jc chk_err
add di,20
inc dword [_ARDSCnt]
cmp ebx,0
je chk_ok
jmp chk
chk_err:
mov dword [_ARDSCnt],0
chk_ok:
DescInit cs,main,MAIN_DESC
DescInit ds,data,DATA_DESC
DescInit ss,stk,STK_DESC
DescInit cs,code3,CODE3_DESC
DescInit ss,stk3,STK3_DESC
DescInit cs,code0,CODE0_DESC
DescInit ds,tss,TSS_DESC
mov ax,ds
movzx eax,ax
shl eax,4
add eax,GDT_DESC
mov dword [gdtlim+2],eax
lgdt [gdtlim]
cli
in al,92h
or al,00000010b
out 92h,al
mov eax,cr0
or eax,1
mov cr0,eax
jmp dword SEL_MAIN:0
[SECTION .main]
ALIGN 32
[BITS 32]
main:
mov ax,SEL_DATA
mov ds,ax
mov ax,SEL_DISP
mov gs,ax
mov ax,SEL_STK
mov ss,ax
mov sp,TopOfStk
mov esi,Msg
mov edi,0
mov ah,0ah
dispm:
mov al,[ds:esi]
cmp al,0
je disok
mov [gs:edi],ax
inc esi
add edi,2
jmp dispm
disok:
mov ax,SEL_TSS
ltr ax
push SEL_STK3
push TopOfStk3
push SEL_CODE3
push 0
retf
mainlen equ $-$
[SECTION .code3]
ALIGN 32
[BITS 32]
code3:
mov ah,0ah
mov al,'3'
mov [gs:20],ax
call SEL_CALL_GATE:0
code3len equ $-$
[SECTION .code0]
ALIGN 32
[BITS 32]
code0:
mov ax,Titles
call DispStr
mov ecx,[ARDSCnt]
mov esi,MemCache
d0:
push ecx
mov ecx,5
d1:
mov eax,[ds:esi]
call DispInt
mov edi,[DisPos]
mov al,'H'
mov ah,0ch
mov [gs:edi],ax
add esi,4
add edi,4
mov [DisPos],edi
loop d1
mov ax,Cr
call DispStr
sub esi,4
cmp byte [ds:esi],1
jne noavl
sub esi,8
mov eax,[ds:esi]
add [MemAVL],eax
jmp c0
noavl:
sub esi,8
c0:
mov eax,[ds:esi]
add [MemTotal],eax
add esi,12
pop ecx
loop d0
mov ax,Cr
call DispStr
mov ax,MemMsg1
call DispStr
mov eax,[MemAVL]
call DispInt
mov ax,Cr
call DispStr
mov ax,MemMsg2
call DispStr
mov eax,[MemTotal]
call DispInt
jmp $
%include "library.inc"
code0len equ $-$
关于代码中引用的文件,通过附件: