Inline assembly, c inline assembly

Source: Internet
Author: User

Inline assembly, c inline assembly

Link: http://www.orlion.ga/776/

The program written in C may be less efficient than assembly, and some platform-related commands must be handwritten. For example, x86 is a port I/O, and C language does not have this concept, therefore, the in/out command must be compiled.

Gcc provides an extended way to try inline assembly in C code. The simplest format is _ asm _ ("assembly code ");, for example, the command _ asm _ ("nop"); nop allows the CPU to be idling for one cycle. If multiple commands need to be executed, separate each command with \ n \ t, for example:

__asm__("movl $1, %eax\n\t"        "movl $4, %ebx\n\t"        "int $0x80");

Generally, inline assembly in c code needs to be associated with the c variable, and the complete inline assembly style must be used:

__asm__(assembler template        : output operands               /* optional */        : input operands                /* optional */        : list of clobbered registers   /* optional */        );

This format is composed of four parts. The first part is the assembly instruction. Like the above example, the second part and the third part are constraints, the second part indicates that the computation result of the assembly instruction is to be output to those c operations, the c operation must be a left value expression, and the third part indicates that the assembly instruction needs to obtain the input from those c operations, the fourth part is the list of registers modified in the assembly instruction, which indicates which registers of the compiler will change when executing this _ asm _ statement. The last three parts are optional. If there is no value, enter the ":" number, for example:

Related Article

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.