public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Passing array variable address as Input Constraint to Inline Assembly  block
@ 2008-03-20 11:48 Daniel Yek
  2008-03-20 21:25 ` Passing array variable as Input Constraint to lea Inline Assembly instruction; works on i386, not on AMD64 Daniel Yek
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Yek @ 2008-03-20 11:48 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1597 bytes --]

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.

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)
#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!

-- 
Daniel Yek.


[-- Attachment #2: prog_i386[1].c --]
[-- Type: text/plain, Size: 542 bytes --]

// gcc -fPIC -DPIC -masm=intel prog_i386 prog_i386.c
// gcc -masm=intel prog_i386 prog_i386.c

#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;
}




[-- Attachment #3: prog_x86_64[1].c --]
[-- Type: text/plain, Size: 556 bytes --]

// gcc -fPIC -DPIC  -masm=intel -o prog_x86_64 prog_x86_64.c
// gcc -masm=intel -o prog_x86_64 prog_x86_64.c

#include <stdio.h>

int array[3] = {111, 222, 333};

int main(void)
{
    int out = 999;
    asm(
        ".intel_syntax noprefix           \n\t"
        "lea         rax, %[array]        \n\t"
        "mov         rax,[rax]            \n\t"
        "inc         rax                  \n\t"
        ".intel_syntax prefix             \n\t"

      : "=a" (out)
      : [array]  "m"  (*array)
    );

    printf("out = %i\n", out);

    return 0;
}


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Passing array variable as Input Constraint to lea Inline Assembly  instruction; works on i386, not on AMD64
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Yek @ 2008-03-20 21:25 UTC (permalink / raw)
  To: gcc-help

[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!
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-03-20 21:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-20 11:48 Passing array variable address as Input Constraint to Inline Assembly block Daniel Yek
2008-03-20 21:25 ` Passing array variable as Input Constraint to lea Inline Assembly instruction; works on i386, not on AMD64 Daniel Yek

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).