Setting up the GDT
Now, the fun parts begins. If you have read the i386 manual, this will go fairly easy. Setting up GDT is very important and if you plan to use multiple GDT(s) (only in advanced systems), set up the GDT in a page boundary (low 12 bits of address must be 0). Since your kernel will be doing most of the stuff, just define only the necessary descriptors. In my example, I used the following descriptors with the following value.
Set up Vital Descriptors
Descriptor #1: Kernel Code Descriptor
Base Address: | Where you want to load your kernel |
Limit: | 0xFFFF |
Access Byte: | 0x98 |
Type Byte: | 0xC0 |
Descriptor #2: Kernel Data Descriptor
Base Address: | This depends on the type of linking |
Limit: | 0xFFFF |
Access Byte: | 0x92 |
Type Byte: | 0xC0 |
Descriptor #3: Init Code Descriptor
Base Address: | Same place where you loaded INIT |
Limit: | 0xFFFF |
Access Byte: | 0x98 |
Type Byte: | 0x00 |
Descriptor #4: Init Data Descriptor
Base Address: | Same place where you loaded INIT |
Limit: | 0xFFFF |
Access Byte: | 0x92 |
Type Byte: | 0x00 |
Descriptor #5: Video Descriptor
Base Address: | 0xB800 |
Limit: | 0xFFFF |
Access Byte: | 0x92 |
Type Byte: | 0x00 |
In the above table, Access Byte is the bits 40 through 47 (numbering from 0) and Type byte is the nibble from 52 to 55 (including 52 and 55). Once the GDT is set, then we can load our kernel to the address specified by Base Address in the descriptor and jump to that location. There are other tutorials out there that does a better job than I do in explaining how to get into Protected Mode from a Real Mode. All programs uses the same way to do that. Just the way the code is set up is different. I will tell you all the steps and coding is just easy as throwing a ball.
Step 1: Initialize the Descriptors with appropriate values
Step 2: Set the GDTR.
Make sure the Base Address is not the Offset address. And the Limit is appropriate.
Step 3: Do a FAR jump to the kernel location.
jmp Selector:Offset
Step 4: Once you are in kernel, set the DS, SS, ES, GS and FS to appropriate 'Selectors'.