user2462909
user2462909

Reputation: 1

add inline asm into C macro

I'm using mingw 4.7.2.

Could someone please tell me what's wrong in this MACRO All I want to do is a simple asm macro which add two int and puts the result into result variable

#define add(result,a,b) \
   __asm__ __volatile__( \
"           movl %1, %%eax \n" \
"           addl %2, %%eax \n" \
"           movl %%eax, %0 \n" \
"           :"=r"(result) \
            :"r"(a),"r"(b) )

The compiler says error: missing terminating " character

Thanks in advance

Upvotes: 2

Views: 1746

Answers (2)

Brett Hale
Brett Hale

Reputation: 22348

Here's a much more flexible implementation:

__asm__ ("addl %2, %k0" : "=r" (result) : "%0" (a), "g" (b) : "cc")

Furthermore, the __volatile__ keyword is completely unnecessary in this context.

Upvotes: 4

BoBTFish
BoBTFish

Reputation: 19757

#define add(result,a,b) \
__asm__ __volatile__( \
"           movl %1, %%eax \n" \
"           addl %2, %%eax \n" \
"           movl %%eax, %0 \n" \
"           :"=r"(result) \  // <---- This line starts with a " for no reason.
            :"r"(a),"r"(b) )

The line marked begins with a " which then offsets all the other strings to the end of the macro. Just get rid of this.

I.e. your last two lines are actually:

"           :"=r"(result) :"r"(a),"r"(b) )
<----str----->  <----str---> <----> <-----.... No end

When you really meant

            :"=r"(result) :"r"(a),"r"(b) )
             <-->          <->    <->

Upvotes: 4

Related Questions