flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Unreal Mode, Help!

Author
Thread Post new topic Reply to topic
Goat6



Joined: 06 Jan 2006
Posts: 8
Goat6
I have been working on a secondary Loader for my OS.
I use Unreal Mode, I can get it to work but, When I do

I cant add any code anywhare or it will tripple fault and reset the system.

Can anyone help me?

You might see some code you have seen before.
I am aslo sorry for using NASM in a FASM forum, but Im desperate.

Code:


[bits 16]

[SEGMENT .text]


;;;;-----Real Mode Startup-----;;;;
start:
        xor   ax,ax
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   sp,0x7C00 





;;;;-----Setup VESA-----;;;;

        mov  bx,4112h
        mov  ax,4f01h
        mov  di,Mode_Info       
        mov  cx,bx
        int  10h 
        
        mov  ax,4f02h
        int  10h



;;;;-----Enter Unreal Mode-----;;;;

        cli 
        lgdt  [gdtr]
        
        mov   eax, cr0
        or    al,0x1 
        mov   cr0,eax

        jmp   0x10: protected



[bits 32]
protected:
        mov   ax,0x8 
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   esp,0x7C00

        mov   eax, cr0
        mov   al, 0
        mov   cr0, eax

        ;Enable A20
        mov eax, 0DFh 
        out 64h, eax
        


;;;;-----Kernel Main Code-----;;;;

        sti     ;reenable interrupts



Hang:
        jmp Hang




[SEGMENT .bss]

%include "vesa.inc"

[SEGMENT .data]

gdt:        dw    0x0000, 0x0000, 0x0000, 0x0000
sys_data:   dw    0xFFFF, 0x0000, 0x9200, 0x00CF
sys_code:   dw    0xFFFF, 0x0000, 0x9800, 0x00CF
gdt_end:

gdtr:       dw gdt_end - gdt - 1                                          
            dd gdt 

    

_________________
Death is not the opposite of life but rather the absence of it.
Post 06 Jan 2006, 23:08
View user's profile Send private message Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio
You forget the 'org 7c00h' instruction, take a look to some bootloader writen in nasm ,there must be a lot of them.
Post 07 Jan 2006, 13:19
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
This is the code for cdpod, with graphic's part taken out, as you can see "org 0x7C00" is missing.
Code:
;************************************; \\|//; (@ @); ASHLEY4.; Entry for the 512b compo.;; Assemble with fasm; c:\fasm CdPod.asm CdPod.bin;; Keys = (UpArrow + Enter = Play).; Keys = (DownArrow + Enter = Stop).;;************************************org 0x7C00use16;****************************; Realmode startup code.;****************************start:        xor   ax,ax        mov   ds,ax        mov   es,ax        mov   ss,ax        mov   sp,0x7C00;****************************; Vesa start code.;****************************        mov  bx,4112h        mov  ax,4f01h        mov  di,Mode_Info        mov  cx,bx        int  10h         mov  ax,4f02h        int  10h;*****************************; Setting up, to enter pmode.;*****************************        cli        lgdt  [gdtr]        mov   eax, cr0        or    al,0x1        mov   cr0,eax        jmp   0x10: protected;*****************************; Pmode. Wink;*****************************use32protected:        mov   ax,0x8         mov   ds,ax        mov   es,ax        mov   ss,ax        mov   esp,0x7C00;================================  cdpod graphic's removed;================================;*************************************; GDT.;*************************************gdt:        dw    0x0000, 0x0000, 0x0000, 0x0000sys_data:   dw    0xFFFF, 0x0000, 0x9200, 0x00CFsys_code:   dw    0xFFFF, 0x0000, 0x9800, 0x00CFgdt_end:gdtr:       dw gdt_end - gdt - 1            dd gdt ;*************************************; Make program 510 byte's + 0xaa55;*************************************times 510- ($-start)  db 0  dw 0xaa55;*************************************; Put uninitialized data here.;*************************************include 'vesa.inc';*************************************; $$$$££££( vote for me Wink)££££$$$$.;*************************************    

