C语言可以访问32位寄存器吗?
C语言可以访问32位寄存器吗?如果是的话,怎么样?如果没有,那么有什么方法可以在 C 中嵌入汇编代码吗?顺便说一句,我正在使用 MinGW 编译器。 提前致谢!
Is it possible to access 32-bit registers in C ? If it is, how ? And if not, then is there any way to embed Assembly code in C ? I`m using the MinGW compiler, by the way.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您只想读取寄存器,您可以简单地:
显然它与实例化相关。
另一种方法是使用内联汇编。例如:
这会将
ecx
值存储到变量value
中。我已经在此处发布了类似的答案。If you want to only read the register, you can simply:
Obviously it's tied to instantiation.
Another way is using inline assembly. For example:
This stores the
ecx
value into the variablevalue
. I've already posted a similar answer here.您想访问哪些寄存器?
通用寄存器通常无法从 C 语言访问。您可以在函数中声明寄存器变量,但这并不指定使用哪些特定寄存器。此外,大多数编译器都会忽略寄存器关键字并自动优化寄存器的使用。
在嵌入式系统中,经常需要访问外设寄存器(例如定时器、DMA控制器、I/O引脚)。 从 C 访问它们
此类寄存器通常是内存映射的,因此可以通过定义指针
:或使用预处理器:
或者,您可以使用汇编例程。
对于使用汇编语言,有(至少)三种可能性:
Which registers do you want to access?
General purpose registers normally can not be accessed from C. You can declare register variables in a function, but that does not specify which specific registers are used. Further, most compilers ignore the register keyword and optimize the register usage automatically.
In embedded systems, it is often necessary to access peripheral registers (such as timers, DMA controllers, I/O pins). Such registers are usually memory-mapped, so they can be accessed from C...
by defining a pointer:
or by using pre-processor:
Or, you can use Assembly routine.
For using Assembly language, there are (at least) three possibilities:
您可以在 C
http://en.wikipedia.org/wiki/Inline_assembler
示例 中嵌入程序集来自维基百科
extern int errno;
You can embed assembly in C
http://en.wikipedia.org/wiki/Inline_assembler
example from wikipedia
extern int errno;
我认为你不能直接做它们。您可以使用如下代码进行内联汇编:
I don't think you can do them directly. You can do inline assembly with code like:
如果您使用 32 位处理器并使用足够的编译器,那么可以。确切的方法取决于您正在编程的特定系统和编译器,当然这将使您的代码尽可能不可移植。
在使用 MinGW 的情况下,您应该查看 GCC 的内联汇编语法。
If you are on a 32-bit processor and using an adequate compiler, then yes. The exact means depends on the particular system and compiler you are programming for, and of course this will make your code about as unportable as can be.
In your case using MinGW, you should look at GCC's inline assembly syntax.
你当然可以。正如其他答案已经显示的那样,“MinGW”(gcc)允许(与其他编译器一样)内联汇编。哪个程序集取决于您系统的 cpu(99.99% 的可能性是 x86)。然而,这使得您的程序无法在其他处理器上移植(如果您知道自己在做什么以及为什么这样做,那就没那么糟糕了)。
讨论 gcc 汇编的相关页面是 这里 和 此处,如果需要,还可以此处。不要忘记它不能是特定的,因为它依赖于体系结构(gcc 可以针对多个 cpu 进行编译)
You can of course. "MinGW" (gcc) allows (as other compilers) inline assembly, as other answers already show. Which assembly, it depends on the cpu of your system (prob. 99.99% that it is x86). This makes however your program not portable on other processors (not that bad if you know what you are doing and why).
The relevant page talking about assembly for gcc is here and here, and if you want, also here. Don't forget that it can't be specific since it is architecture-dependent (gcc can compile for several cpus)
通常不需要从用高级语言编写的程序中访问CPU寄存器:高级语言,如C、Pascal等,其发明正是为了抽象底层机器并使程序更加机器化。独立的。
我怀疑您正在尝试执行某些操作,但不知道如何使用传统方式来执行此操作。
对寄存器的许多访问隐藏在更高级别的构造或系统或库调用中,这可以让您避免编码“脏部分”。请告诉我们更多有关您想要做什么的信息,我们可能会向您建议替代方案。
there is generally no need to access the CPU registers from a program written in a high-level language: high-level languages, like C, Pascal, etc. where precisely invented in order to abstract the underlying machine and render a program more machine-independent.
i suspect you are trying to perform something but have no clue how to use a conventional way to do it.
many access to the registers are hidden in higher-level constructs or in system or library calls which lets you avoid coding the "dirty-part". tell us more about what you want to do and we may suggest you an alternative.