[BITS 16]
[ORG 0x7C00]

; Save boot disk number..
mov [BootDisk], dl

; Enable A20 gate
in al, 0x92
or al, 2
out 0x92, al

mov ax, 0x0
mov es, ax
mov di, 0x500
   
mov eax, 0x0000E820
mov edx, 0x534D4150
mov ebx, 0x00000000
mov ecx, 20
   
int 0x15

reset_drive:
   mov      dl, [BootDisk] ; Disk..
   mov      ah, 0            ; Reset code
   int      13h
   jc      reset_drive

   mov      ax, 0
   mov      es, ax
   mov      bx, 0x1000         ; Destination address = 0000:1000

   mov      ah, 2            ; Read sector code
   mov      al, 50            ; Sectors to read
   mov      ch, 0            ; Cylinder?
   mov      cl, 02h            ; Sector number
   mov      dh, 0            ; Head?
   int      13h
   jc      reset_drive

   cli

   xor      ax, ax
   mov      ds, ax

   lgdt   [gdt_desc]

   mov      eax, cr0
   or      eax, 1
   mov      cr0, eax

   jmp      08h: long_jump

BITS 32
long_jump:
   
   mov ax, 0x10
   mov ds, ax
   mov es, ax
   mov fs, ax
   mov gs, ax
   mov ss, ax
   
   mov al, '!'
   mov [0xB8000], al
   
   jmp $ ; Dont jump continue
   
   jmp 08h: 1000h
   

gdt:

gdt_null:         ; Null segment
   dd 0
   dd 0

gdt_code:         ; Code segment, read/ execute, nonconforming
   dw 0FFFFh
   dw 0
   db 0
   db 10011010b
   db 11001111b
   db 0

gdt_data:         ; Data segment, read/ write, expand down
   dw 0FFFFh
   dw 0
   db 0
   db 10010010b
   db 11001111b
   db 0
   
gdt_interrupts:      ; Interrupt segment, read/ execute, nonconforming
   dw 0FFFFh
   dw 01000h      ; Start at address 1000h
   db 0
   db 10011010b
   db 11001111b
   db 0

gdt_end:


gdt_desc:
   dw gdt_end - gdt - 1
   dd gdt


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

; Uninitialized variables..
[section .bss]
BootDisk resb 1
