我在网上和这个网站上搜索过,我找不到在MIPS中实现2D数组的好例子.我希望能够看到一个如何遍历数组以便将数据放在特定索引以及如何打印数组的示例,如下所示.
例如5x5数组,其中$将是每个索引中的数据.
a b c d e
1 $ $ $ $ $
2 $ $ $ $ $
3 $ $ $ $ $
4 $ $ $ $ $
5 $ $ $ $ $
Run Code Online (Sandbox Code Playgroud)
Nod*_*.JS 15
您需要了解的有关2维数组的所有信息:
要分配你,你需要计算(#row X #column)X #byte
关于char需要1个字节,4个整数,4个单精度浮点数,8个双精度浮点数.例如:
要动态分配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
Run Code Online (Sandbox Code Playgroud)
获取索引(3,4)的地址:
旁注:我使用左移逻辑而不是乘法,因为sllMIPS中的shift()需要1个时钟周期,但mul指令需要33个时钟周期.因此,提高了代码的效率.

更新/编辑(自从我写这个答案以来已经过去3年了,所以我会改进我的答案):
以行主格式迭代整数(不是双精度)的二维矩阵的伪代码如下:
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
}
}
Run Code Online (Sandbox Code Playgroud)
但是,以列主格式迭代整数(不是双精度)的二维矩阵的伪代码如下:
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
}
}
Run Code Online (Sandbox Code Playgroud)
注意:正如我们所看到的,循环结构保持不变,但地址计算部分略有改变.现在实现上述伪代码很简单.我们需要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
Run Code Online (Sandbox Code Playgroud)
将值读入行主矩阵的实现:
.data
read_row_matrix_prompt_p: .asciiz "Enter an integer: "
###########################################################
.text
read_row_matrix:
li $t3, 0 # initialize outer-loop counter to 0
read_row_matrix_loop_outer:
bge $t3, $t1, read_row_matrix_loop_outer_end
li $t4, 0 # initialize inner-loop counter to 0
read_row_matrix_loop_inner:
bge $t4, $t2, read_row_matrix_loop_inner_end
mul $t5, $t3, $t2 # $t5 <-- width * i
add $t5, $t5, $t4 # $t5 <-- width * i + j
sll $t5, $t5, 2 # $t5 <-- 2^2 * (width * i + j)
add $t5, $t0, $t5 # $t5 <-- base address + (2^2 * (width * i + j))
li $v0, 4 # prompt for number
la $a0, read_row_matrix_prompt_p
syscall
li $v0, 5 # read a integer number
syscall
sw $v0, 0($t5) # store input number into array
addiu $t4, $t4, 1 # increment inner-loop counter
b read_row_matrix_loop_inner # branch unconditionally back to beginning of the inner loop
read_row_matrix_loop_inner_end:
addiu $t3, $t3, 1 # increment outer-loop counter
b read_row_matrix_loop_outer # branch unconditionally back to beginning of the outer loop
read_row_matrix_loop_outer_end:
Run Code Online (Sandbox Code Playgroud)
将值读入列主矩阵的实现:
.data
read_col_matrix_prompt_p: .asciiz "Enter an integer: "
###########################################################
.text
read_col_matrix:
li $t3, 0 # initialize outer-loop counter to 0
read_col_matrix_loop_outer:
bge $t3, $t1, read_col_matrix_loop_outer_end
li $t4, 0 # initialize inner-loop counter to 0
read_col_matrix_loop_inner:
bge $t4, $t2, read_col_matrix_loop_inner_end
mul $t5, $t4, $t1 # $t5 <-- height * j
add $t5, $t5, $t3 # $t5 <-- height * j + i
sll $t5, $t5, 2 # $t5 <-- 2^2 * (height * j + i)
add $t5, $t0, $t5 # $t5 <-- base address + (2^2 * (height * j + i))
li $v0, 4 # prompt for number
la $a0, read_col_matrix_prompt_p
syscall
li $v0, 5 # read a integer number
syscall
sw $v0, 0($t5) # store input number into array
addiu $t4, $t4, 1 # increment inner-loop counter
b read_col_matrix_loop_inner # branch unconditionally back to beginning of the inner loop
read_col_matrix_loop_inner_end:
addiu $t3, $t3, 1 # increment outer-loop counter
b read_col_matrix_loop_outer # branch unconditionally back to beginning of the outer loop
read_col_matrix_loop_outer_end:
Run Code Online (Sandbox Code Playgroud)
您可以根据一维数组来设置二维数组。您只需要正确地将元素从一维数组映射到二维数组。这个网站有图片:
http://www.plantation-productions.com/Webster/www.artofasm.com/Windows/HTML/Arraysa2.html#1010609
您可以使用标准格式来寻址每个单元格。例如:
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
Run Code Online (Sandbox Code Playgroud)
您应该能够看到该模式 :) 一般来说,如果有 M 列和 N 行,则可以在点 i * M + j - 1 处访问第 i 行第 j 列(零索引)的单元格