How to use inline assembly of assembly instructions with C expression operands in C code

Extended asm ---- assembly instructions with C expression operands
Extended asm statement syntax format : #Syntax
1:
asm asm-qualifiers (AssemblerTemplate
: OutputOperands
[: InputOperands
[: Clobbers] ]) #Syntax
2:
asm qualifiers-ASM (AssemblerTemplate
:
: InputOperands
: clobbers
: GotoLabels)

Use the extended asm to read/write C variables in the assembly code and jump to the C label from the assembly code.
The extended asm syntax uses a colon ":" after the assembly template to delimit the operand parameters.
The asm qualifier of Syntax 1 includes volatile and inline, and the asm qualifier of Syntax 2de includes both of them, as well as goto.
When compiling code with -ansi and various -std options, use the keyword __asm__ instead of asm.
The extended asm statement must be in a function body.

asm-qualifiers (asm-qualifiers):
Volatile:
The typical use of extended asm statements is to manipulate input values ​​to generate output values.
However, asm statements may also have side effects.
If this is the case, you may need to use the volatile qualifier to disable certain optimization
inline:
If you use the inline qualifier, the size of the asm statement should be as small as possible for the purpose of inlining.
goto: The
goto qualifier informs the compiler that asm may perform a jump to one of the label lists in GotoLabels.

Parameters:
AssemblerTemplate
This is a text string, a template for assembly code.
It is a combination of fixed text and quoted input, output, and goto parameters.
OutputOperands
is a comma-separated list of C variables modified by instructions in the assembly template.
The output operand can be empty.
InputOperands
is a comma-separated list of C expressions read by instructions in the assembly template.
Clobbers
are a comma-separated list of registers or other values ​​changed by instructions in the assembly template.
You can allow an empty list
GotoLabels to jump
from the assembly template C label list
asm statement can not jump to other asm statements, can only jump to the listed GotoLabels
example:
void test()
{ int src = 1; int dst;

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

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

}

Volatile:
If it is determined that the output variable is not needed, the GCC optimizer sometimes discards the asm statement.
In addition, if the optimizer thinks that the code always returns the same result (that is, there is no change in the input value between calls), then they may move the code out of the loop.
Use the volatile qualifier to prohibit these optimizations.
Asm statements without output operands (including asm goto statements) are implicitly volatile.

Guess you like

Origin blog.csdn.net/wzc18743083828/article/details/100544042