AdaPower Logged in as Guest
Ada Tools and Resources

Ada 95 Reference Manual
Ada Source Code Treasury
Bindings and Packages

Join >
Home >
Articles >
Getting Started >
Ada FAQ >
Source Treasury >
Books & Tutorials >
Packages for Reuse >
Latest Additions >
Ada Projects >
Press Releases >
Ada Audio / Video >
Home Pages >
Links >
Contact >
About >
Login >
Executing machine code in an array (David Brown)

This conversion is making a few assumption about how the compiler
represents procedure (or function) access types, and system address.
I suggest finding out from your vendor how these are implemented.

-- Casting example to a special procedure pointer.

with Ada.Unchecked_Conversion;
with System;

procedure Call is

   type Grunt_Call is access procedure (Arg : Integer);

   type Byte is mod 2**8;
   for Byte'Size use 8;

   type Byte_Array is array (Positive range <>) of Byte;
   pragma Pack (Byte_Array);

   Sample_Code : Byte_Array (1 .. 4);

   function "+" is
      new Ada.Unchecked_Conversion (System.Address, Grunt_Call);

   Perform_Grunt : Grunt_Call := "+"(Sample_Code(Sample_Code'First)'Address);

   -- This is an x86 return instruction.
   Sample_Code (1) := 16#C3#;

   -- Processors with separate instruction and data caches may have to
   -- perform a system specific operation to make these caches coherent.

   -- Now we can execute.
   Perform_Grunt.all (10);
end Call;

(c) 1998-2004 All Rights Reserved David Botton