bits 16
org 0x0000

;jmp stage2_entry

stage2_entry:

 	 cli
	 push 0x7e0
	 push .fix_cs
	 retf						; jump to 0x7e0:fix_cs
 .fix_cs:
	 mov	ax, 0x7e0
	 mov	es, ax
	 mov	ds, ax
	;
	; note: stack still at 7c0:ss and 7c0:bp.
	; up to you ifyou want to keep it or create new stack.
	; ss:bp->bios paramater block loaded in boot record
	; but its better if the boot loader just loads the first
	; sector itself and get it to avoid dependency.
	;
	sti
;----------------------------------------------------------------------------
;Setting up 32bit Environment... Load GDT, Set PE bit of CR0, Flush Registers
;----------------------------------------------------------------------------

 ; Load GDT
 lgdt [GDTR32]
 
 
 cli
 ; Set PE bit...Set the lowest bit of CR0 to 1
 mov eax, cr0
 or al, 0x01
 mov cr0, eax
 
 ; Flush registers after loading GDT
 flush_gdtr32:
    lgdt [GDTR32]
    jmp 0x08:complete_flush
 
complete_flush:
    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    ret

;-----------------------------------------------
;Create a GDT with 3 entries: Null, Code, Data
;-----------------------------------------------	
	
align 16
GDTR32:					; Global Descriptors Table Register Points to GDT
dw gdt32_end - gdt32 - 1		; limit of GDT (size minus one)
dq gdt32				; linear address of GDT

align 16
gdt32:
dw 0x0000, 0x0000, 0x0000, 0x0000	; Null desciptor
dw 0xFFFF, 0x0000, 0x9A00, 0x00CF	; 32-bit code descriptor
dw 0xFFFF, 0x0000, 0x9200, 0x00CF	; 32-bit data descriptor
gdt32_end:

align 16	

;jmp 0x8:Start32	;Descriptor:Offset syntax. Each desciptor is 8 bytes
 
;------------------------------------------------
;Start of 32-bit Protected Mode
;------------------------------------------------

;use32

;Start32:

mov si, msg_stage2
call print
	 
 print:
	 lodsb
	 or	al, al
	 jz .done
	 mov	ah, 0eh	
	 int	10h
	 jmp	print
 .done:
	 ret

 msg_stage2 db "The boot loader has been loaded successfully.", 0