📄 page.asm
字号:
;************************
;文件:Page.asm *
;功能:演示分页内存管理 *
;************************
.386p
;----------------------
JUMP32 MACRO selector ,offsetv
DB 0EAH
DW offsetv
DW 0
DW selector
ENDM
;----------------------
JUMP16 MACRO selector,offsetv
DB 0EAH
DW offsetv
DW selector
ENDM
;----------------------
Descriptor STRUC
limitl dw 0
basel dw 0
basem db 0
attributes dw 0
baseh db 0
Descriptor ENDS
;===============================
Data Segment use16
gdt0 Descriptor <>
DataSel = $-gdt0
DataDes Descriptor <0ffffh,,,92H,>
CodeSel = $-gdt0
CodeDes Descriptor <0ffffh,,,98H,>
ProtSel = $-gdt0
ProtDes Descriptor <0ffffh,,,4098H,>
PageSel = $-gdt0
PageDes Descriptor <0ffffh,0000,20H,92h,>
VideoSel = $-gdt0
VideoDes Descriptor <0ffffh,0000H,1AH,92H,>
GdtLen = $-gdt0
GdtPtr dw GdtLen
dd 0
PageOK db 'Now,Page Is OK!',0
Data ends
;================================
Protect32 segment use32
assume cs:Protect32
Begin:
mov ax,PageSel
mov es,ax
cld ;初始化页表
xor edi,edi
mov eax,201000H
Again:
or eax,1
stosd
add eax,1000H
cmp edi,1000H
jnz Again
xor eax,eax
mov edi,1000H
Again1:
and eax,0FFFFF000H
or eax,1
stosd
add eax,1000H
cmp eax,200001H
jnz Again1
mov ecx,4
mov eax,0b8000h
or eax,1
mov edi,1A0000H
shr edi,22
shl edi,2
mov edi,es:[edi]
and edi,0FFFFEH
mov ebx,1A0000H
shl ebx,10
shr ebx,22
shl ebx,2
add edi,ebx
Again2: stosd
add eax,1000H
loop Again2
mov eax,200000H
mov cr3,eax
mov eax,cr0 ;分页
or eax,80000000H
mov cr0,eax
jmp ShowMessage
ShowMessage:
mov ax,DataSel
mov ds,ax
mov ax,VideoSel
mov es,ax
mov esi,offset PageOK
mov edi,80*4
mov ah,1eh
Load1: lodsb
cmp al,0
jz UnPage
stosw
jmp Load1
UnPage: mov eax,cr0
and eax,7FFFFFFFH
mov cr0,eax
jmp OK
OK:
JUMP32 CodeSel,<offset ReadyToReal>
protect32 ends
;==============================
Code Segment use16
assume cs:Code,ds:Data
Start:
xor eax,eax
mov ax,Data
mov ds,ax
shl eax,4
mov dword ptr [GdtPtr+2],eax
mov DataDes.basel,ax ;初始化数据段描述符
shr eax,16
mov DataDes.basem,al
mov DataDes.baseh,ah
xor eax,eax ;初始化代码段描述符
mov ax,Code
shl eax,4
mov CodeDes.basel,ax
shr eax,16
mov CodeDes.basem,al
mov CodeDes.baseh,ah
xor eax,eax ;初始化32位保护模式代码段描述符
mov ax,Protect32
shl eax,4
mov ProtDes.basel,ax
shr eax,16
mov ProtDes.basem,al
mov ProtDes.baseh,ah
lgdt qword ptr GdtPtr ;Load GDT
cli
call EA20
mov eax,cr0 ;转到保护模式模式
or eax,1
mov cr0,eax
JUMP16 ProtSel,0
ReadyToReal:
mov eax,cr0
and eax,0fffffffeh
mov cr0,eax
JUMP16 <seg Real>,<offset Real>
Real:
call DA20
sti
mov ax,Data
mov ds,ax
mov es,ax
mov ax,4c00h
int 21h
;-----------------------------
EA20 PROC
push ax
in al,92h
or al,2
out 92h,al
pop ax
ret
EA20 ENDP
DA20 PROC
push ax
in al,92h
and al,0fdh
out 92h,al
pop ax
ret
DA20 ENDP
;----------------------
Code ends
end Start
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -