comp.lang.ada
 help / color / mirror / Atom feed
From: Gabriele Galeotti <gabriele.galeotti.xyz@gmail.com>
Subject: Re: Using pointers with inline assembly in Ada
Date: Mon, 13 Jun 2022 13:33:14 -0700 (PDT)	[thread overview]
Message-ID: <01af1f18-5dd7-46d9-b082-36ae5b3c5811n@googlegroups.com> (raw)
In-Reply-To: <db04b8e7-ba96-4f4a-afb1-575e446ace64n@googlegroups.com>

On Thursday, June 9, 2022 at 11:30:21 PM UTC+2, NiGHTS wrote:
> I would like to write an inline assembly code in Ada that simply writes a constant to a specific element of an array of unsigned values. 
> 
> I'm shooting in the dark here because there are absolutely no references on this subject, to my surprise. 
> 
> This was my first experiment which produces a memory access error. In this early version I'm just trying to write to the first element. 
> 
> declare 
> type ff is array (0 .. 10) of Unsigned_32; 
> pragma Pack(ff); 
> Flags : aliased ff := (others => 0); 
> Flag_Address : System.Address := Flags'Address; 
> begin 
> Asm ( "movl %0, %%eax" & 
> "movl $1, (%%eax)" , 
> Inputs => System.Address'Asm_Input ("g", Flag_Address), 
> Clobber => "eax", 
> Volatile => true 
> ); 
> Put_Line ("Output:" & Flags(0)'Img); 
> end; 
> 
> Any help would be greatly appreciated!

Your code has no huge problems, apart generating a lot of warnings and being not decorated well.

Just make the array Volatile.

Otherwise the compiler is not able to understand that you're modifying the array,
takes that as an invariant, and prints a "0" instead of "1", optimizing everything
out away:

 type ff is array (0 .. 10) of Unsigned_32; with
   Pack => True,
  Volatile => True;

There should be no problems for 64-bit machines because the components are anyway accessed on 32-bit boundaries,
but you have to adjust register sizes, EAX cannot contain an address in x86 64-bit mode (probably you have to use RAX).

G

      parent reply	other threads:[~2022-06-13 20:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-09 21:30 Using pointers with inline assembly in Ada NiGHTS
2022-06-10  5:24 ` Rod Kay
2022-06-10 11:16   ` Luke A. Guest
2022-06-10 12:26     ` NiGHTS
2022-06-10 12:16   ` NiGHTS
2022-06-10 13:19     ` Jeffrey R.Carter
     [not found]   ` <nnd$727405a5$1c8a5b81@aedbf58048bf777d>
2022-06-10 12:23     ` NiGHTS
2022-06-11  1:43   ` Rod Kay
2022-06-10 13:39 ` Jeffrey R.Carter
2022-06-11  1:51   ` NiGHTS
2022-06-11 12:28 ` Simon Wright
2022-06-11 12:32   ` NiGHTS
2022-06-13 20:33 ` Gabriele Galeotti [this message]
replies disabled

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