2

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

4
  • 1
    I guess here *" :"=r"(result) * something is wrong with number of quotes Commented Jun 7, 2013 at 9:21
  • 2
    You should also marke eax in your clobber (or use the fact that %1 is already in a register, so you don't actually need to move it anywhere to add it to another register) Commented Jun 7, 2013 at 9:28
  • thanks sharptooth , i forgot to check the syntax , but really gcc inline asm is truly painful :( Commented Jun 7, 2013 at 9:31
  • 1
    Since it's tagged C++, everything is wrong with this macro, hrhrhr. Commented Jun 7, 2013 at 10:09

2 Answers 2

4
#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) ) <--> <-> <-> 
Sign up to request clarification or add additional context in comments.

1 Comment

This still fails to inform the compiler that %eax is clobbered, nor does it prevent %0, %1 or %2 being substituted with %eax.
4

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.

1 Comment

Ah, me commenting before reading the entire thread ;-) This one is much better than the original, more compact, leaving more optimization possibilities to the compiler.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.