flat assembler
Message board for the users of flat assembler.

Index > Main > Real-flat mode. Some questions about it

Author
Thread Post new topic Reply to topic
Cas



Joined: 26 Feb 2004
Posts: 82
Location: Argentina
Cas
I finally started trying and made a little program that enters real-flat mode (loads a segment register with a 4GB memory block and gets into real mode again). I'm really amazed, for before today, I had no idea that could be done. But now I've got some serious doubts...

1 - I know that expanded memory managers enter protected mode and therefore I can't use real-flat mode when one of them is running. But I also know that you can allocate EMS, quit your program and your data is safe in EMS. Does the same thing happen with XMS? In other words.... Can I use real-flat mode while HIMEM.SYS is loaded? Wouldn't I corrupt something?

2 - Suppose I prepare a GDT, load it and then enter protected mode to load my FS or GS register with a big descriptor. In the short moment I spend in protected mode.... what happens if a hardware interrupt occurs? Are real-mode interrupts still working? Should I work fast?

3 - What if I have to use FS for something else? I can't save it somewhere else or I will lose the extended part of the segment register and have to warp to protected mode again to get it. Am I right?

Well... thank you in advance. I would really appreciate some help from somebody experienced with this. Smile

_________________
«Earth is my country; science is my religion» - Christian Huygens
Post 03 Jul 2004, 04:27
View user's profile Send private message Yahoo Messenger MSN Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Quote:

Can I use real-flat mode while HIMEM.SYS is loaded?

I believe himem.sys actually uses "real flat"/voodoo/whatever_name itself - but privalov is probably the best person to ask, I haven't dealt with this for quite some years Smile

Quote:

what happens if a hardware interrupt occurs? Are real-mode interrupts still working? Should I work fast?

Mask out hardware IRQs and CLI.

#3 - again, somebody else will have to confirm/deny, but I believe the base+limit "shadow part" of the segment/selector registers are only set in protected mode, so it should be safe. But play around Smile

I generally don't like voodoo mode, as it requires a rather "clean" system, and isn't compatible with protected operating systems. But it's useful for bootloaders etc.
Post 03 Jul 2004, 15:26
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
1. Yes, but you must use HIMEM.SYS to allocate memory. If it isn't used, you can use whole memory. But, if you detect HIMEM.SYS is loaded, it doesn't mean you can use flat-real mode, there can be also EMM386 loaded (which requires HIMEM.SYS loaded), which restricts you from using flat-real mode (it switches to V86 mode)

2. f0dder is right, also real-mode interrupt won't happen in protected mode, unless done so with protected-mode handler.

3. Due to docuemntation, "shadow part" (mainly limit) should be reset to default values (0FFFFh) every time when used, but in reality it didn't happen. That's why flat-real mode was sometimes called a "bug" and wasn't recommended. But many programs used it (even Ultima 7 runned under flat-real mode engine), so designers had to keep it.
Post 03 Jul 2004, 16:04
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
HIMEM.SYS itself turns the flat real mode on, so if you've got HIMEM.SYS loaded and processor is in pure real mode (not V86 one), you can use flat real mode advantages even without initializing. The nicest solution I had to use the flat real mode is:

1) first check whether processor is in V86 mode with "smsw" instruction, it's better to use "smsw" instead of "mov eax,cr0" because it is not privileged instruction and it won't be notices by Win32 system, for examples (if you check for protected mode with "mov eax,cr0" in Windows 3.11, you program will crash, Windows 95 will tell you that the program must be executed in DOS mode and ask whether you want to switch to DOS mode; use of "smsw" gives you an opportunity to display your own message in such cases)
2) install the interrupt 13 handler which turns the flat real mode on and returns - if processor is in real mode, but not in flat real mode, this exception will be invoked by processor when you try to access data above the 64 kilobyte limit. This way your flat real mode initialization routine will be executed only when it's really needed, that is only when there was no flat real mode already turn on (as in case of HIMEM.SYS loaded). Also it will help you in cases when there is some protected mode TSR loaded in system which constatly breaks your flat real mode by switching to protected mode and then back to limited real mode (I had this problem with CUbic Player) - in such case your exception handler will be invoked again when you use the flat real mode features and will restore you mode "just in time". Because interrupt 13 is also IRQ 5, you can read the IRQ status from PIC 1 to determine whether it was really exception and invoke the previous handler if it's IRQ.

As for your doubts: you should have the IF flag cleared for all the time while you are switching to protected mode and back, so there cannot be any interrupt and there is no problem. When enabling flat real mode you should load all the data segment registers (DS, ES, etc.) with big descriptor, as HIMEM.SYS does, too.

