在MIPS中找到二维数组索引的算法?

时间:2019-03-07 23:03:51

标签: multidimensional-array indexing mips

为我的MIPS代码提供了以下程序,作为微型分配,我们仅打算填写sumLoop中前4行的其余部分以在2D数组中找到当前项目的索引。我已经尝试了一些方法,但是我认为我使用的算法不正确。正确的方法是什么? (P.S.我已经尽力减少混乱,这意味着我必须使用此代码中当前的所有内容)。谢谢。

.data 
# the next two lines define an array (mdArray as a 2x2 multidimensional 
array)
mdArray:    .word 2,5  
        .word 3,7


size: .word 2  #dimension of the array (2x2 in this case, note this is only 
for square matrices)
.eqv DATA_SIZE 4 # number of bytes per element, 4 for ints, 1 for chars, 8 
for doubles

.text
main:
la $a0, mdArray # base address
lw $a1, size # size
jal sumDiagonal  #sum of diagonals, in our starting example, this is 9.  
move $a0, $v0 # this is because sumDiagonal will return it's last value in 
$v0
li $v0, 1
syscall

#and done
li $v0, 10
syscall

sumDiagonal:

li $v0,0 #sum =0
li $t0,0 #t0 as the index

    sumLoop: 
        #The next 4 lines are the bit you need to derive and fill in yourself
        mul 
        add
        mul 
        add

        lw $t2, ($t1)  #getting element 
        add $v0, $v0, $t2 # sum = sum + mdArray[i][i]

        addi $t0, $t0, 1 # i = i+1
        blt $t0, $a1, sumLoop  #if i < size, loop again

jr $ra  #ends sum diagonal

0 个答案:

没有答案