Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Processor: Intel Xeon E3-1230 v2
OS: Win 7 64bit
Compiler: GCC

The code is shown below:
C++
// sample.c
#include <stdio.h>

int atomic_add_int(volatile int * address, int value){
	
	asm volatile ("lock xadd %0, %1" : <-- line 11
		"+r" (value) :
		"+m" (*address) :
		"memory");   // <-- line 14
	
	return value;
}

int atomic_inc_int(int * address){
	
	asm volatile("lock inc %0": : <-- line 21
		"+m" (*address):
		"memory");  // <-- line 23
	
	return (*address);
}

int main(){
	
	
}


When I compile the code(>gcc -c sample.c), the errors occur:
sample.c: In function 'atomic_add_int':
sample.c:14:3: error: input operand constraint contains '+'
sample.c: In function 'atomic_inc_int':
sample.c:23:3: error: input operand constraint contains '+'
sample.c:21:2: error: input operand constraint contains '+'
sample.c: In function 'atomic_add_int':
sample.c:11:2: error: input operand constraint contains '+'

I wrote the code as the book(Multicore Application Programming) tell me to, but why those error occur? Can any one help me? Thanks.
Posted

This is because different assemblers for different instruction-set architectures have different syntax. Consequently, the same goes for different C++ compilers for different instruction-set architectures, when it comes to inline assembly. I don't know what was meant by the author of the book you mention, but the syntax you show in your example looks quite foreign to me. At the same time, GCC C++ example for x86, x86-64 or IE-64 look very familiar. See, for example:
http://www.advancedlinuxprogramming.com/alp-folder/alp-ch09-inline-asm.pdf[^].

—SA
 
Share this answer
 
Yo have to tell what compiler you are using. As Sergey tells it depends on the assembler.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900