Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling assembly in GCC?

#include <stdlib.h>

static inline uint
xchg(volatile unsigned int *addr, unsigned int newval)
{
   uint result;
   asm volatile("lock; xchgl %0, %1" : "+m" (*addr), "=a" (result) : "1" (newval) : "cc");

return result;    
}

Can some one tell me what this code does exactly? I mean I have an idea or the parts of this command. "1" newval is the input, "=a" is to flush out its previous value and update it. "m" is for the memory operation but I am confused about the functionality of this function. What does the "+m" sign do? Does this function do something like m=a; m = newval; return a

like image 394
rbr200 Avatar asked Mar 22 '10 03:03

rbr200


1 Answers

= and + are constraint modifiers.

http://gcc.gnu.org/onlinedocs/gcc/Modifiers.html#Modifiers

`=' Means that this operand is write-only for this instruction: the previous value is discarded and replaced by output data.

`+' Means that this operand is both read and written by the instruction.

Basic constrains are here

http://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html#Simple-Constraints

m A memory operand is allowed, with any kind of address that the machine supports in general.

..1.. An operand that matches the specified operand number is allowed. If a digit is used together with letters within the same alternative, the digit should come last.

'a' is i386 specific

http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints

a The a (eax) register.

like image 184
osgx Avatar answered Oct 12 '22 21:10

osgx