Date: Tue, 17 Dec 1996 09:09:56 +0200 (IST) From: Eli Zaretskii To: Aaron m Clemmer cc: djgpp AT delorie DOT com Subject: Re: pmode selectors In-Reply-To: <19961217.041828.8319.2.aclemmer@juno.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII On Mon, 16 Dec 1996, Aaron m Clemmer wrote: > Ok, lets see if I really understand all this. If a buffer such > as __tb resides in conventional memory, it has a rmode seg:off, so trying > to access it by normal methods (ie *(__tb +1) = 5) will cause a GPF. So > I should use functions like movedata, which can copy data into conv mem > buffers. Close, but not quite there yet. `__tb' can be used as both seg:off pair and as a protected-mode selector:offset. If you need the real-mode style address, you use (__tb >> 4) & 0xffff as the segment and (__tb & 0xf) as the offset. If you need a PM-style address, you use `_dos_ds' (a macro defined on ) as the selector and __tb as the offset. > But if I have a buffer that resides above the first meg, I can > either use a selector to access it, or access it directly? And if I can > just access it directly, I really wouldn't need to use a selector. You can only access it directly if the buffer is part of your program's address space. If it is not, like in case of some memory-mapped devices which are mapped above 1MB mark, you still need to use the selector:offset method. (This is explained in section 18.7 of the DJGPP FAQ list in more detail.) > And doesn't a selector just contain the adress of a buffer, like a > pointer? No. A selector is an index into a table which describes the region of memory: its base address, its size, and access rights. Your program's data space is described by the selector whose index is loaded into the DS register at program startup. The pointer that you use to access your data ``directly'' is just an offset relative to the base address of the memory region described by the selector in DS. > And static arrays, dynamic arrays created with malloc(), and > blocks of memory allocated with __dpmi_allocate_memory() all exist above > conv mem, so trying to access these _doesn't_ GPF. The fact that the buffer is below or above 1MB is only relevant to its real-mode address (since real mode can only access the first 1MByte). The protected mode selector:offset method works for *any* buffer *anywhere* on the PC. Memory allocated by `malloc' is inside your program's address space, so you can just use the usual pointers (which are offsets into the DS selector). Memory allocated by DPMI functions (such as `__dpmi_allocate_memory') is NOT in your address space, so you need to explicitly set the selector to access it. > Wouldn't it be grand if there was a good source of info on > general pmode programming? There are a couple of textbooks on this subject. For example, check out "Extending DOS, 2nd ed."