[comp.lang.c] Direct memory addressing in TURBO C/C++

morgan@chaos.cs.brandeis.edu (Dylan Kaufman) (02/28/91)

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...

Thanks,


--
-<>Dylan<>-                     MA EMT-M, CA EMT-1A, BEMCo 107
Dylan Kaufman 			Major in Computer Science
morgan@chaos.cs.brandeis.edu	Brandeis University, Waltham, MA
------<< Support your local Emergency Medical Services >>-------
And on the seventh day, even He rested.  We don't.  -Emerg. Med. Svcs.

jdb@reef.cis.ufl.edu (Brian K. W. Hook) (03/01/91)

In article <MORGAN.91Feb27195403@chaos.cs.brandeis.edu> morgan@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

catfood@NCoast.ORG (Mark W. Schumann) (03/03/91)

In article <MORGAN.91Feb27195403@chaos.cs.brandeis.edu> morgan@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.
>
You can do this with--

    * (unsigned int *) MK_FP (0x0040, 0x0017);

MK_FP() is a macro that is defined in Turbo C (2.0 at least) and
Mix Power C.  I think it is defined in MC.  This is NOT considered
part of ANSI but it looks as if you are trying to do some very
machine-dependent stuff anyway.

Hope it helps.


-- 
============================================================
Mark W. Schumann  3111 Mapledale Avenue, Cleveland 44109 USA
Domain: catfood@ncoast.org
UUCP:   ...!mailrus!usenet.ins.cwru.edu!ncoast!catfood

berryc@arcturus.uucp (Craig D. Berry (x1710)) (03/09/91)

catfood@NCoast.ORG (Mark W. Schumann) writes:

>In article <MORGAN.91Feb27195403@chaos.cs.brandeis.edu> morgan@chaos.cs.brandeis.edu (Dylan Kaufman) writes:
>>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.
>>
>You can do this with--
>    * (unsigned int *) MK_FP (0x0040, 0x0017);
>MK_FP() is a macro that is defined in Turbo C (2.0 at least) and
>Mix Power C.  I think it is defined in MC.  This is NOT considered
>part of ANSI but it looks as if you are trying to do some very
>machine-dependent stuff anyway.

Note that for total safety, that should be (unsigned int far *).  This
would be the default in a large model program compilation, but it never
hurts to be careful...

catfood@NCoast.ORG (Mark W. Schumann) (03/17/91)

berryc@arcturus.uucp (Craig D. Berry (x1710)) writes:
>catfood@NCoast.ORG (Mark W. Schumann) writes:

>>You can do this with--
>>    * (unsigned int *) MK_FP (0x0040, 0x0017);
>>MK_FP() is a macro that is defined in Turbo C (2.0 at least) and
>>Mix Power C.  I think it is defined in MC.  This is NOT considered
>>part of ANSI but it looks as if you are trying to do some very
>>machine-dependent stuff anyway.
>
>Note that for total safety, that should be (unsigned int far *).  This
>would be the default in a large model program compilation, but it never
>hurts to be careful...

I stand corrected.

-- 
============================================================
Mark W. Schumann  3111 Mapledale Avenue, Cleveland 44109 USA
Domain: catfood@ncoast.org
UUCP:   ...!mailrus!usenet.ins.cwru.edu!ncoast!catfood