From: NiGHTS <nights@unku.us>
Subject: Re: Using pointers with inline assembly in Ada
Date: Sat, 11 Jun 2022 05:32:33 -0700 (PDT) [thread overview]
Message-ID: <7e9b7ba2-2304-4ffa-9bd3-d69a712cd022n@googlegroups.com> (raw)
In-Reply-To: <lyv8t7z8nr.fsf@pushface.org>
On Saturday, June 11, 2022 at 8:28:28 AM UTC-4, Simon Wright wrote:
> NiGHTS <nig...@unku.us> writes:
>
> > 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;
> I got an access error as well (macOS, GCC 12.1.0, 64 bits).
>
> Eventually, it turned out that the problem was that eax is a 32-bit
> register. (the compiler used rdx, and the assembler told me that
> movl %rdx, %eax wasn't allowed).
>
> This is my working code; Flags is volatile, because otherwise the
> compiler doesn't realise (at -O2) that Flags (0) has been touched.
>
> ALso, note the Inputs line!
>
> ====
> with System.Machine_Code; use System.Machine_Code;
> with Interfaces; use Interfaces;
> with Ada.Text_IO; use Ada.Text_IO;
> procedure Nights is
> type Ff is array (0 .. 10) of Unsigned_32;
> Flags : aliased Ff := (others => 0) with Volatile;
> begin
> Asm (
> "movq %0, %%rax" & ASCII.LF & ASCII.HT
> & "movl $1, (%%rax)",
> Inputs => System.Address'Asm_Input ("g", Flags (Flags'First)'Address),
> Clobber => "rax",
> Volatile => True
> );
> Put_Line ("Output:" & Flags(0)'Img);
> end Nights;
I haven't tested this yet but the solution makes sense to me. Thank you for your help!
next prev parent reply other threads:[~2022-06-11 12:32 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 [this message]
2022-06-13 20:33 ` Gabriele Galeotti
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox