It took me a while to get into protected mode on VMware with a virtual floppy but eventually I got it working and can run C programs.
I can write my OS code to a usb stick and boot it - it works, but keeps resetting when I try to get into protected mode like I do on the virtual machine. I tried booting on two laptops but they will not go 32bits with the same (gdt) settings as the VM.
I can only think of two things that might be stopping it:
1. The bios doesn't like me using segment limits that are larger than the memory size?
2. I need to restore interrupts ?=> write IVT etc which I don't want to do yet.
My code runs and prints char to screen until the exact line where 'mov cr0,eax' ...
I have no debugger set up yet so I can't tell where the code messes up after this line.
Code: Select all
jmp load_GDT
GDTR:
dw (end_GDT - GDT) -1 ;end_GDT - GDTR ??
dd GDT
GDT db 0,0, 0,0, 0,0, 0,0
code32 equ 0x8
db 0xff,0xff,0x00,0x00
db 0,0b10011010,0b11001111,0
data32 equ 0x10
db 0xff,0xff,0x00,0x00
db 0,0b10010010,0b11001111,0
end_GDT:
load_GDT:
xor eax,eax
mov eax,GDT
add eax,0x7c00
mov [GDTR+2],eax
cli ; disable interrupts
lgdt [GDTR] ; load table into cpu
mov eax,cr0 ; control register
or eax,0x1 ; set protection enable (PE)
mov cr0,eax
jmp 0x08:0x7e00 ; set 32bit registers <- jmp to next sector
; jmp a:b means set cs:ip
Code: Select all
; SECTOR 2, 0X7E00
set_registers:
bits 32
mov ax,0x10
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
mov byte [0xb8322],'y'
mov byte [0xb8323], 0x12
mov esp, 0x4000000
hlt
; C KERNEL PASTES HERE AFTER LINKING
; ...
; ...