Last active
April 18, 2019 17:19
-
-
Save lighth7015/3ae04efbc4610e8819ab4c51cce8f982 to your computer and use it in GitHub Desktop.
Enter/Leave Protected Mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
org 0x100 | |
start: use16 | |
cli ; disable interrupts | |
in al, 0x92 ; IO port method to enable A20 | |
or al, 2 | |
and al,0xFE | |
out 0x92, al | |
lgdt [descriptor] ; load GDT header into GDT register, so the CPU can find the GDT entries | |
mov eax,cr0 ; enable protected mode | |
or eax,1 | |
mov cr0,eax | |
dw _start32 ; jump to start of 32bit protected mode segment | |
db 0xEA | |
db 8 | |
LeaveProtectedMode: use16 | |
pop ds | |
popf | |
mov ah, 4ch ; exit | |
mov al, cl ; status = the byte | |
int 21h | |
descriptor: ; This header describes the GDT itself | |
dw 31 ; GDT limit is 31, which means its size is 32 bytes | |
dd gdt_entries+0x11C70 ; GDT flat memory address is the flat memory address of the start | |
; program (0x11C70, as determined in Bochs debugger) + the offset | |
; into the program at which which the actual GDT entries are stored | |
gdt_entries: ; memory blocks are 4kB in size, and the first block in memory is block 0 | |
dq 0x0000000000000000 ; blank entry | |
dq 0x00C09A0010000008 ; code segment, start of segment is block 1, and is 8 blocks in size | |
dq 0x00C0920090000008 ; data segment, start of segment is block 9, and is 8 blocks in size | |
dq 0x00C0960110000008 ; stack segment, start of segment is block 17, and is 8 blocks in size | |
_start32: use32 ; initialize the segment registers with indecies into the GDT | |
mov ax,2 | |
mov ds,ax | |
mov ax,3 | |
mov ss,ax | |
_star: mov cr0, eax ; | |
db 0eah ; far jump to restore CS & clear prefetch queue | |
dd LeaveProtectedMode ; it MUST be a far jump - return crashes! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment