Is there simple code for switching into protected mode? (for NASM) Yes - I tried resolve it using Google. Still, I not understand how... And how to load the kernel (in this mode), which is located just behind the VBR? (after first 512 byte with size 512 byte) Thank you!
-
The code for switching is fairly painful but not impossible. Writing the complete operating system on top of that so you can do the absolute basics like display text, accept user input, open a file, etc, that's the one that will set you back a couple of years. Little point to it of course, it has been done. – Hans Passant Nov 01 '13 at 12:31
2 Answers
In protected mode, you don't have access to the BIOS to do the stuff for you. You will need to write drivers inorder for the bootloader to load your kernel and start it. Another approach for relatively small kernels (less than ~640kb) is that when you are still in real mode, load it with the BIOS and once in protected mode move it away from low memory and start it.

- 1,858
- 14
- 18
-
You can use VX86 or whatever it's called. I can't remember. Anyway, you can cheat with this since it will switch your task to real mode and you can call regular bios functions. The actual way of doing it is to rewrite those int 10, int 13 and whatnot in protected mode. Basically you'll have to do the in and outs yourself. It's not trivial. – E.T Nov 03 '13 at 06:05
It's been a long time since I've done that but if I remember correctly, before you can make the switch from Real mode to Protected mode you must reset the 8259 Controller (Pic) but then again that was on he i386 chip so this may no longer be necessary.
Then you use CR0 to switch to protected mode.
mov eax, cr0
inc eax ; PE bit must be set
mov cr0, eax ; store the new value in CR0
jmp dword pSelector ; switch to selector
Obviously before you can switch to protected mode you must set the IDT, GDT and e especially mindful to have a double fault handler (don't ask). If you're using GAS then you'll have to translate that to something like this:
mov %cr0, %eax
inc %eax
mov %eax, %cr0
jmp pSelector
There are a few decent books on i386 programming, I believe that the AMD manual will tell you how to switch from Real to Protected mode :)

- 1,095
- 1
- 10
- 19