Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Segments/Offsets 1

Status
Not open for further replies.

Dilenger4

Programmer
Nov 28, 2001
13
0
0
US
First off i just want to say hi to everybody! Im a Java/Visual Basic programmer of 3 years and a always wanted to learn assembly, but im having alittle trouble understanding segments and offsets.

At the bottom is a piece of a simple Hello World prog using a86 compiler.

The way segments and offsets were explained to me was with this formula.

;OFFSET = SEGMENT * 16
;SEGMENT = OFFSET / 16 (the lower 4 bits are lost)

From what i can gather the total number of bits with the
segment and offset combined is always 20, the Message can never be more than 2 word size(32 bits) or an error occures.

So question number one: Does the segment hold 16 bits and the offset hold 4?

Question number two: If i program with 32 bit registers can
i forgo using segments and offsets or are they always used?

Thanks for the help!:)


Message db "Yummy Ham!$" ; message to be display
mov dx,OFFSET Message ; offset of Message is in DX
mov ax,SEG Message ; segment of Message is in AX
mov ds,ax ; move segment of Message into a segment reg



 
First answer: In 16-bit programming (or even if using 32-bit registers in a 16-bit Real mode), BOTH the OFFSET and the SEGMENT are 16-bits.
What happens is that the segment is multiplied by 10h (16) and THEN the offset is added.
In 32-bit mode programming (available only in Protected Mode) the offset is 32-bits while the Segment is only a handle to the memory section it refers to, this gets pretty complicated... some OS's however make all segments start at absolute 00000000h, so that you can pretty much ignore segments.

Second answer: Depends on your MODE. In 16-bit mode you will still use segments and offsets. In 32-bit mode, it will depend on your OS. Windows I think does not use segments (although other OS's might). Note that 32-bit mode is a Protected Mode concept, if you intend to use DOS (which is Real mode and hence only supports 16-bit mode) you MUST use segments and offset (although nothing prevents you from using 32-bit registers).

In many cases you SHOULD ignore segments and pretend that you can only have offsets. The only time you generally want to do segments is if you need to go beyond the limit, or at the start of a DOS program to set the segments properly.

Anyway the message can be up to the limit of the Offset. In 16-bit mode, the limit of the Offset is 65535 (0ffffh) so your message can only be that number of bytes long. In 32-bit mode the limit is somewhere in the gigabyte range, so your message can be that number of bytes long... although you'll run out of memory before that can happen.



"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
So when we say somthing like below. dx is just pointing to the offset of the Message? The actual offset of the message is not contained within dx? Same for ax too.

Message db "Hello World!$"
Mov dx, OFFSET Message
Mov ax, SEG Message

So "Hello World" would be 10 bits? or 2 word(16 bits)im not too sure. Some people say one and some say the other.

Thanks for the help.
 
Let me put it this way.

Let's imagine you have this little nifty robot called IntelProcessor with an arm called AddressSpace which can extend and retract up to 65536 inches. But what if you need to reach something which happens to be 65537 inches away from IntelProcessor? Then you need to be able to move your robot's body so that it can reach the object with its AddressSpace arm. Now, IntelProcessor does this by making use of its SegmentSystem body. The SegmentSystem body moves by units of 16 inches so that it can reach something that is 65537, 65538 or 655360 inches away from the robot's position (which is sometimes called the ABSOLUTE ADDRESS SPACE).

So in one way of looking at things, it is 16-bit because the SegmentSystem body has been moved to with 65536 bytes of the message. But in the ABSOLUTE ADDRESS SPACE it is 10-bits because IntelProcessor also needs to know where its SegmentSystem body is.

Or better yet, just pretend your IntelProcessor doesn't have a SegmentSystem body to move around in the ABSOLUTE ADDRESS SPACE. Just consider it as having an AddressSpace arm that goes 65536 inches. Besides, in Win32 segments are no longer used anyway... "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top