public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Daniel Yek <dyek@real.com>
To: gcc-help@gcc.gnu.org
Subject: Passing array variable as Input Constraint to lea Inline Assembly  instruction; works on i386, not on AMD64
Date: Thu, 20 Mar 2008 21:25:00 -0000	[thread overview]
Message-ID: <47E2D629.9060704@real.com> (raw)
In-Reply-To: <47E24EE2.2030607@real.com>

[Updated email Subject: to better reflect the issue.]

Daniel Yek wrote:
> Hi,
>
> I'm trying to find out the right way to pass an array variable as 
> address into an Inline Assembly block as Input Constraint.

Let me add that I need/want to understand how to make lea instruction 
works in Inline Assembly to facilitate porting (and it works for i386, 
but I wonder why it isn't working for x86_64).

I also want to make sure that I'm using the right Constraint.

One more comment inline...
>
> Test Program:
> #include <stdio.h>
>
> int array[3] = {111, 222, 333};
>
> int main(void)
> {
>    int out = 0;
>    asm(
>        ".intel_syntax noprefix           \n\t"
>        "lea         eax, %[array]        \n\t"
>        "mov         eax,[eax]            \n\t"
>        "inc         eax                  \n\t"
>        ".intel_syntax prefix             \n\t"
>      : "=a" (out)
>      :   [array]  "m"  (*array)
>    );
>
>    printf("out = %i\n", out);
>
>    return 0;
> }
>
>
> Compiling on i386 machine, it worked:
> COMMAND LINE:
> gcc -fPIC -DPIC -masm=intel prog_i386 prog_i386.c
>
> ASSEMBLY:
>    mov %eax, DWORD PTR array@GOT[%ebx]
> #APP
>    .intel_syntax noprefix
>    lea         eax, DWORD PTR [%eax]
>
>
> Compiling on x86_64 machine, it didn't work:
> COMMAND LINE:
> gcc -fPIC -DPIC  -masm=intel -o prog_x86_64 prog_x86_64.c
>
>
> ERROR:
> /tmp/cc7z5OHz.s: Assembler messages:
> /tmp/cc7z5OHz.s:27: Error: invalid operand for 'mov' ('(' unexpected)
> /tmp/cc7z5OHz.s:38: Error: invalid operand for 'lea' ('(' unexpected)
>
> ASSEMBLY:
>    mov %rax, QWORD PTR array@GOTPCREL(%rip)

It now appears to me that this is simply a case of a GCC bug in 
producing -masm=intel AMD64 assembly. If I fix the assembly output up by 
replacing () with [], the resulting assembly would actually assemble and 
execute successfully.

-- 
Daniel Yek.


> #APP
>    .intel_syntax noprefix
>    lea         rax, DWORD PTR [%rax]
>
>
> I am hoping for a solution that is good for both compiling with -fPIC 
> or not, if possible.
>
> What is the right way to pass an array variable into Inline Assembly?
>
> http://gcc.gnu.org/onlinedocs/gccint/Simple-Constraints.html#Simple-Constraints 
>
> Is there any other constraint that I should be using instead?
>
> Thanks for your advice!
>

      reply	other threads:[~2008-03-20 21:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-20 11:48 Passing array variable address as Input Constraint to Inline Assembly block Daniel Yek
2008-03-20 21:25 ` Daniel Yek [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47E2D629.9060704@real.com \
    --to=dyek@real.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).