This is how your code should be, not tested.
Code:
;************************************; \\|//; (@ @); ASHLEY4.; Entry for the 512b compo.;; Assemble with fasm; c:\fasm CdPod.asm CdPod.bin;************************************org 0x7C00use16;****************************; Realmode startup code.;****************************start:        xor   ax,ax        mov   ds,ax        mov   es,ax        mov   ss,ax        mov   sp,0x7C00;****************************; Vesa start code.;****************************        mov  bx,4112h        mov  ax,4f01h        mov  di,Mode_Info        mov  cx,bx        int  10h        mov  ax,4f02h        int  10h;**************************************; Put your A20 code here:;**************************************        lgdt  [gdtr]        mov   eax,cr0        or    al,0x1        mov   cr0,eax        mov   ax,0x8        mov   ds,ax        mov   es,ax        mov   eax,cr0        and   al,0xfe        mov   cr0,eax;================================;  Do your stuff here:;================================;*************************************; GDT.;*************************************gdt:        dw    0x0000, 0x0000, 0x0000, 0x0000sys_data:   dw    0xFFFF, 0x0000, 0x9200, 0x00CFsys_code:   dw    0xFFFF, 0x0000, 0x9800, 0x00CFgdt_end:gdtr:       dw gdt_end - gdt - 1            dd gdt;*************************************; Make program 510 byte's + 0xaa55;*************************************times 510- ($-start)  db 0  dw 0xaa55;*************************************; Put uninitialized data here.;*************************************include 'vesa.inc';*************************************; $$$$££££( vote for me Wink)££££$$$$.;*************************************    


PS: As anyone can see "your" code is just a cut & pasting of "CdPod", you will not learn by just C&P and will not be able to understand the code, once you find code that works, try and work out what the code does and comment it, it will help you learn Wink.
Post 07 Jan 2006, 16:33
View user's profile Send private message Reply with quote
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
i also found that CdPod and Solar OS were two of the best sources to read through and learn from. i went from not knowing what ax meant, to now being able to read asm somewhat like a second language. no, im no where near as good as anyone here, but i just keep trying and learning as i go, i also found that what works good is to retype the code, say to CdPod or some of Solar (theres quite a bit of code there). and if you dont know what a certain part means, look it up or find out somehow, then you will be better at knowing what does what, and how it all comes together.

thanks Dex & Bogdan!

_________________
(AH+AL=AX)+E=EAX
Post 07 Jan 2006, 17:52
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
Thanks Redragon, We all learn from each other, i also learnt from Solar and give full credit in Dex4u
Code:
;=========================================================;; Kernel32.                                      11/12/03 ;;---------------------------------------------------------;;                                                         ;; A 32bit pmode OS called "Dex4u".                        ;; fasm kernel32.asm kernel32.exe                          ;;                                                         ;; Thanks to: Tomasz Grysztar,Christopher Giese            ;; Alexei A. Frounze, smaffy, Ontanu Bogdan Valenti.       ;; Bubach.                                                 ;;                                                         ;; Dex4u V0.01                                             ;; (c) Craig Bamford, All rights reserved.                 ;;=========================================================;    

But just in case he mist it, thanks Smile .
Post 07 Jan 2006, 18:13
View user's profile Send private message Reply with quote
Goat6



Joined: 06 Jan 2006
Posts: 8
Goat6
I have looked over this material, over and over again.
I even got the documentation to NASM so that I could make sure I could translate the code properly from FASM to NASM.

The code enter unreal mode fine, but if I ad ANY cod before or after the UM code the system triple faults. I also made sure that I properly set the segment registers. But I cant fugure out whats wrong.


Also this code will go into a secondary loader NOT a boot loader.

My Plan is to,

Have the Boot Loader load the secondary loader called 'Loader.bin',
Loader.bin then loads the memory manager, process manager, Device manager, ect.

At the end of all these actions I will place structures that discribe these processes into memory, then setup a timer interrupt to jump to the first processin line which is the process manager, then the memory manager
sence the process and memory managers must work together to store processes. I use the timer so that the system will automaticly jump to the process manager, but will allow time for my loading screen to be viewed.

But if I cant add any code to the unreal mode init code then this is all for nothing.

I appreciate any advice all of you could give.

Thanks.

Cool Cool Cool

_________________
Death is not the opposite of life but rather the absence of it.
Post 07 Jan 2006, 18:35
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
Code:
[bits 16][ORG 0x7C00][SEGMENT .text];;;;-----Real Mode Startup-----;;;;start:        xor   ax,ax        mov   ds,ax        mov   es,ax        mov   ss,ax        mov   sp,0x7C00 ;;;;-----Setup VESA-----;;;;        mov  bx,4112h        mov  ax,4f01h        mov  di,Mode_Info               mov  cx,bx        int  10h                 mov  ax,4f02h        int  10h;;;;-----Enter Unreal Mode-----;;;;                lgdt  [gdtr]                mov   eax, cr0        or    al,0x1         mov   cr0,eax        mov ax,8h        mov ds,ax        mov es,ax        mov   eax, cr0        and    al,0xfe         mov   cr0,eax        ;Enable A20        mov eax, 0DFh         out 64h, eax        ;;;;-----Kernel Main Code-----;;;;        Hang:        jmp Hang[SEGMENT .bss]%include "vesa.inc"[SEGMENT .data]gdt:              dw 0x0000       dw 0x0000       dw 0x0000       dw 0x0000sys_data:         dw 0xFFFF                                  dw 0       db 0       db 10010010b       db 11001111b       db 0gdt_end:gdtr:       dw gdt_end - gdt - 1                                                      dd gdt     

