简体   繁体   English

如何在C中使用寄存器?

[英]How are registers used in C?

Like the CR3 register which is used to point to the page directory. 类似于CR3寄存器,该寄存器用于指向页面目录。 Linux also uses paging and is written in C, but how are these registers used in C (how to select a particular register using C)? Linux也使用分页并且用C编写,但是这些寄存器如何在C中使用(如何使用C选择特定的寄存器)?

The C language provides no way to access specific processor registers. C语言无法提供访问特定处理器寄存器的方法。 This is all up to the compiler. 这完全取决于编译器。

To access specific registers you would have to write at least this part of your code in assembler. 要访问特定的寄存器,您必须至少在汇编器中编写这段代码。

The registers you are talking about are not a property of the language but the property of the hardware on which you run your programs. 您所讨论的寄存器不是语言的属性,而是运行程序的硬件的属性。 I believe that you are talking about an x86 type hardware. 我相信您在谈论的是x86类型的硬件。 cr0-4 and orther specific regs are a property of the operating system and are managed by it, including paging table. cr0-4和其他特定的regs是操作系统的属性,由操作系统管理,包括分页表。

So, the language does not provide a way to access those hw-specific registers. 因此,该语言无法提供访问这些硬件特定寄存器的方法。 The only way is to write an assembly code (hardware-specific) to manipulate them. 唯一的方法是编写汇编代码(特定于硬件)以对其进行操作。 The only thing which the language provides is the asm() operator which allows to insert assembly code in the program. 该语言提供的唯一内容是asm()运算符,该运算符允许在程序中插入汇编代码。

Standard C does not provide any facility to directly access processor registers. 标准C没有提供直接访问处理器寄存器的任何功能。 Some implementations may provide extensions that allow you to embed assembly code in your C code (such as the asm extension provided by gcc). 某些实现可能会提供扩展,使您可以将汇编代码嵌入C代码中(例如gcc提供的asm扩展)。

Generally speaking, if you need direct access to a processor register (or other hardware-specific location), you'd write that routine in assembler and link it into the larger program. 一般来说,如果您需要直接访问处理器寄存器(或其他特定于硬件的位置),则可以在汇编器中编写该例程并将其链接到较大的程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM