dr0 equ 0
drw equ 1
sr0 equ 2
srw equ 3
ceo equ 4
cer equ 5
cco equ 6
ccr equ 7
SECTION .code
p_en proc far
mov eax,1000h
mov ebx,1000h
mov cl, drw
mov ch,0
call create_desc
or eax,eax
jl err
mov es,ax
mov edi,o
mov eax,0
mov ecx,1024
rep stosd
mov dword ptr es:[0],00002003h
mov dword ptr es:[0ffch],00003003h
;make first page table
mov eax,2000h
mov ebx,1000h
mov cl,drw
mov ch,0
call create_desc
or eax,eax
jl err
mov es,ax
mov edi,0
mov eax,00000003h
mov ecx,1024
TAB1:
stosd
add eax,1000h
loop tab1
;activate paging
mov eax,1000h
mov cr3,eax
pushfd
cli
mov eax,cr0
bts eax,31
mov cr0,eax
jmp flu
flu:
nop
popfd
ret
err:
ret
p_en endp
end
create_desc proc far
push edx
push eax
call f1_gdt
mov edx,eax
pop eax
or edx,edx
jl err1
;determine base address:
mov dword ptr ds:[edx*8]+2,eax
rol eax,8
dec ebx
cmp ebx,0fffff h
ja p_gran
mov word ptr:ds[edx*8]+0,bx
rol ebx,16
mov byte ptr:ds[edx*8]+6,bl
ror ebx,16
and byte ptr ds:[edx*8]+6,ofh
or byte ptr ds:[edx*8]+6,40h
jmp seg_type
p_gran:
shr ebx,12
mov word ptr:ds[edx*8]+0,bx
rol ebx,16
mov byte ptr:ds[edx*8]+6,bl
ror ebx,16
and byte ptr ds:[edx*8]+6,ofh
or byte ptr ds:[edx*8]+6,0c0h
seg_type:
mov byte ptr ds:[edx*8]+5,0
cmp al,ccr
ja err1
shl cl,1
or byte ptr ds:[edx*8]+5,cl
privilege_level:
movzx ax,ch
mov bx, word ptr ss:[esp]+4
arpl ax,bx
shl al,5
or byte ptr ds:[edx*8]+5,al
or byte ptr ds:[edx*8]+5,90h
mov ax,dx
mov ax,3
arpl ax,bx
pop edx
ret
err1:
pop edx
mov eax,0
dec eax
ret
create_desc endp
end
f1_gdt:
porc far
push ebx
push ecx
mov bx,ds
movzx ebx,bx
lsl ecx,ebx
shr ecx,3
jecxz l1
mov eax,0
l2:
inc eax
cmp byte ptr ds:[eax*8]+5,0
loopnz 12
jnz l3
l1:
pop ecx
pop ebx
ret
l3:
pop ecx
pop ebx
mov eax,0
dec eax
ret
f1_gdt:
endp
end