Page 1 of 1

back to real and unreal mode

Posted: Wed Jan 08, 2003 2:30 pm
by Guest
hi
can anyone plz give me example code and information on how to go back to real mode from protected mode and also how to get into unreal mode.

if you could also give me links to online documentation dat explain it well would be a help 2.

thnx

Re:back to real and unreal mode

Posted: Wed Jan 08, 2003 4:55 pm
by Guest
so far ivae got the follwong code to switch bak to real mode, but i think it has erros init it, and my assembly is not 2 god so can nebody plz help?

[BITS 32]

segment .text

[global _RealMode]

; Begin Real Mode Switch
_RealMode:

   cli            ; Disable Interupts
   jmp word 0x8765:0x4321    ; 32 to 16 bit
   dec ax          ; Switch back to real mode
   mov cr0, eax
   sti            ; Enable Interupts
   ret
; End Real Mode Switch

Re:back to real and unreal mode

Posted: Wed Jan 08, 2003 6:49 pm
by jrfritz
Here:

mov al, <Your GDT Code Sel Here> ;Selector for 4Gb data seg
mov ds, ax ;{2} Extend limit for ds
mov es, ax ;Extend limit for es
dec cx ;Switch back to real mode
mov CR0, ecx ;{5}

Actually...this is real mode with 4GB data...but it lets you have interrupts...and when you want to go back to PMode...I have code for that too...since you can't reenter just the same way...

I THINK this is unreal mode....

Re:back to real and unreal mode

Posted: Sun Jan 12, 2003 11:19 am
by Turdus
Well, unreal is simple real mode with 4GB segments. None of recent processors have real real mode; they emulate them by special 64k GDT entries. If you change that entries, you'll have unreal mode.

For real mode, it's a bit difficult: you have to fill up PIC registers correct real mode values, and GDT with correct entries.
Protmode code segment:
db 66h, 0eah, Back, RealCS

Real Mode segment (RealCS):
Back:
cli
mov ax, RealDS ; switch back to real mode
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov eax, CR0
and al, not 9
mov CR0, eax
jmp far RealSeg, realcs ; load real mode segment into cs
realcs: xor ax, ax
mov ss, ax
mov sp, 1000h
call RestoreIDT
call RestorePIC

RestoreIDT uses lidt to load previously saved state into idt register.
RestoreIDT Proc
lidt Real_IDT_Value
ret
RestoreIDT Endp

RestorePIC does the same:
RestorePIC Proc
push ax dx
mov dx, 0870h ; IRQ -> int 08h, 70h
call SetPIC

mov al, Old_Mask0 ; restore old values
out 21h, al
mov al, Old_Mask1
out 0a1h, al
in al, 60h ; read from keyboard to desire PIC
pop dx ax
ret
RestorePIC Endp

The SetPIC procedure:
SetPIC Proc
; reallocation of IRQ 0-7. hardware interrupts
mov al, 10001b ; ICW1 Bit 0: in PC ICW4 required too
out 20h, al
mov al, dh ; ICW2: new base address of master IRQs
out 21h, al
mov al, 100b ; ICW3: master+slave: irq 2
out 21h, al
mov al, 1 ; ICW4: Bit 0: indicates 8086 mode
out 21h, al ; Bit 1: EOI (0=non automatic)
; IRQ 8-15. hardware interrupts
mov al, 10001b ; ICW1
out 0a0h, al
mov al, dl ; ICW2: new address of slave IRQs
out 0a1h, al
mov al, 10b ; ICW3: master+slave: irq 9
out 0a1h, al
mov al, 1 ; ICW4
out 0a1h, al
ret
SetPIC Endp

That's all ;)

Turdus, developer os OS/42

Re:back to real and unreal mode

Posted: Sat Feb 01, 2003 5:41 am
by Perica
..

Re:back to real and unreal mode

Posted: Sun Feb 02, 2003 1:56 pm
by Pype.Clicker
hmm, it will work, but why bothering yourself with something that complicated while a simple

Code: Select all

mov eax,cr0
and al,0xFE
mov cr0,eax
would also work ?

and, for the efficience, i would not worry too much because mov CRx,<reg> is not pairable and is a serializing (flushes pipe-line) instruction, iirc.

Re:back to real and unreal mode

Posted: Tue Feb 04, 2003 12:09 pm
by distantvoices
one question:

Why bothering about going back to (un)real mode? Except for bios ints?

It would simplify the things a little bit, if one didn't bother about THAT in first time.

Just my opinion

ad Perica: I wonder how you ENTER protected mode *dg*