spark.z wrote:
Eclipse + gcc-4.4.1, compiled the program with optimization flag -O2 I wrote a inline function with inlined assembly void pincrement(int* target) { __asm__ __volatile__ ("pushl %eax"); __asm__ __volatile__ ("lock ; incl (%%eax)" ::"a"(target)); __asm__ __volatile__ ("popl %eax"); }
void pincrement(int* target) { __asm__ __volatile__ ("pushl %%eax"); __asm__ __volatile__ ("lock ; incl (%%eax)" ::"a"(target)); __asm__ __volatile__ ("popl %%eax":::"eax"); } seems to do the trick. (the third asm statement adds eax in the clobber list) (and I also add to write %%eax instead of your %eax). You should tell gcc what your asm statement modifies. Here, the pop modifies eax. But even the first asm statement modifies the stack and we don't tell gcc, but does that matter? I don't know. And the second also modifies *target, maybe we should tell gcc too here. Anyway, you've got the idea. Maybe someone else will have a better answer. And you should read the gcc documentation about "Extended Asm."