c - 如何初始化 GNU C 全局寄存器变量

标签 c gcc assembly inline-assembly cpu-registers

GNU C global register variables不能有初始化程序。这不会编译为 C 或 C++:

// at global scope.
register int i asm ("r12") = 10;

给出 ( Godbolt ) 错误:全局寄存器变量具有初始值。本地作用域当然没问题,但 GNU C 本地寄存器变量是完全不同的事情。 (仅保证在与扩展 asm() 语句交互方面执行任何操作。)

代码

#include<stdio.h>
register int i asm ("r12");  //how to initialize i here?
int main()
{
    i=10;            // Would rather avoid this workaround
    printf("%d\n",i);
}

如何在全局范围内初始化i,而不是等到main的顶部?

最佳答案

您无法初始化全局寄存器变量。

GCC documentation声明如下:

Global register variables cannot have initial values, because an executable file has no means to supply initial contents for a register.

另请注意下面的段落:

When selecting a register, choose one that is normally saved and restored by function calls on your machine. This ensures that code which is unaware of this reservation (such as library routines) will restore it before returning.

您不应该使用r12,它不会在调用之间保存。

关于c - 如何初始化 GNU C 全局寄存器变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58321181/

相关文章:

assembly - 如何通过gdb获取从链接器导入的全局变量的值?

c++ - 有人可以帮助我了解 stmdb、ldmia 以及如何用 arm 汇编语言实现此 C++ 代码吗?

visual-studio-2010 - Visual Studio 2010 - 内存窗口 - 编辑值

c - OpenSSL导致丢包?奇怪的CPU使用率

c - C 中的链表实现

c - "-1>>5;"是 C 中的未指定行为吗?

android - 在 Android.mk 中为程序集 (.s) 源文件定义一个符号?

c - 使用 autotools 链接 gstreamer 插件中的外部库

c - gcc 是否应该能够找到它随附安装的 header ?

c++ - g++ 4.9 sanitizer 错误,在 linux 上使用 cin 解析 bool 值(ubuntu 12.04 64 位)