For a example of this flat real mode approach look at the sources of some of the old DOS versions of fasm, which were using this mode.
Post 07 Jul 2004, 15:51
View user's profile Send private message Visit poster's website Reply with quote
Cas



Joined: 26 Feb 2004
Posts: 82
Location: Argentina
Cas
Very Happy Thank you, guys... but there's something more...

1) If I don't have EMM386 or HIMEM loaded and I want to know how much memory I have, in order not to try and read or write a place that does not exist, what do I do? And what would happen if I addressed such a place?

2) Privalov, what do you mean with this:
Quote:
if you've got HIMEM.SYS loaded and processor is in pure real mode (not V86 one), you can use flat real mode advantages even without initializing.

Does it mean that while HIMEM.SYS is on, I can ALWAYS address a 32 bit offset without having an exeption?

3) Suppose I have initialized ES with a 4GB limit and somebody (say, a TSR) executes this:
Code:
mov ax,es
mov es,ax
    

Does the ES register lose its huge limit feature? Confused

Thank you again! Rolling Eyes

_________________
«Earth is my country; science is my religion» - Christian Huygens
Post 09 Jul 2004, 05:14
View user's profile Send private message Yahoo Messenger MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
1) You should use the BIOS functions of interrupt 15h in such case -see the source of fasm for DOS to have an example

2) No, it's too risky to assume this. First, processor can be in V86 mode (like when some memory manager like EMM386 is enabled) and in such case you even cannot initialize flat real mode yourself. Second, as I said in my previous post, some protected mode TSR program may restore the limit for selectors again to 64 kilobytes even after HIMEM.SYS changed it 4 GB.

3) No when this is real mode code. For other cases, see 2)
Post 09 Jul 2004, 17:06
View user's profile Send private message Visit poster's website Reply with quote
Cas



Joined: 26 Feb 2004
Posts: 82
Location: Argentina
Cas
Thank you! As soon as I disconnected I tried it and I understood the part about HIMEM. But there seems to be something abiguous in another part... for example...

Suppose I prepare a GDT with descriptor 1 at base 0 and limit 4G, and then I do this:

Code:
cli
mov eax,cr0
or eax,1
mov cr0,eax

mov ax,1000b   ; P.L. = 0, GDT, descriptor 1
mov ds,ax    ; load the descriptor (now DS = Cool

mov eax,cr0          ; return to real mode
and eax,0fffeh
mov cr0,eax
sti

mov ax,ds
call output_ax_value_to_the_screen
    


What do I have? 8 or 0?

And if then I do this:

Code:
mov ax,0a000h
mov ds,ax
    


Does the base of DS change to 0a0000h and the limit stays at 4Gb or the limit is adjusted so I cannot address further than 4Gb or the limit is set to 64k?

Based on what you told me, I suppose the base would change to 0a0000h and the limit would not change. Is that right?


THANKS Very Happy

_________________
«Earth is my country; science is my religion» - Christian Huygens
Post 11 Jul 2004, 03:37
View user's profile Send private message Yahoo Messenger MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
Quote:
Based on what you told me, I suppose the base would change to 0a0000h and the limit would not change. Is that right?

Yes, exactly. The limit part of selector can only be changed from protected mode.
Post 13 Jul 2004, 08:15
View user's profile Send private message Visit poster's website Reply with quote
Martin_Bian



Joined: 30 Aug 2005
Posts: 2
Martin_Bian
In real flat, 4G memory block can be accessed with a unlimited segment register. The register may be DS,ES,GS or FS. I'm wanderring if the CS is possible to be configured as 4G limited? if that the program can run above 1M. If not, please tell me why. Thanks.
Post 30 Aug 2005, 09:33
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
It's possible, but only with interrupts disabled - the problem with interrupts is that in real mode they always store only 16-bit IP on the stack, so after interrupt occurs when you've got EIP above 1M, it will go wrong.
Post 30 Aug 2005, 11:00
View user's profile Send private message Visit poster's website Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
smiddy
You could rewrite the interrupts if you are ambitious enough...
Post 30 Aug 2005, 11:22
View user's profile Send private message Reply with quote
Martin_Bian



Joined: 30 Aug 2005
Posts: 2
Martin_Bian
Interrupts is a big issue. But if interrupts are disable, is there any 16-bit C compiler can generate the code above 1M?
Post 31 Aug 2005, 01:12
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
Quote:

I finally started trying and made a little program that enters real-flat mode


Try GRDB 6.9 (free symbolic debugger w/ TASM source -- seems to work under Win XP).

Quote:
- Support for debugging Flat Real applications


http://members.tripod.com/~ladsoft/grdb.htm
Post 09 Sep 2005, 05:13
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.