Embedded assembly of gcc under linux

Only the translation part is commonly used, the original address (Gcc manual) https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Extended-Asm 

0. Format
asm [volatile] ( AssemblerTemplate
                 : OutputOperands
                 [: InputOperands
                 [ : Clobbers ] ])


asm [volatile] goto ( AssemblerTemplate
                      :
                      : InputOperands
                      : Clobbers
                      : GotoLabels)


When using the -ansi or -std option, use __asm__ instead of asm
1. The parameter introduces the


AssemblerTemplate
assembly code, which is a series of assembly code text
This is a literal string that is the template for the assembler code. It is a combination of fixed text and tokens that refer to the input, output, and goto parameters. See AssemblerTemplate. OutputOperands


A comma-separated list of the C variables modified by the instructions in the AssemblerTemplate. An empty list is permitted. See OutputOperands. InputOperands Input variables, which can be empty A comma-separated list of C expressions read by the instructions in the AssemblerTemplate. An empty list is permitted. See InputOperands . Clobbers listed as output











A comma-separated list of registers or other values ​​changed by the AssemblerTemplate, beyond those listed as outputs. An empty list is permitted. See Clobbers and Scratch Registers.


GotoLabels
Goto SignWhen
you are using the goto form of asm, this section contains the list of all C labels to which the code in the AssemblerTemplate may jump. See GotoLabels.


asm statements may not perform jumps into other asm statements, only to the listed GotoLabels. GCC's optimizers do not know about other jumps; therefore they cannot take account of They when deciding how to optimize. 2. There are two representations for variables used in


remarks
assembly code
    %n n is the position in the variable list (commonly used)
    %[alias] Aliases need to be declared before variables
Example
int src = 1;
int dst;   


asm ("mov %1, %0\n\t"
    "add $1, %0"
    : "=r" (dst)
    : "r" (src));


printf("%d\n", dst);



This code copies the value of src to dst


and converts it in two ways.
uint32_t Mask = 1234;
uint32_t Index;


  asm ("bsfl %1, %0"
     : "=r" (Index)
     : "r" (Mask)
     : "cc");


another way
uint32_t Mask = 1234;
uint32_t Index;


  asm ("bsfl %[aMask], %[aIndex]"
     : [aIndex] "=r" (Index)
     : [aMask] "r" (Mask)
     : "cc");


3. InputOperands and OutputOperands
OutputOperands must start with = or +, followed by the possible storage location and (variable name)
InputOperands only writes the possible storage location and (variable name)


= at the beginning means output only (write only), + at the beginning means both Output is also read (read and write)
Common storage locations indicate
    r register
    m memory

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325600775&siteId=291194637