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

;if you leave out everything between here and (1), it works well

    mov ax, cs

BITS 32

    movzx eax, ax
    shl eax, 4		;eax now contains linear address of real mode cs
    mov ebx, eax	;here we
    shr ebx, 16		;write linear
    mov [cbase1+1], bl	;address of real mode
    mov ebx, eax	;code segment
    mov [cbase1+2], bh	;into code segment descriptor
    mov [cbase2], bl	;in gdt

BITS 16

    mov ax, ds

BITS 32

    movzx eax, ax
    shl eax, 4		;eax now contains linear address of real mode ds
    mov ebx, eax	;here we
    shr ebx, 16		;write linear
    mov [dbase1+1], bl	;address of real mode
    mov ebx, eax	;data segment
    mov [dbase1+2], bh	;into data segment descriptor
    mov [dbase2], bl	;in gdt

    mov ebx, 0ffffffffh		;calculate highest possible value
    sub ebx, eax		;for pm stack pointer
    mov [stack_pointer], ebx	;and store in [stack_pointer]

BITS 16
    
    ;now let's prepare for entry into protected mode
    push ds				;save these
    push es				;registers
    mov bx, ss ;bx=old stack segment	;for future
    mov cx, sp ;cx=old stack pointer	;restoration

    mov ax, 0ch
    mov ds, ax
    mov es, ax
    mov ss, ax

BITS 32
    mov esp, [stack_pointer]
    cli
    lgdt [gdt_desc]
    mov eax, cr0		;enable
    or eax, 1			;protected
    mov cr0, eax		;mode
    
    call 08h:enter_pm

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

BITS 16

    mov ss, bx				;fortunately
    mov sp, cx				;we saved these registers
    pop es				;before entering
    pop ds				;protected mode

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

;(1)
    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
gdt_code:
	dw 0ffffh	;limit of the segment
cbase1:	dw 0		;base address of segment
	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 granularity
			;1 for 32 bit code segment
			;0 (reserved bit)
			;0 (available to system programmers)
			;1111b for last bits of segment limit
cbase2:	db 0		;last byte of base address

gdt_data:
	dw 0fffh	;limit of the segment
	dw 0		;base address of segment
dbase1:	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 granularity
			;1 for 32-bit stack pointer
			;0 (reserved bit)
			;0 (available to system programmers)
			;1111b for last bits of segment limit
dbase2:	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:
	dd 0

BITS 32

enter_pm:
    ret
