Passing array variable address as Input Constraint to Inline Assembly block

Daniel Yek dyek@real.com
Thu Mar 20 11:48:00 GMT 2008


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.

-------------- next part --------------
// 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;
}



-------------- next part --------------
// 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;
}



More information about the Gcc-help mailing list