Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays MIPS中的二维阵列_Arrays_Mips - Fatal编程技术网

Arrays MIPS中的二维阵列

Arrays MIPS中的二维阵列,arrays,mips,Arrays,Mips,我已经在网上和这个网站上搜索过了,我找不到一个在MIPS中实现2D数组的好例子。我希望能够看到一个示例,说明如何遍历数组,以便将数据放置在特定索引中,以及如何打印数组,如下所示 例如,一个5x5数组,其中$将是每个索引中的数据 a b c d e 1 $ $ $ $ $ 2 $ $ $ $ $ 3 $ $ $ $ $ 4 $ $ $ $ $ 5 $ $ $ $ $ 可以根据一维阵列设置二维阵列。您只需要正确地将元素从一维数组映射到二维数组。本网站有以下图片: 可以使用标准格式寻址每个单元

我已经在网上和这个网站上搜索过了,我找不到一个在MIPS中实现2D数组的好例子。我希望能够看到一个示例,说明如何遍历数组,以便将数据放置在特定索引中,以及如何打印数组,如下所示

例如,一个5x5数组,其中$将是每个索引中的数据

  a b c d e
1 $ $ $ $ $
2 $ $ $ $ $
3 $ $ $ $ $
4 $ $ $ $ $
5 $ $ $ $ $

可以根据一维阵列设置二维阵列。您只需要正确地将元素从一维数组映射到二维数组。本网站有以下图片:

可以使用标准格式寻址每个单元格。例如:

      a  b  c  d  e

1     0  1  2  3  4
2     5  6  7  8  9
3    10 11 12 13 14
4    15 16 17 18 19
5    20 21 22 23 24

您应该能够看到模式:)一般来说,如果有M列和N行,则可以在i*M+j-1点访问第i行和第j列(零索引)处的单元格。您需要了解的关于二维数组的所有信息:

  • 分配
  • 实现嵌套循环
  • 要分配给您,您需要计算所需的(#行X#列)X#字节

    关于字节数,字符需要1,整数需要4,单精度浮点需要4,双精度浮点需要8。 例如:

          a  b  c  d  e
    
    1     0  1  2  3  4
    2     5  6  7  8  9
    3    10 11 12 13 14
    4    15 16 17 18 19
    5    20 21 22 23 24
    
    要动态分配150个双精度元素的数组,以便15行10列:

    li  $t1,15
    li  $t2,10
    mul $a0, $t1, $t2
    sll $a0, $a0, 3   # multiply number of elements by 2^3 = 8
                      # because each double precision floating point number takes 8 bytes
    li  $v0, 9
    syscall
    move $s0,$v0   # save array address in $s0
    
    要获取索引(3,4)的地址,请执行以下操作:

    • 行主:8x(10x3+4)=272,然后将其添加到基址
    • 主列:8x(15x4+3)=504,然后将其添加到基址
    旁注:我使用左移位逻辑代替乘法,因为MIPS中的移位(
    sll
    )需要1个时钟周期,而
    mul
    指令需要33个时钟周期。因此,提高了代码的效率


    更新/编辑(我写这个答案已经三年多了,所以我会改进我的答案):

    以行主格式迭代二维整数矩阵(非双精度)的伪代码如下:

    for (int i = 0; i < array height; i++) {
        for (int j = 0; j < array width; j++) {
    
            prompt and read array value
    
            row index = i
            column index = j
    
            memory[array base address + 4 * (array width * row index + column index)] = array value
        }
    }
    
    for (int i = 0; i < array height; i++) {
       for (int j = 0; j < array width; j++) {
    
           prompt and read array value
    
           row index = i
           column index = j
    
           memory[array base address + 4 * (array height * column index + row index)] = array value
       }
    }
    
    for(int i=0;i
    但是,在列major
    格式中迭代二维整数矩阵(非双精度)的伪代码如下所示:

    for (int i = 0; i < array height; i++) {
        for (int j = 0; j < array width; j++) {
    
            prompt and read array value
    
            row index = i
            column index = j
    
            memory[array base address + 4 * (array width * row index + column index)] = array value
        }
    }
    
    for (int i = 0; i < array height; i++) {
       for (int j = 0; j < array width; j++) {
    
           prompt and read array value
    
           row index = i
           column index = j
    
           memory[array base address + 4 * (array height * column index + row index)] = array value
       }
    }
    
    for(int i=0;i
    注意:如我们所见,循环的结构保持不变,但地址计算部分略有改变。现在实现上述伪代码非常简单。我们需要2个嵌套循环。假设:

    $t0 <-- base address of array (or matrix or 2 dimensional array)
    $t1 <-- height of matrix
    $t2 <-- width of matrix
    i <---- row index
    j <---- column index
    

    $t0通常我会画一些东西,但感觉很懒:/使用我修改后的响应中的映射,您可以使用标准数组访问技术访问2D数组中的任意点