delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/12/02/04:01:06

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Accessing Memory?
Date: Wed, 03 Dec 1997 00:08:26 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 99
Message-ID: <3484A2FA.1E3B@cs.com>
References: <348310b5 DOT 2770526 AT nntpserver DOT swip DOT net>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp217.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

This is a multi-part message in MIME format.

--------------2E8774073ACA
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Rikard Bj=F6rklind wrote:
> =

> Hi! I am new to DJGPP.. I have programmed some pascal,
> but I want to use C instead...  Now there is one problem..
> In real mode programs I know how to access memory.. like
> 0a000h:0abcdh .. but in pmode it is different, right? Now how do I do
> to adress mem in djgpp? in TMT-pascal, a pmode compiler, you
> add a variable called _zero to the offset.  is it something similar
> in DJGPP??  Well, bye!

You don't need to do anything special at all to access protected mode
memory under DJGPP (or any DPMI program, for that matter).  All
available physical and virtual memory on your computer is mapped into a
single linear virtual address space with a maximum size of 4 GB (4 x
1024 x 1024 x 1024 bytes).  Practically, the maximum size of your
address space is limited by the memory provided by the DPMI host in
question (Win95 provides up to 64 MB, cwsdpmi provides up to 256 MB,
etc.).

How much does all of this matter to you?  Not one bit.  Go ahead and
malloc() as much memory as you want.  Feel free to create 10 MB global
arrays.  The DPMI interface handles all of the internal stuff completely
transparently.  I'm attaching a fun little program you might want to try
that demonstrates this.

The _only_ case where you actually need to worry about the mechanics of
the DPMI interface is when you try to access memory-mapped devices in
conventional (real-mode) memory.  This includes the VGA or text video
memory, interrupt tables, hardware ports, and other similar things.  A
complete discussion of how to do this sort of work in protected mode
would be far too lengthy for an email message, but the DJGPP Frequently
Asked Questions list (v2/faq210b.zip from SimTel or online at
http://www.delorie.com/djgpp/v2faq/) discusses it in detail in chapters
10, 17, and 18, and gives additional pointers to more information.  It's
a tremendously interesting bit of reading; I recommend you give it a
try.

Good luck!

-- =

John M. Aldrich <fighteer AT cs DOT com>                      =


* Anything that happens, happens.
* Anything that, in happening, causes something else to happen,
  causes something else to happen.
* Anything that, in happening, causes itself to happen again, happens
  again.
* It doesn't necessarily do it in chronological order, though.
 =

                                       --- Douglas Adams

--------------2E8774073ACA
Content-Type: text/plain; charset=us-ascii; name="Lotsamem.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Lotsamem.c"

/* lotsamem.c */
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    char *p;
    int megs, init;

    printf( "How many megabytes would you like to allocate (1-256)? " );
    if ( scanf( "%d", &megs ) < 1 || megs < 1 || megs > 256 )
        printf( "That's not what I asked for, dufus.\n" );
    else
    {
        printf( "Initialize the memory (1 = yes, 0 = no)? " );
        scanf( "%d", &init );
        if ( init )
            p = calloc( 1, megs * 1048576 );
        else
            p = malloc( megs * 1048576 );
        if ( p != NULL )
        {
            printf( "Wow!  You just allocated %d megabytes!\n", megs );
            free( p );
        }
        else
            printf( "Whoops!  You don't have enough memory available.  Sorry.\n" );
    }
    return 0;
}

--------------2E8774073ACA--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019