gpt4 book ai didi

c - MIPS 中的双重条件 if 语句

转载 作者:太空宇宙 更新时间:2023-11-04 03:10:26 24 4
gpt4 key购买 nike

我正在将以下 C 代码转换为 MIPS,isIdent 函数似乎总是返回 0。

C: full code here

int isIdent (int m[N][N], int n)
{
for (int row = 0; row < n; row++)
for (int col = 0; col < n; col++)
if (row == col && m[row][col] != 1)
return 0;
else if (row != col && m[row][col] != 0)
return 0;
return 1;
}

MIPS: isIdent code here Full code here

我试过改变 if 语句的位置,例如首先检查 rowcol 但是它似乎没有什么区别。任何帮助将不胜感激!

最佳答案

已找到 solution !正如@CraigEstey 指出的那样,我必须做的实际上是从计算出的地址中获取值。

# m[row][col] = *(&m[0][0] + (row * N) + col)
mul $t0, $s2, $s1 # % <- row * N
add $t0, $t0, $s3 # + col
li $t1, 4
mul $t0, $t0, $t1 # * sizeof(word)
addu $t0, $s0, $t0 # + &m[0][0]
lw $a0, ($t0) # actually fetch m[r][c] from memory

非常感谢大家:)

关于c - MIPS 中的双重条件 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927886/

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