A simple understanding of C language Embedded Assembly, Embedded Assembly

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.