Page 1 of 1

Need help with real mode segmentation

Posted: Sat Jul 22, 2006 6:40 am
by joke
First of all, I'm new to the forum so hi all! :D


I'm currently trying to develop a very simple, real mode os (monotasking, like dos). My problem is that I only find tutors for pmode and nothing for real mode. Here's my question: I've found a very simple bootloader that loads my kernel in 1000:0000 How can i set-up my code,data and stack segments?
(Should i load the base addresses in cs,ds,ss?)

thanks

Posted: Sat Jul 22, 2006 8:14 am
by carbonBased
You should do a search for some information on real mode system architecture (the intel manuals are good for this)... this information does exist, and can be used to differentiate a lot of this pmode discussion from what you want.

I say this, because you seem to be implying that there is some setup involved in defining segments. While this is true in pmode, real mode segments are simply a portion of the full address space, such that:

linear_address = (segment * 16) + segment_offset

Each segment is also fixed at 64k (limited by a 16-bit offset value).

Hopefully that helps,
Jeff

PS: I think anybody that feels the urge to chime in about the realmode idt being a subset of the pmode idt should ignore this urge, as it would just muddy the water, and confuse the issue (IMO, at least...)

Posted: Sat Jul 22, 2006 10:03 am
by joke
I've been examining the source code of some real mode oses (like MiniDos by Dex), and there is extensive usage of all three segment registers. Eg, every time a function is called, there's a pusha. But to use pusha there also must be a stack segment. So, if I want a 64k stack at 0300:0000 then what must i do, just
mov ss, 0x12C0 ? (seg*16+offset, if i've understood correctly the formula)
is that enough?
Thanks again

Posted: Sat Jul 22, 2006 12:17 pm
by carbonBased
Generally speaking, without a stack, you cannot call functions... this is a programming fundamental which osdevers should know and understand.

In order to setup a stack in real mode, yes, all you need to do is load the ss register.

The equation is for converting linear addresses (which you would see in a non-segmented platform) to segmented addresses (and back again). You syntax of 0300:0000 *is* segmented (this syntax generally means segment:offset). As such, you'd simply load ss with 300.

The equation is there so that one understands that 0300:0000 is actually referencing the linear address 300*16+0 = 3000 (I'm assuming 0300 is hex).

--Jeff

Posted: Sat Jul 22, 2006 1:21 pm
by joke
Thnaks a lot! :D ( i just found it too strange that real mode segmentation was so simple!)