Hi there,

I've been reading Art Of Assembly and it talks about Segments and 16 bit memory addressing.

Given that my computer is 32 bit, do I need to declare segments e.g. a data segment, at all?

Furthermore, what are the segment registers used for in a 32 bit envirmonment?

Thanks.

S
Posted on 2002-03-22 12:50:59 by Station
Under win32 you don't have to worry about the segment registers. You can still declare a data section in your file. win32 uses flat linear addressing wich makes things a lot easier.
Posted on 2002-03-22 13:38:16 by Quantum
Quantum is in essence correct. But you still need to declair "code segments" such as the .code and .data or .data?

The old 16 bit segments you're reading up on are still used, but for a different function entirely. I cant go into great depths (as i've forgotten most of the details) but i can say that the fact you have "seemingly" flat memory is still being emulated thru these segments. The processor uses the segment register to point to a "descripter table" that has all of the available memory, indexed into "pages". All of this is done by Intel and the OS (windows). You need not worry about this stuff as Quantum suggested (but we are now talking about Intel's Protected Mode memory management). Just realize they *still* have a purpose, and is part of the mechanics of how you can seemingly get 4Gb of addressable space, dispite the fact you only have 256Mb ram.

This means that you can percieve the memory as one *big* flat area with a starting point, and an index 32 bits long, and no specail translations needed (as you would moving from segment to segment in the old 16 bit style), while moving from byte to byte.

In Win32 apps, you dont even need to think about *where* the starting point is!! This is even easier!! MASM does the dirty work of keeping your 'offsets' relative to the start of the code and data segments. Which is why you still need to tell masm you have these segments. The OS, when loading your programs, will *litterally* skatter your program in fragments all over memory (in chunks called pages). Pages are given privilage levels like *read only* (such as a CONST segment). When the OS fills memory pages with *pieces* of your program it places the needed order of your program (in page indexes) in another table, and the regulates the segment register to realize this table. In the end, between the Os and Intel your program will execute linearly as you have written it (dispite being tossed all over memory).

When you access memory outside of the 'relative area' your code segments indicate (and wasnt givent to you by the os ~ ie global alloc) you get the famous "page fault error", which is exactly as it implies. Your asking to mess with memory not within the # of pages your program occupies (code and data). And it knows this because you origionally declaired the sections, and it know how long each compiled to!!...

I think i will stop myself here. This is a very *big* topic to learn, and looking back i see im moving into a mild rant, rather than a Q/A :grin:

I hope you followed all that?? Long story short (litterally), write code as you always have and forget about how far away in memory you data might be, but dont use the segment registers (or you will piss off the OS pretty good :grin: )

Enjoy...
:NaN:
Posted on 2002-03-23 01:05:11 by NaN
Here's the low down. In real mode (where segments exist) if DS = 0x100 and ES = 0x110 then DS:0010 = ES:0000. In protected mode (where Win32 exist) this is never true. You can think of a segment (aka section) as a being 4GB in size; but they are not offsets * 16 from 0. The processor does a conversion from virtual (what the programmer interface with) to physical (you'd have to be a nut case and 3 to interface with) memory address space.

Once you understand all of this you don't really think about it (unless you're a die hard, real man, binary coder).

If you're a nut case, I suggest the following reading material:
   • The 80x86 IBM PC... ISBN 0-13-016568-9
   • Understaning the Lnux Kernel ISBN 0-596-00002-2
Posted on 2002-03-23 03:58:14 by eet_1024
Hey, thanks for the book reference.:alright:
Posted on 2002-03-23 10:14:48 by Station
In fact, how feasible do you think it would be to build an Altair type machine out of bits of wires and program it using light switches?
Posted on 2002-03-23 10:33:16 by Station
Hi,

If the segment registers are used by Windows, how is it that Windows can still run old MS-DOS programmes which make vigorous use of the segment registers for their own needs?

Cheers,

S
Posted on 2002-03-23 15:02:38 by Station
By Emulating the old MS DOS and probabaly switching control back to real mode (but not before it properly allocates the memory space needed, such that pages are preserved).

This is just a guess... I could be wrong, and even the Intel real mode gets emulated as well, but i doubt it.

NaN
Posted on 2002-03-23 17:54:34 by NaN
If I wrote a program which ran from the boot of a floppy disk and proceeded to make no use of any operating system, could I use the segment registers any way that I pleased?

Meaning, would the hardware be happy to have a program access memory using absolute references and segment-offset references ?

Cheers,

S
Posted on 2002-03-23 18:41:01 by Station
Station: yes, it would.
e.g.

mov ,dx

or

.radix 16
push 0b800
pop es
mov es:,dl


would not leave a sad Mac, so to speak.
to use flat memory space, you would have to switch to protected mode, i think.
Posted on 2002-03-23 19:06:43 by jademtech
Station,

There are different members here who have written their own OS that runs from a floppy, some roll their own from scratch, some use one of the linux kernels as reference and if you get it right, it will boot from the floppy and run the OS you design.

If you study the Intel literature on processor design, you will find information on using the segmented architecture if you are interested in that style.

There are some advantages in flat memory model in linear address space and code simplification. It really depends what you have in mind.

Good luck with it.

hutch@movsd.com
Posted on 2002-03-23 19:13:02 by hutch--
Well, since you're a "Die hard" 16b programmer and 32b is for the devil, you have free run of the entire system. I hope you're using FAsm since only nut cases use it (FAsm is for people who believe variables are really pointers).

edit:
BTW, you can even replace the "slow" BIOS interupts ;)
Posted on 2002-03-24 02:18:28 by eet_1024
Windows can manage 16 bit tasks programmed for DOS because Intel and Intelish CPU's have been designed to work with both. To run a DOS program, Windows sets up a task selector (like a segment selector) that specifies that the task should run as 16bit code in V86 mode. V86 mode works by treating segments as they would be in real mode, however memory paging and segment protection features are still active. It's rather complicated, but between V86 and dos emulation, that's how Windows gets the job done. Keep in mind that Windows grew out of the DOS environment, and finding a way to multitask legacy ware was one of the first things MS set out to acheive with Windows, and their solution to that problem is largely responsable for the structure of the Window VMM.
Posted on 2002-03-24 15:54:48 by Canite