From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!aioe.org!Nk+3gcWp7UG8G05s3m3vmg.user.46.165.242.75.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Using pointers with inline assembly in Ada Date: Sat, 11 Jun 2022 13:28:24 +0100 Organization: Aioe.org NNTP Server Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: gioia.aioe.org; logging-data="46761"; posting-host="Nk+3gcWp7UG8G05s3m3vmg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (darwin) X-Notice: Filtered by postfilter v. 0.9.2 Cancel-Lock: sha1:BfUgrSy+l0z3+siSkPdrm8EWmQw= Xref: reader02.eternal-september.org comp.lang.ada:63971 List-Id: NiGHTS 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;