Perhaps I should make it a bit clearer what I'm doing. I've written the following code
and run it in qemu on Linux.
I know that this will never work on a real machine - it's just a learning exercise for myself. If I don't choose the correct value to load into the data selectors I get a triple fault, however I don't understand where this number comes from.
Can anyone explain how this works?
thanks.
<----cut here ----->|<----cut here ----->|<----cut here ----->|<----cut here ----->
; Compile with:
; nasm -o bios.bin bios.S
; Run Qemu with:
; qemu -cpu 486 -L ./ -hda /dev/zero -serial stdio -m 16 -nographic
;
ORG 0xffff0000 ; 64k BIOS
BITS 16
rom_start:
cli
lgdt [gdtDesc]
mov eax,cr0 ; protected mode
or ax,1
mov cr0,eax
jmp word pm_start
pm_start:
; Signal we are in PM via COM1, output a byte on serial port.
mov dx, 0x3f8
mov al, 'P'
out dx, al
; Load data selectors
mov eax, 0x2
mov ds, eax
mov es, eax
mov fs, eax
mov gs, eax
; Signal we are done via COM1
mov dx, 0x3f8
mov al, 'F'
out dx, al
jmp $
ALIGN 4
gdtDesc:
dw (gdtEnd - gdt)
dw gdt
ALIGN 4
gdt:
dd 0,0 ; null.
dw 0xffff, 0x0000
db 0x00, 10011010b, 11001111b, 0x00 ; code
dw 0xffff, 0x0000
db 0x00, 10010010b, 11001111b, 0x00 ; data
gdtEnd:
TIMES 0xfff0-($-$$) DB 0xff
reset_entry: ; power on
jmp rom_start
TIMES 0x10000-($-$$) DB 0xff
<----cut here ----->|<----cut here ----->|<----cut here ----->|<----cut here ----->