asm ---expressing asm language
volatile----telling complier not to optimize my asm coding
lock-------asm instruction, expressing synchronous operation
xchg-------asm insturction, expressing to exchange the values in *ptr and val varible
%0---------expressing *ptr variable
%1---------expressing val variable
"r"(val)--------expressing to input val variable into common register in system
"m"(*ptr)------expressing that the data pointed by the pointer of ptr varable are stored in memory
"=r"(val) and "=m"(*ptr)----expressing that the data exchanged will be store val and *ptr variable ,
"m" expresses to use memory to store the data of *ptr variable
"r" expresssed to use common register to store the data of val variable
1)
sorry, I take a mistake
%0---------expressing ptr variable
%1---------expressing val variable
传入的是ptr,%0为什么不表示ptr
2)
when the assembler insturction has a read-write operand, you must logically split its function into two separate operands, one input operand and one write-only output operand.The connection between them is expressed by constrains which say they need to be in the same location when the instruction executes.Various optimizations or reloading could cause operand 0 to be in different register; GNU CC knows no reason not to do so. For example, the compiler might find a copy if the value of "val" in one register and use it for input operand , but generate the output operand in a different register(copying it afterward to "val"'s own address). Of cource, since the register for input operand is not event mentioned in the assembler code, the result will not work, but GNU CC cann't tell that.
sample
the following would not work:
asm volatile ("lock; xchg %0,%1" : "=r" (val), "=m" (*ptr) : "m" (*ptr) , "r" (val) );
3)
asm's structure in gcc:
asm ( assembler template
: output operands (optional)
: input operands (optional)
: list of clobbered registers
(optional)
);