View Single Post
Old Feb 24th, 2008, 4:32 AM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 927
Rep Power: 4 lectricpharaoh will become famous soon enough
Re: Segmentation Question

I also have some implementation details for x86 CPUs for you; I didn't put it in the first post because it was mostly theory. There are three kinds of segment registers, which are code, stack, and data.

The CS (code segment) register stores the segment or selector for currently-executing code. You cannot directly assign to it as you can for other segment registers, but you can assign it to another register or push it on to the stack. The only way to change the value of CS is through a context switch (call, jump, interrupt, etc). IP (instruction pointer) is paired with CS to form the logical address CS:IP; this stores the address of the next instruction. When you execute a call or int instruction, either IP (near call) or CS:IP (far call, int) is pushed on the stack so the code can return. Like CS, IP cannot be directly modified.

The SS (stack segment) register stores the segment or selector value for the current stack. You can directly assign to this or otherwise muck about with it, but you'd best know exactly what you're doing. It's paired with SP (stack pointer) to form the logical address SS:SP. A push stores data at SS:SP and decrements SP, whereas a pop does the reverse. The BP register (base pointer) also implicitly uses SS as its segment register of choice, but you can override this in some circumstances with a segment override. The reason BP uses the stack segment is because it's often used to create 'stack frames' for higher-level languages.

There are two (four on 386+) data segment registers. These are DS (data segment) and ES (extra segment). By default, most data accesses will use DS. The reason ES exists is to allow you to copy data from one segment to another, without having to constantly reload the registers. On the 386 and later CPUs, there are two additional segment registers, FS and GS. I don't think that they stand for anything; I suppose they picked those names because they already had DS and ES, so they fit the pattern. In general, you can use any of these coupled with one of the general-purpose registers for addressing, though you may need an override if using a segment register besides DS (SS for BP/BP). Though there was no real need for additional segment registers, it was helpful for certain tasks. For example, you could point one at your program's data, a second at a shared memory block, and another at a memory-mapped device like a video card.

Note also that I didn't mention the extended registers, but they work identically. For example, what is DS:BX in 16-bit addressing is DS:EBX for 32-bit.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote