Far pointer
This article needs more citations. (February 2024) |
In a segmented architecture computer, a far pointer is a pointer to memory in a specific context,[1] 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
[edit]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 21 bits[Note 1]. 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 far qualifier; e.g., char far *p; defined a far pointer to a char. The difficulty of normalizing far pointers could be avoided with the non-standard huge qualifier. On other compilers it was done using an equally non-standard __far qualifier.[2]
Example of far pointer:
#include <stdio.h>
int main() {
char far *p = (char far *)0x55550005;
char far *q = (char far *)0x53332225;
*p = 80;
(*p)++;
printf("%d", *q);
return 0;
}
- Output of the following program: 81; Because both addresses point to same location.
- Physical Address = (value of segment register) × 0x10 + (value of offset).
- Location pointed to by pointer
pis : 0x5555 × 0x10 + 0x0005 = 0x55555 - Location pointed to by pointer
qis : 0x5333 × 0x10 + 0x2225 = 0x55555 - So,
pandqboth point to the same location 0x55555.
Legacy
[edit]The Windows API was originally designed for 16-bit x86, and provided typedefs for working with near and far pointers.[3] In the WinAPI convention, the names of far pointer types are prefixed with LP (for long pointer
).[4] For example, PDWORD denotes a near pointer to a DWORD, and LPDWORD denotes the corresponding far pointer.[5]
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.[5] This has resulted in code originally written for Win16 performing redundant pointer type conversions once ported to Win32.[3]
In ESA/390 and z/Architecture
[edit]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.[6][7] 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".[8]
On AVR
[edit]On larger AVR microcontrollers, addressing more than 64 KB is possible via far pointers, which include an identification of the 64 KB "page" to access.
Notes
[edit]- ↑ Early x86 processors only had a 20-bit address bus, so results above 1 MiB wrapped around to zero, discarding the carry bit. PCs using the 80286 or newer, which had the necessary address lines, implemented the A20 gate to toggle this behavior for backwards compatibility with older software.
References
[edit]- ↑ Ethan L. Miller; George Neville-Neil; Achilles Benetopoulos; Pankaj Mehra; Daniel Bittman (November 17, 2023). "Pointers in Far Memory". Communications of the ACM. 66 (12): 40–45. doi:10.1145/3617581. ISSN 0001-0782. Wikidata Q130948234. Retrieved February 11, 2024.
- ↑ "Introduction to Open Watcom C/C++". GitHub. 2024. Retrieved February 11, 2024.
- 1 2 Chen, Raymond (November 3, 2004). "Why do I sometimes see redundant casts before casting to LPARAM?" (blog post). The Old New Thing. Microsoft.
- ↑ Petzold, Charles (1996). "Hello, Windows 95". Programming Windows 95. Redmond, Washington: Microsoft Press. ISBN 1-55615-676-6.
- 1 2 Lauer, Thomas (1996). "Appendix 1". Porting to Win32™. New York: Springer–Verlag. pp. 355–358. doi:10.1007/978-1-4612-0727-6. ISBN 0-387-94572-5. Archived from the original on June 22, 2026.
- ↑ "Access Register Mode Support". SAS/C Compiler and Library User's Guide, Release 7.00. SAS.
- ↑ "Metal C Programming Guide and Reference" (PDF). IBM. p. 35.
- ↑ Rogers, P.; Hering, P.; Kuehner, L.; Lafitte, J.; Minatto, M.; Mourao, J.; Naidoo, M.; Peleg, G.; Philipi, E.; Rodolfi, G. (December 2007). "z/OS XL C/C++ Metal option". z/OS Version 1 Release 9 Implementation (PDF) (1st ed.). IBM. pp. 251–274. ISBN 0738488607.