BITS 16
;jmp 07C0h:start

start:
    pusha
    pushf
    mov ax, cs
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov si, txt0	;tell user that
    call printstring	;we're entering protected mode

;here i write the base addresses of the code segment descriptors...i don't know if im doing something wrong here (order of the bytes??)

    mov ax, cs
    movzx eax, ax
    shl eax, 4			;eax now contains linear address of real mode code segment
    mov ebx, eax		;move the linear address
    mov [cbase1], bx		;of the real mode cs
    shr ebx, 16			;into the base fields of
    mov [cbase2], bx		;the 32-bit code segment in the gdt

    mov ebx, eax		;move the linear address
    mov [cbase16_1], bx		;of the real mode cs
    shr ebx, 16			;into the base fields of
    mov [cbase16_2], bx		;the 16-bit code segment in the gdt

;leave everything out from here to (1) and everything works fine!
    cli
    lgdt [gdt_desc]
    mov eax, cr0	;enable
    or eax, 1		;protected
    mov cr0, eax	;mode   
    jmp 08h:enter_pm

return_from_pm:
    mov eax, cr0	;disable
    or eax, 0		;protected
    mov cr0, eax	;mode
    sti

;(1)

    mov si, txt1	;tell user that
    call printstring	;we're back from protected mode

    popf
    popa
    retf

;routine printstring: will print zero-terminated string from ds:si.
printstring:
    pusha
    pushf
    mov bx, 7 ;use the right page
    mov ah, 0Eh
nxtprintstring:
    lodsb
    or al, al
    jz endprintstring
    int 10h
    jmp nxtprintstring
endprintstring:
    popf
    popa
    ret

txt0:
    db 0ah, 0dh, 'Will now go into Protected Mode...',0
txt1:
    db 0ah, 0dh, 'Return from Protected Mode Successful!',0
gdt:
gdt_null:
	dd 0
	dd 0
;(1)
gdt_code:
	dw 0ffffh	;limit of the segment
cbase1:	dw 0		;base address of segment
cbase2:	db 0		;still belonging to base address of segment
	db 10011010b	;1 for "segment is present"
			;00 for "privilege 0"
			;1 for "data or code segment"
			;1 for "code segment"
			;0 for "unconforming"
			;1 for readable
			;0 (access flag set by cpu on 1st access)
	db 11001111b	;1 for page-granularity
			;1 for 32 bit code segment
			;0 (reserved bit)
			;0 (available to system programmers)
			;1111b for last bits of segment limit
	db 0		;last byte of base address
;(2)
gdt_data:
	dw 0fffh	;limit of the segment
dbase1:	dw 0		;base address of segment
dbase2:	db 0		;still belonging to base address of segment
	db 10010010b	;1 for "segment is present"
			;00 for "privilege 0"
			;1 for "data or code segment"
			;0 for "data segment"
			;0 for "expand downward"
			;1 for writable
			;0 (access flag set by cpu on 1st access)
	db 11001111b	;1 for page-granularity
			;1 for 32-bit stack pointer
			;0 (reserved bit)
			;0 (available to system programmers)
			;1111b for last bits of segment limit
	db 0		;last byte of base address
;(3)
gdt_code16:
	dw 010000h	;limit of the segment
cbase16_1:
	dw 0		;base address of segment
cbase16_2:
	db 0		;still belonging to base address of segment
	db 10011010b	;1 for "segment is present"
			;00 for "privilege 0"
			;1 for "data or code segment"
			;1 for "code segment"
			;0 for "unconforming"
			;1 for readable
			;0 (access flag set by cpu on 1st access)
	db 10000000b	;0 for byte-granularity
			;0 for 16 bit code segment
			;0 (reserved bit)
			;0 (available to system programmers)
			;0000b for last bits of segment limit
	db 0		;last byte of base address
;(4)
gdt_data16:
	dw 010000h	;limit of the segment
dbase16_1:
	dw 0		;base address of segment
dbase16_2:
	db 0		;still belonging to base address of segment
	db 10010010b	;1 for "segment is present"
			;00 for "privilege 0"
			;1 for "data or code segment"
			;0 for "data segment"
			;0 for "expand downward"
			;1 for writable
			;0 (access flag set by cpu on 1st access)
	db 00000000b	;0 for byte-granularity
			;0 for 16-bit stack pointer
			;0 (reserved bit)
			;0 (available to system programmers)
			;0000b for last bits of segment limit
	db 0		;last byte of base address

gdt_end:

gdt_desc:
	dw gdt_end - gdt - 1	;limit of the gdt
	dd gdt			;base address of the gdt
stack_pointer:			;holds the value the stack pointer is initially
	dd 0			;going to have in protected mode
stackpointer_16:
	dw 0			;backup of our real mode sp value
realcode:			;backup of our real mode cs value
	dw 0			;backup of our real mode ds, es, ss values
realdata:
	dw 0

BITS 32
enter_pm:
    jmp 018h:return_from_pm

times 510-($-$$) db 0 ; Fill the file with 0's
dw 0AA55h ;
