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=-0.3 required=3.0 tests=BAYES_00,SCC_BODY_URI_ONLY, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Received: by 2002:a05:622a:394:b0:304:eacb:2c69 with SMTP id j20-20020a05622a039400b00304eacb2c69mr26587932qtx.439.1654950754313; Sat, 11 Jun 2022 05:32:34 -0700 (PDT) X-Received: by 2002:a81:1d43:0:b0:313:a537:50ed with SMTP id d64-20020a811d43000000b00313a53750edmr13121180ywd.335.1654950754068; Sat, 11 Jun 2022 05:32:34 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 11 Jun 2022 05:32:33 -0700 (PDT) In-Reply-To: Injection-Info: google-groups.googlegroups.com; posting-host=73.205.150.94; posting-account=Ru7E4QoAAAC_HiQ2D8LjZ7rh1mbTNcVn NNTP-Posting-Host: 73.205.150.94 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <7e9b7ba2-2304-4ffa-9bd3-d69a712cd022n@googlegroups.com> Subject: Re: Using pointers with inline assembly in Ada From: NiGHTS Injection-Date: Sat, 11 Jun 2022 12:32:34 +0000 Content-Type: text/plain; charset="UTF-8" X-Received-Bytes: 2870 Xref: reader02.eternal-september.org comp.lang.ada:63972 List-Id: On Saturday, June 11, 2022 at 8:28:28 AM UTC-4, Simon Wright wrote: > 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; I haven't tested this yet but the solution makes sense to me. Thank you for your help!