comp.lang.ada
 help / color / mirror / Atom feed
From: pburnand0-news@yahoo.com
Subject: Re: function at specific address
Date: Thu, 04 Mar 2004 06:18:40 +0100
Date: 2004-03-04T06:18:40+01:00	[thread overview]
Message-ID: <4046b131_1@127.0.0.1> (raw)
In-Reply-To: m23c8ydm5v.fsf@jvdsys.demon.nl

Jerry van Dijk wrote:

> 
> Szymon Guz <guzo@stud.ics.p.lodz.pl> writes:
> 
>> is there a possibility to make a function that after compiling will point
>> to a specific address in the memory ? And I want to set the address in
>> the code.
> 
> 
> Do you think your OS is going to allow that ?
> 

That's a good question to ask if the OS allows this...
I think this would anyway be for a specific machine with a minimal or even 
no OS at all (ie just a bootstrap for an embedded system)...


There is perhaps a first simple solution :
A so called at_clause:


proc_address : constant Address := 16#789abcde#;
procedure  foo( i: integer; j: integer; k: integer );
for foo'Address use proc_address;


procedure  foo( i: integer; j: integer; k: integer ) is
begin
  Text_IO.Put_Line( "Hello, world !" );
  Text_IO.Put( "i = " );
  Text_IO.Put( i );
  Text_IO.Newline;
  Text_IO.Put( "j = " );
  Text_IO.Put( j );
  Text_IO.Newline;
  Text_IO.Put( "k = " );
  Text_IO.Put( k );
  Text_IO.Newline;
end;






Note: the following solution has not been tested.  This is only an idea.  
You'll surely need to adapt it to your specific system...


I think it's directly possible to put a function at a precise address, but 
there should be a way of doing indirectly.

Using the SYSTEM package, you can directly poke into the computer's memory.

One solution is to make put assembler code at the address you want.  This 
assembler should them jump to your ADA procedure or function.

For a Motorola processor, you would have something like:
This code does only two thing.  Call your procedure and then return to the 
caller.
  JSR your_proc    // jump sub-routine
  RTS              // return

Once assembled this generated perhaps 8 bytes of binary code.

Then you would need to store the binary code somewhere at the adress you 
want.  You can use the SYSTEM package for this.

Then you've the procedure to call


procedure  foo( i: integer; j: integer; k: integer ) is
begin
  TEXT_IO.PUT_LINE( "Hello, world !" );
  TEXT_IO.PUT( "i = " );
  TEXT_IO.PUT( i );
  TEXT_IO.NEWLINE;
  TEXT_IO.PUT( "j = " );
  TEXT_IO.PUT( j );
  TEXT_IO.NEWLINE;
  TEXT_IO.PUT( "k = " );
  TEXT_IO.PUT( k );
  TEXT_IO.NEWLINE;
end;


Now you would need to define the constants to poke into the memory.  
(Obviously, you must be sure that this memory is not used for something 
else !)


  type byte is mod 256;

  JSR_code1 : constant byte := 16#xx#   // xx depends of CPU...
  JSR_code2 : constant byte := 16#xx#
  RTS_code1 : constant byte := 16#xx#
  RTS_code2 : constant byte := 16#xx#


  PROC_ADR : constant := 16#DEADBEEF#  // the absolute where your proc must 
reside

  Writing into the memory...  Use the SYSTEM package.

  SYSTEM. ... ( JSR_code1, PROC_ADR + 0 );
  SYSTEM. ... ( JSR_code2, PROC_ADR + 1 );
  SYSTEM. ... ( foo'Address, PROC_ADR + 2 );
  SYSTEM. ... ( RTS_code1, PROC_ADR + 4 );
  SYSTEM. ... ( RTS_code2, PROC_ADR + 5 );


Now the problem remaining is that a the JSR call adds the return address on 
the stack.  Depending of how your ADA compiler passes the parameter, you 
may need to ignore the first parameter.  The ignored parameter should have 
the same size as the address.  (I've assumed 32 bits so far).


So your procedure becomes:

procedure  foo( DUMMY_PARAM: integer; i: integer; j: integer; k: integer ) 
is
begin
...
end;

or perhaps:

procedure  foo( i: integer; j: integer; k: integer; DUMMY_PARAM: integer ) 
is
begin
...
end;

Depending in which order the parameters are put on the stack.


You should perhaps test with a procedure without parameter first...





  parent reply	other threads:[~2004-03-04  5:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-02-25 21:59 function at specific address Szymon Guz
2004-02-26 13:59 ` Robert I. Eachus
2004-02-26 14:23 ` Frank J. Lhota
     [not found] ` <m23c8ydm5v.fsf@jvdsys.demon.nl>
2004-03-04  5:18   ` pburnand0-news [this message]
2004-03-04  9:03     ` Preben Randhol
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox