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.9 required=3.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Received: by 2002:ae9:e8c4:0:b0:6a6:ab86:47ac with SMTP id a187-20020ae9e8c4000000b006a6ab8647acmr1592338qkg.48.1655152395129; Mon, 13 Jun 2022 13:33:15 -0700 (PDT) X-Received: by 2002:a0d:eacc:0:b0:30c:30cc:e161 with SMTP id t195-20020a0deacc000000b0030c30cce161mr1646413ywe.375.1655152394825; Mon, 13 Jun 2022 13:33:14 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Mon, 13 Jun 2022 13:33:14 -0700 (PDT) In-Reply-To: Injection-Info: google-groups.googlegroups.com; posting-host=82.57.69.237; posting-account=JRF_-woAAABYlsAtkCl_CUxBuQy2SsaQ NNTP-Posting-Host: 82.57.69.237 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <01af1f18-5dd7-46d9-b082-36ae5b3c5811n@googlegroups.com> Subject: Re: Using pointers with inline assembly in Ada From: Gabriele Galeotti Injection-Date: Mon, 13 Jun 2022 20:33:15 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader02.eternal-september.org comp.lang.ada:63983 List-Id: 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