MIPS中基于指针的阵列访问 [英] Pointer based array access in MIPS

查看:117
本文介绍了MIPS中基于指针的阵列访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MIPS中基于指针的数组访问是什么意思?

What do we mean by the pointer based array access in MIPS?

推荐答案

基于指针的数组访问"还有其他可能的含义或暗示:

There is an additional possible meaning or implication to "pointer based array access":

您可能有一个指向数组的指针,而不是指向固定地址的数组.实际上,在C/C ++中,指向数组的指针"通常通常只是指向数组第一个元素的指针.基本上,您有一个数组,该数组是函数的参数,或者是一个指向结构或类的成员的数组的指针:

You may have a pointer to an array, rather than an array at a fixed address. Actually, in C/C++, a "pointer to an array" is really usually just a pointer to the first element of the array. Basically, you have an array that is a parameter to a function, or a pointer to an array that is a member of a struct or class:

 void Foo(char a[]); 
   /*or*/ void Foo(char *a);
 struct Bar {  int offset4bytes; char* a; };

通常,当您要使用这样的数组时,该数组的基地址将被加载到寄存器中.

Typically, when you want to use such an array, the base address of the array will be loaded into a register.

现在说您要访问这种数组的元素i,

Now say you want to access element i of such an array,

 char tmp = a[i];

让我们说r1包含数组地址. (实际上,调用约定可能为函数参数指定了一个不同的寄存器.无论编译器发现什么可用于其他代码.)

Let's say that r1 contains the array address. (Actually, probably a different register, specified by the calling convention, for the function paramaters. Whatever the compiler finds available for other code.)

假设我住在寄存器r2中.

Let's say that i lives in register r2.

而且,在很好的情况下,让tmp为r3.

And, for good measure, let tmp be r3.

在某些指令集上,例如Intel x86,有一个寻址模式,看起来像

On some instruction sets, e.g. Intel x86, there is an addressing mode that looks like

  MOV r3, (r2,r1)4

即寻址模式可以添加两个寄存器和一个偏移量(我在struct示例中任意添加了一个字段,以便可以显示出来).

i.e. the addressing mode can add two registers and an offset (I arbitrarily added a field to the struct example so that I could show this).

哎呀,他们甚至可以缩放其中一个寄存器,即所谓的索引寄存器:

Heck, they can even scale one of the registers, the so-called index register:

  MOV r3, (r2*2,r1)4

或者我更喜欢写它

  r3 := load( Memory[r2<<1+r1+4]

但是,

MIPS没有这种base + _index * scale + offset寻址模式.大多数MIPS存储器访问指令仅限于寄存器+偏移量.因此,对于MIPS,您可能必须这样做

MIPS, however, does not have this sort of base+_index*scale+offset addressing mode. Most MIPS memory access instructions are limited to register+offset. Therefore, for MIPS, you might have to do

  ADDU r10, r1,r1    ; dest on left. r10 = 2*r1
  ADDU r11, r2,r10  
  LB r3,(r11)4

即您可能必须添加额外的RISC指令才能完成x86在具有复杂寻址模式的一部CISC指令中的工作.但是,这种寻址方式并不常见,通常可以避免使用.

i.e. you might have to add extra RISC instructions to accomplish what x86 does in one CISC instruction with a complicated addressing mode. However, such addressing is not common, and canm often be avoided.

此外,即使只是在固定地址处寻址阵列,也可能需要MIPS中的其他指令. x86指令可以具有32位内存偏移量-在这种情况下,它实际上可能是数组的绝对地址. MIPS指令的偏移量限制为16位-MIPS指令的宽度为固定宽度,为32位.因此,甚至可能需要单独的指令来访问固定地址的数组,通常将地址的高位加载到寄存器中.

Further, even just addressing an array at a fixed address may require extra instructions in MIPS. x86 instructions can have a 32 bit memory offset - which in this case may actually be an absolute address of an array. MIPS instructions are limited to a 16 bit offset - MIPS instructions are fixed width, 32 bits wide. Therefore, a separate instruction may be required even to access an array at a fixed address, typically to load the upper bits of the address into a register.

更多-MIPS具有较新的指令,例如LUXC1,具有reg + reg寻址模式.但没有按比例缩放的索引,也没有第三个偏移量分量.

And more - MIPS has newer instructions like LUXC1, that have reg+reg addressing modes. But not scaled index, and no third offset component.

由于这种受限的寻址方式,天真的编译器为诸如

Because of this restricted addressing modes, the code that a naive compiler generates for a lop such as

   for(int i=0;i<N;i++) {
      this->a[i] = 0;
   }

如果循环包含上述多指令序列,则

效率低下.

would be inefficient, if the loop contained the multiple instruction sequence mentioned above.

一个循环,例如

   for(char *p=this->a;p<&(this->a[N]);p++) {
      *p=0;
   }

或等效地

   for(char *p=this->a;p<this->a+N;p++) {
      *p;
   }

甚至有时

   for(i=-N,*p=this->a;i<0;i++,p++) {
      *p=0;
   }

可能会更有效率,因为在前两个中,只有一条指令可以进行存储. (最后一次通常只有遍历多个数组才能获胜.

might be more efficient, because, e.g. in the first two there would only be a single instruction to do the store. (The last is usually only a win if traversing several arrays.

现在,在一个简单的示例中,任何优秀的编译器都会为您进行此优化.但是有时候编译器更喜欢这种基于指针的访问.

Now, in a simple example any good compiler will do this optimization for you. But sometimes the compiler prefers such pointer based accesses.

这篇关于MIPS中基于指针的阵列访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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