;ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
;³             PMode Tutorial (c) 1996 MATTsoft, All rights reserved.        ³
;ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´
;³   THiS iS EXAMPLE (2nd) Howto switch to PMode, and check for 386 and V86  ³
;³                    úúúúúúúúúúúúú G00D LuCK! úúúúúúúúúúúúú                 ³
;ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
LOCALS @@
;ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ
CODE    SEGMENT BYTE PUBLIC 'CODE'
	ASSUME CS:CODE,DS:CODE
        .386
	.386P
;ùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùú
Start:  mov ax,cs
	mov ds,ax
	mov es,ax
	call CheckProcessor                     ; Check if 386+
	call CheckV86				; Check for V86 mode
        mov eax,cr0
        or al,1
        mov cr0,eax                             ; Set Pmode
;ùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùú
        nop                                     ; Protected Mode instruction!
;ùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùú
        xor al,1
        mov cr0,eax                             ; Return back to real :-(
;ùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùú
        mov ax,4c00h
        int 21h                                 ; ... and terminate
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
;                               $0ME R0UTiNES...

;ùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùú
; Error messages
no386err db 'Sorry, 386 is needed!',13,10,'$'
InV86err db 'Sorry, processor is in V86 mode!',13,10,'$'

;ùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùú
; Checks if the processor is 386+
CheckProcessor:
	pushf			; save flags for later
        xor ah,ah               ; clear high byte
        push ax                 ; push AX on the stack
	popf			; pop this value into the flag register
	pushf			; push flags on the stack
        pop ax                  ; ...and get flags into AX
        and ah,0f0h             ; try to set the high nibble
        cmp ah,0f0h             ; on a 80386, the high nibble can never be 0f0h
        je @@1                  ; ...
        mov ah,70h              ; now try to set NT and IOPL
        push ax                 ;
        popf                    ;
        pushf                   ;
        pop ax                  ;
        and ah,70h              ; if they couldn't be modified, no 386 is installed
        jz @@1                  ; ...
	popf			; restore flags
        retn                    ; and return
@@1:    mov dx,offset no386err  ; if there is no 386, exit with error msg
        mov ah,9
        int 21h
        mov ax,4c01h
        int 21h

;ùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùú
; Checks if in V86 mode
CheckV86:
        smsw ax                 ; mov eax,cr0 is not allowed in V86
        test al,1               ; Test for V86
        jnz @@1
        ret
@@1:    mov dx,offset InV86err  ; if in V86, exit with error msg
        mov ah,9
        int 21h
        mov ax,4c01h
        int 21h
;ùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùúùú
CODE    ENDS
;ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ
        END
