A simple understanding of C language Embedded Assembly, Embedded Assembly
Recently I have looked at the implementation of the spin lock. The main implementation of the loop lock of the self-selected lock is similar to the following. This implementation uses an Embedded Assembly (from the sanos kernel, the source code has two implementations, there is no problem with intel assembly in one place, and the embedded assembly syntax in the other. The source code is cmpxchgl % 2, % 0, which is incorrect. It should be cmpxchgl % 0, % 2)
Embedded Assembly has a fixed format, as shown below:
asm ( assembler
Template/* Assembly statement */
: Output operands/* output */
: Input operands/* input */
: list of clobbered registers
);
Cmpxchgl is described as follows: Compares the value in the AL, AX, EAX, or RAX register with the first operand (destination operand ). if the twovalues are equal, the second operand (source operand) is loaded into the destination operand. otherwise, thedestination operand is loaded into the AL, AX, EAX or RAX register. RAX register is available only in 64-bit mode. (* Accumulator = AL, AX, EAX, or RAX depending on whether a byte, word, doubleword, or quadword comparison is being stored med *)
TEMP shortdest
IF accumulator = TEMP
THEN
ZF limit 1;
DEST parse SRC;
ELSE
ZF limit 0;
Accumulator implements TEMP;
DEST revoke TEMP;
FI;
Int atomic_compare_and_exchange (int * dest, int exchange, int comperand) {int old = comperand; if (comperand = * dest) {* dest = exchange ;} else {old = * dest;} return old ;}
Reference: http://blog.chinaunix.net/uid-23955580-id-2945814.html