繁体   English   中英

从C到MIPS的循环和数组转换

[英]C to MIPS conversion for loop and array

我试图将以下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已经很长时间了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM