comp.lang.ada
 help / color / mirror / Atom feed
From: "news.oxy.com" <Vladimir_Olensky@oxy.com>
Subject: Re: Assembler in Ada?
Date: 1999/01/22
Date: 1999-01-22T00:00:00+00:00	[thread overview]
Message-ID: <78a32f$dbr$1@remarQ.com> (raw)
In-Reply-To: 369C5E08.69727537@mbox5.swipnet.se


Thomas Larsson wrote in message <369C5E08.69727537@mbox5.swipnet.se>...
>I would like to write some assembler into my Ada program (to change
>video mode, use mouse etc).
>
>How do I do that? (I have tried to figured out how, but I can't)
>Can I do it in a way similar to assembler in C or Pascal?
>\x18eg: _asm {
> mov cx,100
> LP: loop LP
> }
>
>I would appreciate a small example program.
>
>Thanks for your help
>Thomas
>
>PS. I do the programming in Win98, not UNIX


Below there are several examples of using Assembler with GNAT 3.10p1
(Windows NT 4.0).
Hope that they will be of some help for those who found that it is difficult
to understand how to use Assembler with GNAT.

I enjoy playing with GNAT 3.10p1 and find that it is really good.
But unfortunately I should admit the poor quality of GNAT documentation
regarding using of the assembler with it.  ACT people perfectly well know
one generic rule that should be followed to make any software system work:
ALL THE REFERENCIES FROM THE SOFTWARE MODULES SHOULD BE RESOLVED WITHIN THE
GIVEN SET OF MODULES THAT MAKE UP
THE SYSTEM. One won't be able to build the system if that rule is not
obeyed.
To my great surprise they forgot to apply this rule to the documentation so
it does not work.ALL THE REFERENCIES FROM ANY PIECE OF THE SYSTEM
DOCUMENTATION SHOULD BE RESOLVED WITHIN THE SET OF MANUALS THAT COME WITH
THE GIVEN SYSTEM. Otherwise the documentation does not work.

If you open GNAT ref. Manual at the Machine Code Insertion you will find
that just from the second paragraph ACT suggest you to go far away - to the
"Using and Porting GNU CC" which is not included in the documentation that
comes with GNAT. "Happy" GNAT users! (especially new ones). They want to
know how to use Assembler with GNAT but they are suggested to learn how to
use and port GCC. Moreover they are even directed where to find this manual.
Just wonderful!
There also no info regarding control over passing parameters, names of the
registers, how the template string is
passed to the GCC Assembler e.t.c.

Another thing that they forgot to do is to supply several small examples,
which can help to understand the rules. As a professor Rober Devar should
know that very complex and difficult things could be easily explained to
students using well designed simple examples.

Hope that ACT will be able to improve this in the GNAT 3.11p documentation
and close this issue.
DOCUMENTATION SHOULD BE SELF-CONTAINED!

Hope that my examples will be of some help.

Regards to all,
Vladimir Olensky (vladimir_olensky@yahoo.com)

 (Vladimir_Olensky@oxy.com)
Telecommunication specialist,
Occidental C.I.S. Service, Inc. ( www.oxy.com )
Moscow,
Russia.


**********************************************
There are two ways using Assembler:
1. Inline Assembler code
2. External Assembler code (xxx.S file) which is compiled by tha same GCC

Here I will give only two small examples in order not to overload
this posting too much. Later I can give some more
if there will be new requests.

*************************************************
--------------------------------
-- Inline assembler code example.
-- Author: Vladimir Olensky
---------------------------------
WITH Ada.Text_IO; USE Ada.Text_IO;
WITH Ada.Integer_Text_IO; USE Ada.Integer_Text_IO;

-- first you need to use System.Machine_Code package
WITH System.Machine_Code; USE System.Machine_Code;

PROCEDURE Asm_Tst1 IS

   -- new line - Unix stile - LF
   --            CRLF is causing GCC assembler to crash !!!
   nl: CONSTANT Character  := Character'Val(10);

   A:INTEGER:=15;
   B:INTEGER:=20;
   C:INTEGER:=0;

BEGIN
   Put_Line(" Inline assembler test");
   New_Line;
   -------------------------------------------
    Asm (
             "movl  %1,    %%eax"  & nl &    -- note nl here
         "    addl  %2,    %%eax"  & nl &    -- to construct proper string
template.
         "    movl  %%eax, %0",              -- Without that it won't work
         INTEGER'Asm_Output("=g", C),   -- Asm_Output("=g", C) compiler
choose output register for C
                                       -- Asm_Input( "g", A) -compiler
choose register for for A
        (INTEGER'Asm_Input( "g", A),INTEGER'Asm_Input("g", B)),
         "eax",  -- register eax will be spoiled by my code
          False  -- compiler, do not optimise my code !
          );
   --------------------------------------
   Put(C);
END Asm_Tst1;

-- Asm_tst1 output = 35
-------------
extras from assembler listing with my comments:
 ....
/APP    - my asm inline code
    movl  $15,    %eax    / move A to ax
    addl  $20,    %eax    / add B to A
    movl  %eax, %edx   / save result to C ( compiler have chosen edx)
/NO_APP
        / call to Put(C)
 movl _ada__integer_text_io__default_base,%eax
 pushl %eax
 movl _ada__integer_text_io__default_width,%eax
 pushl %eax
 pushl %edx     / push C
 call _ada__integer_text_io__put$2
 ....

*********************************************
another example with manual register control

--------------------------------
-- Inline assembler code example.
-- Author: Vladimir Olensky
---------------------------------

WITH Ada.Text_IO; USE Ada.Text_IO;
WITH Ada.Integer_Text_IO; USE Ada.Integer_Text_IO;

-- first you need to use System.Machine_Code package
WITH System.Machine_Code; USE System.Machine_Code;

PROCEDURE Asm_Tst1 IS

   -- new line - Unix stile --LF
   --            CRLF is causing GCC asembler to crash !!!
   nl: CONSTANT Character  := Character'Val(10);

   A:INTEGER:=15;
   B:INTEGER:=20;
   C:INTEGER:=0;

BEGIN
   Put_Line(" Inline assembler test");
   New_Line;
   -------------------------------------------
    Asm (
      -- we add two registers where we directed compiler to put A and B
         "    addl  %%ebx,    %%eax",
        INTEGER'Asm_Output("=a", C),   -- Asm_Output("=a", C) compiler, use
eax to output C
                                       -- Asm_Input( "a", A) -compiler, use
eax for for A
                                       -- Asm_Input( "b", B) -compiler, use
ebx for for B
        (INTEGER'Asm_Input( "a", A),INTEGER'Asm_Input("b", B)),
         "",  -- do not need to use anything, compiler already knows
everything
         False  -- compiler, do not optimise my code !
          );
   --------------------------------------

   Put(C);
END Asm_Tst1;
-- Asm_tst1 output = 35
----------
extras from the assembler listing with my comments:
 ...
 call _ada__text_io__new_line$2  / call to New_Line
 movl $15,%eax   / INTEGER'Asm_Input( "a", A)
 movl $20,%ebx   / INTEGER'Asm_Input("b", B)
/APP
     addl  %ebx,    %eax         / My inline assembler code
/NO_APP
 / call to the Put(C)
 movl %eax,%edx                 / compiler moves C to edx
 movl _ada__integer_text_io__default_base,%eax  / as eax will used for
defauly dase
 pushl %eax
 movl _ada__integer_text_io__default_width,%ebx
 pushl %ebx
 pushl %edx                                            / push C
 call _ada__integer_text_io__put$2
  ...
********************************
From the last example you can see that it is necessary to be very careful
when
manually controlling passing varaibles to the registers. It can be less
effective.
Just compare two examples and you will see that the last one has one
instruction more.
There are a lot of pitfalls here. Be careful!







  parent reply	other threads:[~1999-01-22  0:00 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-01-13  0:00 Assembler in Ada? Thomas Larsson
1999-01-13  0:00 ` Matthew Heaney
1999-01-14  0:00   ` Bill Ghrist
     [not found]     ` <369ED5E0.DB29E68C@usc.edu>
1999-01-15  0:00       ` Will this help? (Re: " Bill Ghrist
1999-01-22  0:00 ` news.oxy.com [this message]
1999-01-24  0:00   ` dewar
1999-01-25  0:00     ` news.oxy.com
1999-01-25  0:00       ` robert_dewar
1999-01-26  0:00         ` news.oxy.com
1999-01-26  0:00           ` Larry Kilgallen
1999-01-27  0:00             ` dewar
1999-01-27  0:00           ` robert_dewar
1999-01-25  0:00       ` robert_dewar
1999-01-24  0:00   ` dewar
1999-01-25  0:00     ` news.oxy.com
1999-01-25  0:00       ` robert_dewar
1999-01-26  0:00         ` news.oxy.com
1999-01-27  0:00           ` Samuel Tardieu
1999-01-27  0:00             ` news.oxy.com
1999-01-27  0:00               ` Marin David Condic
1999-01-28  0:00                 ` news.oxy.com
1999-01-27  0:00           ` dewar
1999-01-25  0:00       ` Richard Kenner
1999-01-25  0:00         ` news.oxy.com
replies disabled

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