读取寄存器值成C变量 [英] Reading a register value into a C variable

查看:147
本文介绍了读取寄存器值成C变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得看到一个方法来使用扩展GCC内联汇编读取寄存器值,并将其存储到一个C变量。我不能尽管对于我的生活记得如何形成汇编语句。任何帮助深表AP preciated。


解决方案

在比其他的答案不同的方向发展,到目前为止,因为我不知道你想要什么。

GCC手册和教派; 5.40变量指定寄存器中


 寄存器为int * foo的ASM(A5);

下面 A5 是应该使用&hellip登记的名称;


  
  

自然寄存器名是CPU决定的,但这不是一个问题,因为专用寄存器是最多有用以明确的汇编指令(参见扩展ASM )。这两种东西一般需要你根据CPU类型条件化程序。


  
  

定义这种寄存器变量并不保留寄存器;流控制确定变量的值不活它仍然可用于地方其他用途。


GCC手册和教派;为3.18选项code生成约定


  

-ffixed -


  
  

    

将名为的为一个固定名册内;产生code不应该引用它(除非作为堆栈指针,帧指针或某个其他固定的功能)。


  

这可以复制理查德的答案更简单的方式,

  INT的main(){
    注册INT I A​​SM(EBX);
    返回I + 1;
}

虽然这是没有意义的,因为你不知道什么是在 EBX 注册。

如果您结合这两点,以 GCC -ffixed-EBX编写本

 的#include<&stdio.h中GT;
注册INT计数器ASM(EBX);
无效检查(INT N){
    如果((N%2及!&安培; N%3及&安培; N%5))反++;
}
诠释主(){
    INT I;
    计数器= 0;
    为(ⅰ= 1; I&下; = 100;我++)检查(ⅰ);
    的printf(1和100 \\ n之间%d个海明数字,计数器);
    返回0;
}

您可以确保C变量始终使用驻留在快速访问寄存器中,也不会被其他产生code得到重挫。在通常的x86调用约定(相当方便, EBX 是被调用者保存,所以即使它被通过调用无 -ffixed-编译等功能重挫* ,它应该得到恢复了。)

在另一方面,这绝对是不可移植,通常不是性能优势不是,因为你限制编译器的自由。

I remember seeing a way to use extended gcc inline assembly to read a register value and store it into a C variable. I cannot though for the life of me remember how to form the asm statement. Any help is much appreciated.

解决方案

Going in a different direction than other answers so far, since I'm not sure what you want.

GCC Manual § 5.40 Variables in Specified Registers

register int *foo asm ("a5");

Here a5 is the name of the register which should be used…

Naturally the register name is cpu-dependent, but this is not a problem, since specific registers are most often useful with explicit assembler instructions (see Extended Asm). Both of these things generally require that you conditionalize your program according to cpu type.

Defining such a register variable does not reserve the register; it remains available for other uses in places where flow control determines the variable's value is not live.

GCC Manual § 3.18 Options for Code Generation Conventions

-ffixed-reg

Treat the register named reg as a fixed register; generated code should never refer to it (except perhaps as a stack pointer, frame pointer or in some other fixed role).

This can replicate Richard's answer in a simpler way,

int main() {
    register int i asm("ebx");
    return i + 1;
}

although this is rather meaningless, as you have no idea what's in the ebx register.

If you combined these two, compiling this with gcc -ffixed-ebx,

#include <stdio.h>
register int counter asm("ebx");
void check(int n) {
    if (!(n % 2 && n % 3 && n % 5)) counter++;
}
int main() {
    int i;
    counter = 0;
    for (i = 1; i <= 100; i++) check(i);
    printf("%d Hamming numbers between 1 and 100\n", counter);
    return 0;
}

you can ensure that a C variable always uses resides in a register for speedy access and also will not get clobbered by other generated code. (Handily, ebx is callee-save under usual x86 calling conventions, so even if it gets clobbered by calls to other functions compiled without -ffixed-*, it should get restored too.)

On the other hand, this definitely isn't portable, and usually isn't a performance benefit either, as you're restricting the compiler's freedom.

这篇关于读取寄存器值成C变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