↑ arvau

Wikipedia / Far pointer

In a segmented architecture computer, a far pointer is a pointer to memory in a specific context, such as a segment selector making it possible to point to addresses outside of the default segment.

Comparison and arithmetic on far pointers is problematic: there can be several different segment-offset address pairs pointing to one physical address.

In 16-bit x86

For example, in an Intel 8086, as well as in later processors running 16-bit code, a far pointer has two parts: a 16-bit segment value, and a 16-bit offset value. A linear address is obtained by shifting the binary segment value four times to the left, and then adding the offset value. Hence the effective address is 21bits. There can be up to 4096 different segment-offset address pairs pointing to one physical address. To compare two far pointers, they must first be normalized to a form with only one representation address. Such a normalized form may be one that minimizes the segment (maximizing the offset), minimizes the offset (maximizing the segment), or converts the 2-part segmented address to a (20-bit) linear representation. One commonly used normalized form minimizes the offset part of the address to a value less than 16 (10 hex): such a normalization can be computed simply by taking the low-order 4 bits of the unnormalized offset as the new offset, and adding to the unnormalized segment the unnormalized offset shifted right by 4 bits.

On C compilers targeting the 8086 processor family, far pointers were declared using a non-standard qualifier; e.g., defined a far pointer to a char. The difficulty of normalizing far pointers could be avoided with the non-standard qualifier. On other compilers it was done using an equally non-standard qualifier.

Example of far pointer:

  1. Output of the following program: 81; Because both addresses point to same location.

  2. Physical Address = (value of segment register) × 0x10 + (value of offset).

  3. Location pointed to by pointer is : 0x5555 × 0x10 + 0x0005 = 0x55555

  4. Location pointed to by pointer is : 0x5333 × 0x10 + 0x2225 = 0x55555

  5. So, and both point to the same location 0x55555.

Legacy

The Windows API was originally designed for 16-bit x86, and provided typedefs for working with near and far pointers. In the WinAPI convention, the names of far pointer types are prefixed with LP (for long pointer). For example, PDWORD denotes a near pointer to a DWORD, and LPDWORD denotes the corresponding far pointer.

In Win32, the older pointer typedefs are retained for backwards-compatibility. Because the need for far pointers was obviated, there was no longer a distinction between the actual types underlying the near and far pointer typedefs. This has resulted in code originally written for Win16 performing redundant pointer type conversions once ported to Win32.

In ESA/390 and z/Architecture

On some C compilers for ESA/390 and z/Architecture, far pointers can be used to include an identifier of an alternate address space to access. ESA/370, ESA/390, and z/Architecture include facilities for a program running in one address space to access data in another via "access registers".

On AVR

On larger AVR microcontrollers, addressing more than 64KB is possible via far pointers, which include an identification of the 64KB "page" to access.

Notes

References