How to use the explicit register variables of inline assembly in C code

Explicit register variables
GNU C allows you to associate specific hardware registers with C variables. In almost all cases, allowing the compiler to allocate registers will generate the best code. However, in some unusual situations, more precise control of variable storage is required.
Both global variables and local variables can be associated with registers.

Global register variable
You can define a global register variable and associate it with a specified register as follows:
register int *foo asm ("r12");

r12 is the register name.
Note that this is the same syntax used to define local register variables, but for global variables, the declaration appears outside the function.
The keyword registerb cannot be used with the keyword static.
The register name must be a valid register name for the target platform.
Do not use type qualifiers such as const and volatile, because the result may be contrary to expectations.
In particular, the use of the volatile qualifier does not completely prevent the compiler from optimizing access to registers.
Registers are a scarce resource in most systems, and allowing the compiler to manage their use usually results in the best code. However, in special cases, it makes sense to keep some in the global scope. For example, this may be useful in some programs, such as a programming language interpreter, which has two global variables that are frequently accessed.
After defining global register variables, the current compilation unit:

Local register variable

You can define a local register variable and associate it with a specified register as follows:
register int *foo asm ("r12");

Guess you like

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