; This kernel is supposed to store the character 'T' somewhere in memory in real mode and
; then read that value back from protected mode and print it to demonstrate how this works

org 0x7C00
bits 16

cli
mov bx, 0x00
mov es, bx
mov bx, 0x7E00
mov byte [es:bx], 'T'

xor	ax, ax
mov	ds, ax
mov	es, ax
lgdt [ds:gdtr_start]
mov	eax, cr0
or eax, 1
mov	cr0, eax

; Move 'bits 32' after the jump far to make this work
; If I have already set up protected mode at this point
; why doesn't the far jump work with bits 32 ?
bits 32

jmp gdt_code-gdt_start:next_instruction
next_instruction:
mov ax, gdt_data-gdt_start
mov ds, ax
mov es, ax

mov eax, 0x7E00
mov al, [es:eax]
mov ebx, 0xB8000
mov [es:ebx], al
mov byte [es:ebx+1], 0xA4

jmp $

gdt_start:
gdt_null: 
    dq 0            ; null descriptor 

gdt_code:
    dw 0xFFFF       ; Segment Limit 15:00
    dw 0x0000       ; Base Address 15:00
    db 0x00         ; Base Address 23:16
       ; Present    Privilege       Descriptor type     Executable      Conforming      Readable    Accessed
    db 0b1__________00______________1___________________1_______________0_______________1___________0
       ; Granularity    32 bits     64 bits     Available   Segment Limit 19:16
    db 0b1______________1___________0___________0___________1111
    db 0x00         ; Base 31:24

gdt_data:
    dw 0xFFFF       ; Segment Limit 15:00
    dw 0x0000       ; Base Address 15:00
    db 0x00         ; Base Address 23:16
       ; Present     Privilege      Descriptor type     Executable      Expansion-direction     Write-enable    Accessed
    db 0b1___________00_____________1___________________0_______________0_______________________1_______________0
       ; Granularity    32 bits     64 bits     Available   Segment Limit 19:16
    db 0b1______________1___________0___________0___________1111
    db 0x00         ; Base 31:24

gdt_end:

gdtr_start: 
    dw gdt_end - gdt_start - 1  ; limit (Size of GDT - 1)
    dd gdt_start                ; base of GDT

times 510-($-$$) db 0
dw 0xAA55