I looked for it in many sources, including OsDev itself, but all they
are very, very confused: Each one describes the entering procedure and GDT descriptor in a different way, and all they seems trusty.
The worst part of that is that i didn't found anything about returning to the 16-bit real-mode.
My idea is create a simple procedure that will enter 32-bit protected mode, will copy data from base memory to extended memory, and then return to the 16-bit real-mode leaving all registers and flags untouched.
I tried to ( following some sources) create a bootstrap that enter protected mode, then print a single character on CGA video and then freezes, but all it does is reset my computer (it is booting):
Code: Select all
[BITS 16] ;set instructions to 16-bit
[ORG 0x7C00] ;set compiler offset to 007C00h
CLI ;clear automatic interrupts
Xor ax, ax ;set ax to 0000h
Push ax ;push ax (0000h)
Popf ;pop 0000h from ax into flags register
Mov ds,ax ;move 0000h from ax into DS register
lgdt [GDTP] ;load GDT (see above)
Mov eax, CR0 ;Move CR0 to eax
Or eax, 000000001h ;set the first bit, wich will be the PE bit
Mov CR0, eax ;move eax to CR0, setting Protected mode
Jmp 08h:CLEAR ;flush pipe-line
[BITS 32] ;set instructions to 32-bit
CLEAR: ;declaration of CLEAR offset
mov ax, 08h ;set segment identifier (first segment after the null one)
mov ds, ax ;move 0008h from ax into ds
mov ss, ax ;move 0008h from ax into ss
Mov ebp,080000h ;set the stack somewhere away from the code
Mov esp, 08FFFFh
Mov ax,0F01h ;set ax to CGA character code (white smile)
Mov [0B8000h],ax ;put ax on first character of CGA video
HANG:
jmp HANG ;infinite loop, just to be possible see the smile
GDT:
dq 00000000000000000h ;null 8-bytes entrie
dw 0FFFFh ;limit low word
dw 00000h ;base low word
db 000h ;base middle byte
db 010011010b ;acess byte (code, readable, system, unconforming)
db 011001111b ;4 granularity (4Kb) bits and limit high 4 bits
db 000h ;base high byte
dw 0FFFFh ;limit low word
dw 00000h ;base low word
db 000h ;base middle byte
db 010010010b ;acess byte (data, writeable, system, unconforming)
db 011001111b ;4 granularity (4Kb) bits and limit high 4 bits
db 000h ;base high byte
GDTP: ;GDT pointer declaration
dw GDTP-GDT ;GDT limit = GDT end - GDT init - 1
dd GDT ;32-bit offset of GDT
times 510-($-$$) db 000h ;fill with zeroes until byte 01FEh
dw 0AA55h ;boot identifier