当前位置:网站首页>GCC C inline assembly

GCC C inline assembly

2022-06-11 02:30:00 Eddy_ l

GCC C Inline assembly


C The basic syntax of language inline assembly is as follows :

asm asm-qualifiers ( AssemblerTemplate 
                 : OutputOperands 
                 [ : InputOperands
                 [ : Clobbers ] ])

asm asm-qualifiers ( AssemblerTemplate 
                      : OutputOperands
                      : InputOperands
                      : Clobbers
                      : GotoLabels)

among :

  • asm: Statement C Insert assembly code into the program , Can also write __asm__.

  • asm-qualifiers: qualifiers , Its optional values are :
    volatile、inline、goto, among volatile Is to instruct the compiler not to optimize this assembly code ,inline Used to instruct the compiler to keep the size of the hypothetical assembly code instructions as small as possible ,goto The code used for assembly can jump to one or more C label .

  • AssemblerTemplate: Assembly instruction , In use, you need to enclose it in double quotation marks , If there are multiple instructions , Each instruction needs to be followed by \n\t Separate .

  • OutputOperands: Output operands , When inline assembly code is executed , Return to C Program save , When there are multiple output parameters , Use commas to separate . The format is :[ [asmSymbolicName] ] constraint (cvariablename)

    • asmSymbolicName: Symbol name , Any permitted C Variables are allowed , Two operands in the same statement cannot use the same symbolic name .
    • constraint: Denotes a constraint , See table for optional values 1.
    • cvariablename:C Language variable name .
  • InputOperands: Enter the operands , Where to transfer operation data . The format is :[ [asmSymbolicName] ] constraint (cexpression).

    • asmSymbolicName: It's a symbolic name , Any valid C Variable names are acceptable , Two operands in the same statement cannot use the same symbolic name . When not in use , Don't write .
    • constraint: Denotes a constraint .
    • cexpression:C Language expressions .
  • Clobbers: In inline assembly , For the registers involved in the output operand 、 Memory , Made changes ,Clobbers Used to indicate what is being modified , See table for optional values 3.

Different platforms have different inline assemblies , It is called assembly dialect , The differences between specific platforms can be seen GNU C Extended assembly .


surface 1 constraint Optional value
constraint describe
mmemory operand Indicates that you want to pass in a valid address , as long as CPU Can support this address , Can be passed in .
rregister operand Register operand , Use registers to hold these operands .
iimmediate integer operand Indicates that an immediate number can be passed in .

surface 2 constrain Modifier
constraint Modifier Characters describe
= Write only operands , Indicates that inline assembly will modify this operand .
& This means that this operand is an early change operand , It is modified by using the input operand before the instruction is completed . therefore , This operand cannot be located in a register that is used as an output operand or any memory address part . If the old value is only used as input before it is written , An input operand can be an early change operand .
+ Read and write operands , Can only be used to decorate output operands .
nothing The default operation is read-only .

See for more modifiers http://hehezhou.cn/gccint/Modifiers.html#Modifiers.


surface 3 Clobbers Optional value
Clobbers describe
cc Indicates that the assembly code will modify the condition ( sign ) register
memory In assembly code , Will modify the memory .

For example

#define __ASM_STR(x) #x

#define csr_write(csr,val) \ ({
       \ unsigned long __val = (unsigned long)(val); \ __asm__ __volatile__("csrw "__ASM_STR(csr) ", %0" \ : \ : "rK"(__val) \ : "memory"); \ })

#define csr_read_set(csr,val) \ ({
       \ register unsigned long __v = val; \ __asm__ __volatile__("csrrs %0,"__ASM_STR(csr) ", %1" \ : "=r"(__v) \ : "rK"(__v) \ : "memory"); \ __v;
})

Reference resources

GNU C Extended assembly

ARM GCC Embedded (inline) Compilation manual

GCC Inline assembly


🧇

原网站

版权声明
本文为[Eddy_ l]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110124175423.html