可以将整个数组保存在cpu寄存器中 [英] Is it possible to keep an entire array in cpu register

查看:455
本文介绍了可以将整个数组保存在cpu寄存器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,

  int main()
{
register int arr [4] ;
/ * ... * /
}

arr'分配在某些cpu寄存器中。
(考虑cpu有4个或更多的寄存器)。



或者编译器将忽略数组的

解决方案

根据我的理解,回答是

,因为


  1. (例如对于16位的uC / uP,其地址应该总是位于0x0000到0xFFFF地址空间之间。)


  2. 寄存器直接寻址模式(例如 mov r2,#100 )。
    此寻址模式没有有效地址。 (即使不被认为是寻址模式


  3. 数组元素必须内存位置。 (对于指针运算,使用数组的主要原因)



  1. 编译器可以为上述数组分配寄存器,以便我们可以对其执行一些有限的操作。
    但不能使用内部使用地址进行优化的操作。

请参阅下面的代码。

  int main()
{
register int arr [4];
int i;

arr [0] = 10; / * OK * /
arr [1] = 20; / * OK * /
arr [2] = 30; / * OK * /
arr [3] = 40, / * OK * /

for(i = 0; i <4; i ++)
arr [i] = 10; / *错误:注册变量的地址'arr'请求* /

return 0;
}

所以我的最终结论是,理想情况下即使您的编译器允许,也不应该使用 存储类。



请纠正我或提供更多输入。 : - )


In below code,

int main( )
{
    register int arr[4];
    /* ... */
}

Is it possible that 'arr' is allocated in some cpu register. (Consider cpu has 4 or more registers).

Or compiler will ignore register storage class for array.

解决方案

As per my understanding, answer is YES and NO.

NO because,

  1. Any array element must be explicitly addressable (i.e. for eg. for 16 bit uC/uP its address should always lie between 0x0000 to 0xFFFF address space.)

  2. CPU registers are accessed using register direct addressing mode ( such as mov r2,#100 ). This addressing mode does not have an effective address. ( even it is not considered to be an addressing mode )

  3. Array elements must reside in continous memory locations. ( for pointer arithmetic, the main reason to use array )

and YES because,

  1. Compiler can allocate register for above array, so that we can perform some limited operations on it. But operations which internally uses address for optimizations can't be used.

See below code.

int main( )
{
  register int arr[4];
  int i;

  arr[0] = 10;      /* OK */
  arr[1] = 20;      /* OK */
  arr[2] = 30;      /* OK */
  arr[3] = 40;      /* OK */

  for(i=0;i<4;i++)
    arr[i]=10;    /* Error : "address of register variable 'arr' requested" */

  return 0;
}

So my final conclusion is that, ideally register storage class should never be used with array even if your compiler permits it.

Please correct me or give more inputs. :-)

这篇关于可以将整个数组保存在cpu寄存器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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