From: James Daughtry on
How and why does one use the segment registers (CS, DS, SS, and ES)?
Actually, I'm looking more for the why than the how. ;-) Are they
mostly geared toward 16-bit programming and I simply haven't
encountered a need for them?

Cheers!

From: randyhyde@earthlink.net on

James Daughtry wrote:
> How and why does one use the segment registers (CS, DS, SS, and ES)?
> Actually, I'm looking more for the why than the how. ;-) Are they
> mostly geared toward 16-bit programming and I simply haven't
> encountered a need for them?

If you're programming in a typical 32-bit environment (Win32, Linux),
you'll not need to use the segment register very often. FS gets used
for structured exception handling. (and you don't actually modify FS,
the value is given to you, you just reference the segment pointed at by
FS). GS also has a special purpose under Win32, but I've forgotten at
the moment. I don't know if Linux has similar uses for the segment
registers.

By and large, however, you are correct. DOS is the main reason people
use segmentation.

In general, segmentation could be use for some *very* powerful
purposes. For example, the Pentia have a 36-bit address space and
you're limited to 32-bits in flat model. A decent 32-bit OS that used
segmentation would allow you to do some really clever things, such as
independent address spaces for memory-mapped files, and things like
that. If you can dig up the April 1988 issue of Byte Magazine, you can
read about segmentation's benefits in the article I wrote "Memory
Management Primer".
Cheers,
Randy Hyde

From: santosh on
James Daughtry wrote:
> How and why does one use the segment registers (CS, DS, SS, and ES)?
> Actually, I'm looking more for the why than the how. ;-) Are they
> mostly geared toward 16-bit programming and I simply haven't
> encountered a need for them?

Well, segmentation exists even in 32 bit protected mode, though most
flat memory model operating systems like Windows and Linux effectively
hide it by setting the segment registers to hold selectors pointing to
the same start address.

Under real model, segment registers are used to construct the physical
address on every memory reference. The hold a segment address that
starts on every paragraph boundary, (i.e. every 10h bytes), in the
CPU's address space (a megabyte). The segment address is used as a base
address pointing at the start of a "segment". A segment is a region of
memory, whose start is specified in a segment register/selector and
which extends for some number of bytes. Under real mode the "range" of
a segment is always 65536 bytes. In protected mode, the operating
system can set the range for each segment.

In real mode, to construct the actual physical address, the segment
address in the implicit or explicit segment register is multiplied by
16 and the offset address is added to it. The CPU does this
automatically and transparently. You simply have to specify the
locations that contain the segment and offset addresses. For the
former, it is either a segment register assumed from a built-in set of
implicit rules or one specified by yourself as a segment override. The
later is of course a label, or a register value, (for which only BX,
SI, DI, SP may hold offsets), or a literal constant.

Segmentation came about because of certain decisions taken by Intel for
porting CP/M programs from 8080 to the 8086/8088 under DOS. See the
Intel manuals for definitive guide to segmentation and it's history.