简体   繁体   English

获取C中的__regvar变量的地址

[英]Get address of __regvar variable in C

At the moment I'm working in a poject for an embedded system in which we're using a AVR ATMega8 microproccessor. 目前,我正在为一个嵌入式系统工作,其中我们使用的是AVR ATMega8微型处理器。 As it has a quite small memmory area, I'm trying to store some global variables into registers using the __regvar command. 因为它的内存区域很小,所以我正在尝试使用__regvar命令将一些全局变量存储到寄存器中。

I need to access those variables from multiple files so instead of making all of them external I had thought about making external only a pointer to the first one and through offsets modify all the variables. 我需要从多个文件访问这些变量,所以与其将所有变量都设置为外部,我还没有想到只将外部指针指向第一个变量,并通过偏移量来修改所有变量。

For example: 例如:

Global 1 --> $Reg4 Global 2 --> $Reg5 Global 3 --> $Reg6 Global 4 --> $Reg7 全局1-> $ Reg4全局2-> $ Reg5全局3-> $ Reg6全局4-> $ Reg7

Pointer = AddressOfGlobal1 指针= AddressOfGlobal1

And with that I can modify all registers just by adding 1 (1 byte) 这样我就可以通过添加1(1个字节)来修改所有寄存器

The problem is that I can't get the address of that variable: 问题是我无法获取该变量的地址:

__no_init __regvar unsigned char ms_flags@ 4; 
__no_init __regvar unsigned char tb_flags@ 5; 
__no_init __regvar unsigned char st_flags@ 6; 
__no_init __regvar unsigned char dp_flags@ 7; 


int myfunction(){


 unsigned char* pointer     = 0;

    pointer = &ms_flags;
}    

Getting the following output: 得到以下输出:

Error[Pe513]: a value of type "unsigned char __regvar *" cannot be assigned to an entity of type "unsigned char *" C:\\Users\\Alex\\Desktop\\CentroStirling\\Progreso\\MSUIAR\\app\\init.c 39 错误[P​​e513]:无法将类型为“ unsigned char __regvar *”的值分配给类型为“ unsigned char *”的实体C:\\ Users \\ Alex \\ Desktop \\ CentroStirling \\ Progreso \\ MSUIAR \\ app \\ init.c 39

You can't do that; 你不能那样做; a value stored in a register has no address , by definition. 根据定义,存储在寄存器中的值没有地址 That's the point, the thing that you're jumping through hoops to achieve. 重点在于,您正在努力实现目标。

The draft C99 standard says, in §6.5.3.2: C99标准草案在§6.5.3.2中指出:

The operand of the unary & operator shall be either a function designator, the result of a [] or unary * operator, or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier. 一元&运算符的操作数应为函数指定符, []或一元*运算符的结果,或者是指定不是位字段且未用register存储类说明符声明的对象的左值。

If we peel off the compiler-specifics, I'm pretty sure __regvar somehow implies register . 如果我们剥离了特定于编译器的内容,我可以肯定__regvar某种程度上暗含了register

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

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