bits	16
org  	0x000
jmp	main
; comments and my include statements are here normally the below is for the testing 

;=========================
main:
	call	enable_A20
	call	enable_pmode
	
;A20 enabler (it seems to work)
enable_A20:
        cli

        call    a20wait
        mov     al,0xAD
        out     0x64,al

        call    a20wait
        mov     al,0xD0
        out     0x64,al

        call    a20wait2
        in      al,0x60
        push    eax

        call    a20wait
        mov     al,0xD1
        out     0x64,al

        call    a20wait
        pop     eax
        or      al,2
        out     0x60,al

        call    a20wait
        mov     al,0xAE
        out     0x64,al

        call    a20wait
        sti
        ret

a20wait:
        in      al,0x64
        test    al,2
        jnz     a20wait
        ret


a20wait2:
        in      al,0x64
        test    al,1
        jz      a20wait2
        ret	
		
;pmode enabler (what I think the culprit is)	
enable_pmode:
	cli
	AND EAX, 0x7FFFFFFF
	lgdt [gdt_ptr]
	MOV 	EAX, CR0
	OR 		AL, 1
	MOV 	CR0, EAX

bits 32							; I say its bits 32 here because I believe that at this point it is 32 bit might be wrong.
	
	jmp SYS_CODE_SEL:(Kernel)

Kernel:

	;-------------------------------;
	;   Set registers		;
	;-------------------------------;

	mov ax, 0x10 
	mov ds, ax 
	mov ss, ax 
	mov es, ax 
	mov fs, ax 
	mov gs, ax
	mov		esi, msg
	mov		edi, 0xb8000
	call	Print
	hlt

;PRINT IN: ESI (with the string) and EDI (containing 0xb8000) NOT USED IN THE KERNEL AT THE MOMENT.
Print:    cld            
            mov ah,0x0f  ; color 
nextchar:   lodsb ; get char and increase si 
            or al,al  ; if char= 0 then stop print. 
            jz screen.print.finish 
            stosw  ; put the char+color on the screen and increase di with 2 
            jmp short nextchar  ; countione (sry for my bad spelling) 
screen.print.finish: 
             ret
			 
;*******************************************
; Global Descriptor Table (GDT)
;*******************************************

gdt:
; null descriptor
	dw 0			; limit 15:0
	dw 0			; base 15:0
	db 0			; base 23:16
	db 0			; type
	db 0			; limit 19:16, flags
	db 0			; base 31:24

; linear data descriptor
LINEAR_SEL	equ	$-gdt
	dw 0FFFFh               ; maximum limit 0FFFFFh (1 meg or 4 gig)
	dw 0			; base for LINEAR_SEL is always 0
	db 0
	db 92h			; present, ring 0, data, expand-up, writable
	db 0CFh                 ; page-granular (4 gig limit), 32-bit
	db 0

gdt_cs:
; code descriptor
SYS_CODE_SEL	equ	$-gdt
	dw 0FFFFh		; maximum limit 0FFFFFh (1 meg or 4 gig)
	dw 0			; linear base address; gets patched above
	db 0
	db 9Ah			; present,ring 0,code,non-conforming,readable
	db 0CFh			; page-granular (4 gig limit), 32-bit
	db 0

gdt_ds:
; data descriptor
SYS_DATA_SEL	equ	$-gdt
	dw 0FFFFh		; maximum limit 0FFFFFh (1 meg or 4 gig)
	dw 0			; linear base address; gets patched above
	db 0
	db 92h			; present, ring 0, data, expand-up, writable
	db 0CFh			; page-granular (4 gig limit), 32-bit
	db 0
gdt_end:

gdt_ptr:
	dw gdt_end - gdt - 1	; GDT limit
	dd gdt			; linear address of GDT; gets patched above
	
msg db 0x0A, 0x0D, "HELLO WORLD!", 0x00