Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand this extend assembly inline code in C

here is a C function that used extend assembly code in it:

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

I read this codeproject link for learn how to use Extended Assembly in C code but I confused at this code. I don't understand something in this code :
1) where is (newval) used as input in assembly code ? (%0 is refer to (*addr) and %1 is refer to (result) right ? then "1" (newval) never used in the code (we never see %2 in code). or I am wrong ? )
2) what is "1" in "1" (newval) in input operands ?
3) what is "+m" sign means in "+m" (*addr) ?

like image 276
mojibuntu Avatar asked Nov 10 '22 15:11

mojibuntu


1 Answers

All the answers of your questions about GCC asm can be from from 6.41 Assembler Instructions with C Expression Operands and 6.42 Constraints for asm Operands.

like image 156
Lee Duhem Avatar answered Nov 14 '22 22:11

Lee Duhem