gpt4 book ai didi

algorithm - Mips函数数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:22 24 4
gpt4 key购买 nike

我正在尝试创建一个函数,将数字排列在方形网格中,其中每一行、每一列中的数字以及前向和后向主对角线中的数字加起来都是相同的数字。虽然我对 mips 比较陌生,但这是我最近的尝试:

square:  lw $t0,$zero #i = 0
lw $t2,1 # k = 1
div $t1,$a1,2 # j = n/2
mul $t3,$a1,$a1 # n*n

while: ble $t2,$t3,else1 # k <= n*n
bgt $t0,-1,else2 #
bgt $t1,-1,else2 #
ble $t1,$a1,else3 #
ble $t0,$a1,else4 #
bne $,$,else5#a[i,j] == 0

else1: #a[i,j] = k
addi $t0,$t0,-1 #
addi $t1,$t1,1 #
addi $t2,$t2,1 #

else2: lw $t0,1 #
addi $t1,$a1,-1 #

else3: lw $t1,$zero #


else4: addi $t0,$a1,-1 #

else5: addi $t0,$t0,2 #
addi $t1,$t1,-1 #

end: jr $ra

此外,这是寄存器的结构:

$a0 = 数组(矩阵)的基址,a

$a1 = n,矩阵的大小(行数和列数)

$t0 = 我

$t1 = j

$t2 = k

$t3 = n*n

$t4 = arraya 索引 - 在步骤中

$t5 = 数组值

我遇到的麻烦是创建 a[i,j] 它是如何完成的?下面是算法:

 i = 0, k = 1 and j = n/2
while (k <= n*n)
if (i > -1 and j > -1 and j < n and i < n and a[i,j] == 0)
a[i,j] = k
i = i - 1, j = j +1 and k = k + 1
else if (i < 0 and j == n) move out of upper right square
i = 1 and j = n - 1
else if (j == n) move out of right side of square
j = 0
else if (i < 0) move above top row
i = n - 1
else move to an already filled square
i = i + 2 and j = j - 1
end if - else
end while loop

最佳答案

您可以通过地址计算连续存储二维数组:a[i, j] 存储在 base address + i + j * n

为了优化,行/列遍历以 +/-1+/-n 增量进行。

或者,您可以为行分配不同的区域并提供行起始表。

关于algorithm - Mips函数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23012004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com