我试图将下面的C代码转换成MIPS,但是我很难理解您如何获取数组中的[k-1]。
int vek[100];
main()
{
int k;
int first = vek[0];
for (k = 1; k < 100; k++)
{
vek[k - 1] = vek[k];
}
vek[k – 1] = first;
}
到目前为止我得到的是:
.data
vek: .space 400
.text
main:
addi s0,zero,1 #k = 1
addi s1,zero.100 #value 100
la t1,vek #t1 as index in vek
add s2,t1,zero #first = vek[0]
L1:
bgt s0,s1,end #if k is bigger then 100 jump to end label
addi s0,zero,-1 #k-1
sll t2,s0,2 #t2 = 4*k next space in the array
这就是我迷失自我的地方,我不明白该如何翻译剩下的代码。由于网络上缺少MIPS教程,你是我最后的机会。如果某个善良的灵魂能帮我翻译代码的最后一部分,并给我一个很好的解释。
这不是我要用的东西,它只是一个简单的例子,一个问题,将在考试中。
最佳答案
下面是一个应该可以工作的简单实现,逐行解释发生了什么:
get_first:
lw $s0, vek # 1. Load the word stored at location 'vek' into $s0.
addi $s1, $zero, 0 # 2. Load zero into $s1. This will store an offset into 'vek', in bytes.
loop_1:
la $s2, vek($s1) # 3. Load the *address* of the $s1-th word of 'vek' into $s2. This is the location you want to write to.
addi $s1, $s1, 4 # 4. Advance the $s1 pointer to the next word in 'vek'.
lw $s3, vek($s1) # 5. Load the *value* of the $s1-th word of 'vek' into $s3. This is the value you want to copy.
sw $s3, ($s2) # 6. Store the value obtained in line 5 into the location obtained in line 3.
blt $s1, 400, loop_1 # 7. Repeat until we have iterated over all memory in 'vek'.
set_last:
sw $s0, vek + 396 # 8. Store the value obtained in line 1 into the end of 'vek'.
你也许可以让这个更简洁,但是我试着让它更容易理解,而且我已经很久没有看到MIPS了。
关于c - 从C到MIPS的循环和数组转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33395460/