News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Segments and Offsets?

Started by ambir, March 28, 2006, 10:32:41 PM

Previous topic - Next topic

ambir

Sorry but I am VERY new to ASM. the only thing i haven't figured out as yet is how the segments and offsets work.  i think they are like pointers but i'm not sure so help me out some plz.

i think they are suppose to be someting like:

xxxx:xxxx (with numbers ofcourse) ( i think it has something to do with 16, but what do i know, i'm a know nothing )

but i'm not sure how they work, why they work and when do people use them? examples (small or long) would be nice too plz :green2

Thank You

MichaelW

This subject belongs in the 16-bit DOS Programming forum, and I will be moving it there shortly.

This is recycled from an old tutorial I wrote:

The memory in a PC is arranged as a one-dimensional array of bytes. The bytes are numbered sequentially starting at 0. This number is referred to as a "physical address". When the processor accesses memory, it uses the physical address to specify the memory location. The entire range of physical addresses that the processor can generate is referred to as its "address space".

The 8086 and 8088 were 16-bit processors with 20-bit address spaces. Intel's solution to the problem of generating a 20-bit physical address with a processor that could directly manipulate values no larger than 16 bits was to use a "logical" address that consisted of two 16-bit components. The first component is a "segment" address and the second component is an "offset" address. To generate a 20-bit physical address, the processor (effectively) shifts the segment address left by 4 bits and adds it to the offset address.

From the programmer's perspective, the segment address specifies the region of memory (referred to as a "segment") and the offset address specifies the location within the region. Because the least significant 4 bits of the segment address are cleared in the process of generating the physical address, segments must start on 16 byte boundaries. Because the offset address is a 16-bit value, the maximum segment size is 65,536 bytes (2^16).

For example:

The BIOS Data Area is a 256-byte block of memory at segment address 40h that the system BIOS uses as a data segment. The BIOS maintains a 32-bit count of timer ticks since midnight at offset 6Ch in the BIOS Data Area. The address of the count in segment:offset format would be 0040:006C, and the physical (AKA absolute) address would be 046Ch:

0040h SHL 4 = 0400h
0400h + 006Ch = 046Ch


To access this count in code you would normally load the segment address into a segment register, and (for 16-bit code) the offset address into a base or index register:

mov  ax, 40h
mov  es, ax
mov  bx, 4ch


You could then read the count into the DX:AX register pair with:

mov  ax,es:[bx]
mov  dx,es:[bx+2]


The ES segment override is necessary because MOV, like most instructions that access data, uses DS by default.

Because the absolute address will fit into a 16-bit register, you could just as well access the count relative to segment 0, in which case the address of the count in segment:offset format would be 0000:046C, and the absolute address would be the same 046Ch.

eschew obfuscation

ambir

Ok, thanks a ton, i have better understanding of it now. But you moved the post because it isn't neccesary to learn how to use segments and offsets in win32 masm programming?

MichaelW

For Win32 you do use offset addresses (32-bit), but you do not normally do anything with segment addresses, and only rarely with segment registers. Segment registers contain segment addresses, as I described, only for 'real' mode. For 'protected' mode the segment registers contain 'selectors', which are essentially pointers to segment 'descriptors', which are essentially data structures that specify the segment characteristics, base address, size, etc. Windows sets up and controls the segments and the segment registers. So for Win32 programming, knowledge of real mode segment addressing is essentially useless. In the unlikely event that you did anything with structured exception handling, you would need to know how to do a segment override.

eschew obfuscation