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