I'm trying to use the inline asm directive of gcc/g++ (I have to say I've been using the Intel syntax on MSVC previously and that was a breeze). I'm playing around with double values and the following my_func2 seems to crash after execution:
#include <iostream>
void my_func(const double *in, double *out) {
asm("mov %0, %%r8" : : "r"(in));
asm("movupd (%%r8), %%xmm0" :);
asm("movupd (%%r8), %%xmm1" :);
asm("addpd %%xmm1, %%xmm0" :);
asm("movupd %%xmm0, (%0)" : : "r"(out) : "%r8", "%xmm0", "%xmm1");
}
double my_func2(const double *in) {
double ret = 0.0;
asm("mov %0, %%r8" : : "r"(in));
asm("movupd (%%r8), %%xmm0" :);
asm("movupd (%%r8), %%xmm1" :);
asm("addpd %%xmm1, %%xmm0" :);
asm("movupd %%xmm0, %0" : "=m"(ret) : : "memory", "%r8", "%xmm0", "%xmm1");
return ret;
}
int main(int argc, char *argv[]) {
const double a = 1.0;
double b = 0.0;
my_func(&a, &b);
std::cout << "b:" << b << std::endl;
b = my_func2(&a);
std::cout << "b:" << b << std::endl;
}
The error I get is specifically (when I'm running with gdb):
Program received signal SIGBUS, Bus error.
0x00000000004008e1 in main (argc=<error reading variable: Cannot access memory at address 0x400fffffffffffec>,
argv=<error reading variable: Cannot access memory at address 0x400fffffffffffe0>) at asm_test.cpp:28
28 b = my_func2(&a);
What am I doing wrong? In the last line of my_func2 I've specified that memory is clobbered too, I don't understand...
Where can I find a good guide how to use the infamous AT&T syntax?
I compile with: g++ -g -o asm_test asm_test.cpp, g++ version g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 on Ubuntu Linux scv 3.2.0-48-generic #74-Ubuntu SMP Thu Jun 6 19:43:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux.
I've found http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html and http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html , is there something more you would recommend?
Thanks,
Ema