Direct memory addressing in TURBO C/C++

Brian K. W. Hook jdb at reef.cis.ufl.edu
Fri Mar 1 05:58:14 AEST 1991


In article <MORGAN.91Feb27195403 at chaos.cs.brandeis.edu> morgan at chaos.cs.brandeis.edu (Dylan Kaufman) writes:
|>Hi,
|>
|>I have been trying to figure out how to do direct memory addressing in
|>C++.  The addressing I am thinking about in particular is done with
|>the keyword absolute in TURBO Pascal.  For example, to turn the Num
|>Lock key off :
|>
|>var
|>  Key_Status_Bit : word absolute $0040:$0017;
|>begin
|>  Key_Status_Bits := (Key_Status_Bits and $DF);
|>end.
|>
|>I guess what I'm trying to ask is what the C equivalent (if any) for
|>absolute is...

Number one:  While I am a big proponent of liberal cross posting WHEN
APPROPRIATE, I do believe this belongs on comp.os.msdos.programmer so
please redirect all followups accordingly.

Number two:

What you need is the FAR keyword of MS or the far keyword of Borland.
In a nutshell, all pointers that you declare assume that you are using
the "near" heap, ie. that you are not addressing outside of your data 
segment (if you are using a small memory model -- small or tiny ).  While 
this is a gross oversimplification and generalization, it is the quickest 
way possible to explain this.

The area that you are trying to access is given in the segment starting
at 0000.  Depending on your memory model, a pointer that you define will
likely default to pointing in your current data segment.  You don't want
this.  Thus declare a far pointer, let's assume to a word (usigned
int).  For most purposes, all a far pointer does is let you explicity
state which segment that you wish your pointer to offset from, i.e. a
complete 20-bit IBM PC address vs. a 16-bit segment address.

unsigned int far *data;

Now point this to the correct location:

data=(unsigned char far *)MK_FP(0x0040,0x0017);

For more info on MK_FP (make far pointer) please read the Turbo C++ help
on this.

That's all there is to it.  Data is pointing there, so you can just say

*data=*data&0xDF;

Or something like that.  

Warnings:

You MUST be using TC++ keywords (this is far from ANSI compatible).
You MUST include DOS.H (which defines the MK_FP macro)

And remember, please follow up to comp.os.msdos.programmer


Brian



More information about the Comp.lang.c mailing list