If you still get the same problem, with the above code let me know, also you need to check your A20 code some do not work.
Post 07 Jan 2006, 21:27
View user's profile Send private message Reply with quote
bogdanontanu



Joined: 07 Jan 2004
Posts: 403
Location: Sol. Earth. Europe. Romania. Bucuresti
bogdanontanu
Hi,

It is nice to findout that SolarOS was somehow helpfull for others aslo;)
After all, helping others understand was one of it's purposes.
However the path to walk can not be transfered with the source code. For this you need more close contact

Now about this code here...

As a general rule:
When developing an OS for the first time
(and if you are not GOD himself)
YOU MOST PE PARANOICALY CAUTIOUS!
Please suspect everything anytime and never assume anything!

Beeing busy, I will not go as far as testing your code rigurousely but after a quick look...

This is what I did noticed (not necessary in the order of importance):

1)Setting stack at 0x7c00 is a little risky.

A much higher address like 0x9C00:0000 (right below A000 video segment) is more cautious.

Here the stack is too low and right under the boot loaded sector.
As a consequence you are playing with fire (do you have a match?)

A stack underflow will erase your code and a stack overflow will hit the real mode INTERUPT table... please choose Razz

2)The VESA calls are not tested for an error Wink
Even more you do NOT test the very existence of a VESA interface.

3)After enabling protected mode
Are you sure that the instruction:
jmp 0x10: protected
is assembled corectly? because the [bits 32] is AFTER that instruction
(uncertain on FASM here)

4)You are resetting the whole CR0 low 8 bits and NOT only the protected mode bit. This must hurt the CPU a little Wink

5)In protected mode you insist on keeping the stack low

6)Enable A20 line is done OK only by that 2 instructions?

7) Maybe you GDT table is not ok?
Well if it did work in CdPod i assume it is ok Very Happy

I did not have the dedication to check it out
but I have noticed an ODD thing:
like having the DATA selector before the CODE selector
...oh dear no problem here but still ...

Cool I would like to see the code that fails after this unreal setup..
Just to make myself a better oppinion.

In the End:
I suggest that you check any assumtions you make
with some string/character output funtions to the normal text screen
located at 0xB000:0000

Just do not enable VESA GFX mode until you are sure everything is ok.

Unreal mode is just a tweak, with some limited use,

I would not use it before I have everything working ok and also have a very clear need to do so.

The only use I can see for unreal mode is to load a kernel that is bigger than 1M into the high memory areas. So, until your kernel grows bigger that 1M... you do not really need it. BUT you do need a working functional protected mode and a good grasp of it!

The motto: "Death is not the opposite of life but rather the absence of it..."
it is one of my favourite sayings....

I wonder IF you really understand the meaning of it?


Last edited by bogdanontanu on 07 Jan 2006, 22:01; edited 1 time in total
Post 07 Jan 2006, 21:45
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
@bogdanontanu, he has used code that was from a 512b compo entry, which because of fitting things in, left out things like vesa error or even checking for vesa, he should of read the warning
Quote:

Please do not use the code, as a example of how to do vesa, get into pmode, atapi code, etc.
To fit it all in, i have had to compromise.
I have better code for that.

Also its unrealmode he wants to get into.
Post 07 Jan 2006, 21:59
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
bogdanontanu wrote:
Are you sure that the instruction:
jmp 0x10: protected
is assembled corectly? because the [bits 32] is AFTER that instruction

In that place the protected mode is already enabled, but the code selector has not yet been reloaded - thus this instruction is still in 16-bit. How could it be assembled incorrectly here?
Post 07 Jan 2006, 23:59
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
bogdanontanu wrote:
A stack underflow will erase your code


If the code will never be used again, what's wrong with erasing it? Sure, I agree with you and don't recommend this idea, but it will not be so fatal. Wink

The boot loader code is usually not used twice, so it can be erased as it is executed. Smile


Examples like SolOS are a good resource of learning, but you should also read some books or talk with someone with experience. In theory, it's a good idea to collect a lot of resources, and select what's good from all of them, since no single one will be perfect, and usually each has it's own advantages and disadvantages.


Redragon wrote:
i went from not knowing what ax meant, to now being able to read asm somewhat like a second language.

Wow, you must be pretty smart by learning only by example. Good luck Wink
Dex4u wrote:
We all learn from each other

