   asm(".code16gcc\n");
   asm(".globl start\n");
   asm("start:\n");
   asm("cli; movw $0, %ax; movw %ax, %bx; movw %ax, %cx; movw %ax, %dx;");
   asm("jmp _load_sectors;\n");

struct gdt_entry
{
    unsigned short limit_low;
    unsigned short base_low;
    unsigned char base_middle;
    unsigned char access;
    unsigned char granularity;
    unsigned char base_high;
} __attribute__((packed));

struct gdt_ptr
{
    unsigned short limit;
    unsigned int base;
} __attribute__((packed));

struct gdt_entry gdt[3];
struct gdt_ptr gp;

void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
{
    gdt[num].base_low = (base & 0xFFFF);
    gdt[num].base_middle = (base >> 16) & 0xFF;
    gdt[num].base_high = (base >> 24) & 0xFF;
    gdt[num].limit_low = (limit & 0xFFFF);
    gdt[num].granularity = ((limit >> 16) & 0x0F);
    gdt[num].granularity |= (gran & 0xF0);
    gdt[num].access = access;
}

void gdt_install()
{
    gp.limit = (sizeof(struct gdt_entry) * 3) - 1;
    gp.base = (unsigned int)&gdt;
    gdt_set_gate(0, 0, 0, 0, 0);
    gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
    gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
    asm("lgdt (_gp); \
         movl %cr0, %eax; \
         or $1, %eax; \
         movl %eax, %cr0; \
         movw $0x10, %ax; \
         movw %ax, %ds; \
         movw %ax, %es; \
         movw %ax, %fs; \
         movw %ax, %gs; \
         movw %ax, %ss; \
         jmp $0x08, $(0x7C00 + _pmode_entry_point);");
}      

void load_sectors() /* load 18 sectors to 0x1000:0x0000 (0x10000) */
{   
    asm("xorw %ax, %ax; \
         int $0x13;");
    
    asm("movw $0x1000, %ax; \
         movw %ax, %es; \
         movw $0x0000, %bx; \
         movb $0x02, %ah; \
         movb $0x12, %al; \
         movb $0x00, %ch; \
         movb $0x01, %cl; \
         movw $0x0000, %dx; \
         int $0x13;");
         
    gdt_install();
}

void pmode_entry_point()
{
     asm(".code32;");
     asm("cli;");
     asm("movl $0x000000, %esi;");
     asm("movl $0x90000, %esp;");
     asm("jmp $0x08, $0x10200;"); /* jump to the kernel */
}

asm(".org (0x200 - 2); .word 0xAA55;\n");
/* anything placed past here bleeds into the next sector */
