Page 1 of 1

can't jump to protected mode

Posted: Sun Nov 18, 2018 11:23 am
by drnono
Hi. My code doesn't survive the jump to protected mode - it just crashes and resets. I've tried for ages to get this to work. I can't see whats wrong with the code either. Any ideas?

Code: Select all

start:

 mov ax, 0x7c0
 mov es, ax
 mov ds, ax


 xor eax,eax
 mov ax,ds ; get linear/physical address for gdt, ie  16*ds + gdt
 shl eax, 4 ; *16
 add eax, GDT
 mov [GDTR+2], eax ;  GDTR - contains gdt offset
 cli
 lgdt [GDTR]

 mov eax, cr0
 or eax,1       ; set protection enable in control register 0
 mov cr0, eax

 jmp 0x8:0x7e00 ; jump, ie set cs:ip. 8 ptr to code selector in gdt

jmp $

GDTR:            ; descriptor  to load into lgdt
 dw 23           ; gdt table length
 dd 0            ; to store physical address for GDT:

GDT:
 gdt0 dq  0x0000000000000000 ; null
 gdt1 dq  0x00cf9a000000ffff ; code         proper order
 gdt2 dq  0x00cf92000000ffff ; data
endGDT:



variable dd 0
%include "functions.asm"

 times 510-($-$$) db 0
 dw 0xaa55

; 0x7e00
protected_mode:
 
 bits 32
 mov ax,0x10
 mov ds,ax
 mov es,ax
 mov fs,ax
 mov gs,ax
 mov ss,ax
 mov esp, 0x90000

 mov byte [0xb8002], 'P'
 mov byte [0xb8003], 24

jmp $


Re: can't jump to protected mode

Posted: Sun Nov 18, 2018 11:52 am
by Octocontrabass
The BIOS (or MBR) only loads 512 bytes of your code. Where do you load the rest of your code? Why is your code bigger than 512 bytes?

Why are you setting the segment registers to 0x7c0 instead of setting them to 0 and using "org 0x7c00"?

Re: can't jump to protected mode

Posted: Sun Nov 18, 2018 1:04 pm
by drnono
That sorted it thanks. I was trying to call code past the 512 bytes, although it works I have to admit I can't really understand what org does

Re: can't jump to protected mode

Posted: Sun Nov 18, 2018 1:19 pm
by Octocontrabass
drnono wrote:I can't really understand what org does
It tells the assembler the offset where your code will be located (relative to the segment base), so you can do things like this:

Code: Select all

jmp 0x8:protected_mode
If you set the segment base to 0, the offset is the same as the linear address, so you can also do things like this:

Code: Select all

GDTR:
dw 23
dd GDT