delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/02/13/18:01:00

From: vAbazarov AT dAnai DOT com (Victor Bazarov)
Newsgroups: alt.msdos.programmer,comp.lang.c++,comp.arch.embedded,comp.os.msdos.djgpp
Subject: Re: Specifing the location of a function
Date: Sun, 13 Feb 2000 22:22:47 GMT
Lines: 149
Message-ID: <38a724f2.7359732@ispnews.dnai.com>
References: <8866lt$t10$1 AT nclient3-gui DOT server DOT dtn DOT ntl DOT com>
X-Trace: 3Ewu33O/xRmGqBaEHi5bt6dE4ZpS/stj50Oi0b8YpgM=
X-Complaints-To: abuse AT rcn DOT com
NNTP-Posting-Date: 13 Feb 2000 22:22:48 GMT
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

My first thought was that you were definitely in a wrong newsgroup.
It's because the things like loading functions in a specific memory
chunk is not really defined by the C++ language.  It is not even
guaranteed that your function is one contiguous piece of memory.  Read
my answers below.

You cross-posted your message in many newsgroups, in which interests of
readers may or may not be common.  Please choose the forums wiser next
time.  "COMP.LANG.C++" is definitely not the right place to post this,
and you will see it from my answers, I hope.  And since I don't read nor
do I post in other newsgroups, I will construct my reply as if it was
intended for c.l.c++ only.

On Sun, 13 Feb 2000 12:07:17 -0000, "Andrew Betts" <andy AT jasp DOT com>
wrote:

> Specifing the location of a function / dynamic loading for .COM
> 
> 
> 
> This is my problem: I have 64K of program storage space (tiny memory model)
> and vast quantities of disk space.  To increase program functionality I want
> to be able to load/unload program code at runtime.

Glorious goal.

> As tiny contains only fixed addresses/non relocatable....

If you say so.  Standard C++ doesn't define "memory model".  Nor does it
say anything about "fixed" versus "relocatable" addresses.

> 
> The way I thought I could acheive this was by telling the compiler I have a
> function at address XYZ and then loading from disk into this address the
> code I want.  I'm assuming I can generate the code that is loaded by:
> 
> 1) compiling a separate, complete program for each module I want to build
> 
> 
>  eg
>   If we represent each block of program as a letter
> 
>   prog 1 - XXXXXXXXX....AAAAAA....
>   prog 2 - XXXXXXXXX....BBB.......
>   prog 3 - XXXXXXXXX....CCCC......

You may not need to have the common part of the code in those modules
just because the compiler/linker combination may allow you to specify
the starting address of the program. OFF-TOPIC in c.l.c++, though.

> 
>   where X is the common code, '.' is free space and A, B, and C are
> different functions.
> 
>  so now any reference A, B, and C make to global data/functions will be the
> same for all three programs. (I hope)

It can be achieved, I suppose.  As I remember, Borland C++ had language
extensions allowing it to define the '.', or address counter in the
language specifically for building COM programs.  Something like
". = 0x200"

However, it definitely falls outside the topics of c.l.c++

> 
> 
> 2) Extracting and storing each section on disk
> 
>  ie
>   file 1 - AAAAAA
>   file 2 - BBB
>   file 3 - CCCC

It may not be necessary.  You can simply start reading your code from a
file at a specific offset.

> 
> 
> 3) Load and run any one of the progs (doesn't matter which -say prog 1.  And
> somewhere in the code do
> 
>  typedef void(*MYFUNCTION)();
> 
>  MYFUNCTION x=placeHolder; //  this is where I have fixed the function to be
> 
>  ...
> 
>  copyFromFileToRam(x,"file3");
>     //  XXXXXXXXX....AAAAAA....  becomes
>     //  XXXXXXXXX....CCCCAA....
> 
>  x(); // call function C

I am not sure how Standard defines writing to an address of a function.
It is probably implementation-defined.  I just don't have the Standard
in front of me.  However, you may not really be interested in the
Standard ways, I guess.  Portability is not your goal.  Then it's truly
OFF-TOPIC in c.l.c++.

> 
>  ...
> 
>  copyFromFileToRam(x,"file2");
>     //  XXXXXXXXX....CCCCAA....  becomes
>     //  XXXXXXXXX....BBBCAA....
> 
>  x(); // call function B
> 
>   ...etc
> 
> 
> 
> ---------
> 
> It this technique possible?

Everything is possible.  That's the phrase you, as a programmer, should
learn by heart and use any time somebody asks you that question.

> 
> Specifically:
>  a)  Can I tell the linker to put a function at a certain location

If depends on your linker.  Wrong newsgroup to ask.  Try
comp.os.ms-dos.programmer.  Oh, you sort of did... OK.  Don't ask it
here, then.  Waste of bandwidth.

>  b)  Is it safe to assume in the above 3 programs that the common code with
> be in the same place in each.  (of course this relies on the fixed location
> code (ABC) not getting in the way)

It may be safe within the limitations imposed by your system and assumed
by you.  Again, it may not.

> 
> 
> Alternatively, does anyone know a way to acheive the same thing?

Some compilers (linkers) have built-in capability to produce overlays.
I can remember at least two: Microsoft Linker that came in MS FORTRAN v4
and v5 compiler packages, and Zortech C++ 3.0 linker.  Both could
probably be still found, but with DOS being ever-shrinking environment,
it is becoming increasingly difficult, I suppose.

Good luck!

Victor
--
Please remove capital A's from my address when replying by mail

- Raw text -


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