Very true Very Happy , even if some people don't want to admit that... I, also, give credit where it is due Wink
Post 08 Jan 2006, 16:45
View user's profile Send private message Reply with quote
dasyar



Joined: 27 Feb 2005
Posts: 33
dasyar
From what I can tell, and I have been down this road before, you are trying to use some readily available code in FASM format, and you are trying to compile it with NASM. For protectected mode that scheme will not work. There are differences in how you would do it in FASM and NASM. I suggest that you go here http://my.execpc.com/~geezer/index.htm, and find geezers tutorial as to how to get into PMODE, and then how to proceed from there. But, if you want to waste your time cutting and pasting and then finding out it does not work, well ...

I think what you may want to do is think about making a decission as to which compiler you will stick with. It will save you a lot of wasted time in the long run.
Post 08 Jan 2006, 19:44
View user's profile Send private message Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio
bogdanontanu wrote:
Hi,

1)Setting stack at 0x7c00 is a little risky.

why?
many bios load the boot code with this setting.
Quote:

(right below A000 video segment) is more cautious.

Usually this is a reserved memory area.
Post 11 Jan 2006, 12:03
View user's profile Send private message Visit poster's website Reply with quote
bogdanontanu



Joined: 07 Jan 2004
Posts: 403
Location: Sol. Earth. Europe. Romania. Bucuresti
bogdanontanu
Octavio wrote:

why?
many bios load the boot code with this setting.


Yes, but you are warned about this low on stack space condition for boot loader code. Many boot loaders immediately change the stack location to a more safeer area.

Since the stack expands downwards it is logically to assume that a higher memory area is the best location for a stack since it provides more place for "expasion".

Quote:

Usually this is a reserved memory area.

Yes there is an rezerved area in there of approximative 1K or 2K in size but as you may notice I have suggested a value that is a little under that reserved area.

Anyway, as I have said in my post I was not trying to be extreemly exact here. Instead I was tryoing to teach some usefull attitude / concepts / philosophy: Play it safe and cautious when exploring unknown areas

Especially when you are a newbie and can not see the consequences of your choices far in advance.

This kind of attitude always plays back with faster learning, less frustration and better progress ...

Well at least for me it does so even afer i have become more experienced, so I honestly try to propose it to others as well. But by all mean please feel free to choose other methods of paths as you like...

to Grey beast:

Yes i guess everybody knows you can overwrite the boot loader code after it's job was done

However why to do so? To complicate things a little and save 512 bytes of RAM and later on to require 4Megabytes minimum for the OS to run?

_________________
"Any intelligent fool can make things bigger,
more complex, and more violent.
It takes a touch of genius -- and a lot of courage --
to move in the opposite direction."
Post 11 Jan 2006, 18:15
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
bogdanontanu wrote:
Yes i guess everybody knows you can overwrite the boot loader code after it's job was done

However why to do so? To complicate things a little and save 512 bytes of RAM and later on to require 4Megabytes minimum for the OS to run?


I know, it is not necessary useful, but at least it's not a big tragedy if it will erase a few boot code. If it works OK, it doesn't need to be changed.

Octavio wrote:
many bios load the boot code with this setting.


What is exactly loaded by the BIOS at boot? I only know 'bout dl register which is drive number, and cs which is 0, because boot loader starts at 0000:7C00. What else is common for BIOSes at boot? If I do not set up my own stack via the boot loader code, what does the BIOS actually load in sp and ss? any articles/links?
thanks
Post 13 Jan 2006, 17:32
View user's profile Send private message Reply with quote
dasyar



Joined: 27 Feb 2005
Posts: 33
dasyar
The function of the BIOS, is to bootstrap, which means find a bootable source, and load the bootsector of that source to 7c0. At that point the processor runs what ever is there.
Post 13 Jan 2006, 19:58
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
to which values are sp and ss (and other registers, for that matter) initialized? i only know dl is the drive number.
Post 13 Jan 2006, 20:04
View user's profile Send private message Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio
The_Grey_Beast wrote:
to which values are sp and ss (and other registers, for that matter) initialized? i only know dl is the drive number.

you must initialize registers yourself, dirfferent bios have different values
at boot time.
i start my boot code like this:
xor ax,ax es=ax ds=ax ss=ax sp=7c00h cld

cs:ip can be 0:7c00h or 7c0h:0 but the code works the same.
Post 14 Jan 2006, 10:13
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
thanks Smile

I thought the direction flag is cleared at startup? some boot loaders I see were obviously not using cld and used string instructions.. in comments it says cld is not needed?? Question
Post 15 Jan 2006, 12:07
View user's profile Send private message 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.