帮助打印5到15的MIPS阵列

问题描述

| 我试图了解MIPS中的数组,并且在这样做时真的很难。我正在使用的数组在C ++中看起来像这样:
int array [10];
void main(){
  int i;
  for(i = 0; i < 10; i++){   
    array[i] = i + 5;  
  }  
  for(i = 0; i < 10; i++){  
    cout << array[i] << endl;  
  }  
  return 0;  
} 
到目前为止,我已经有了此MIPS代码,但它有错误并打印了所有的
0
.data  
array: .space 40  
    .globl main  
    .text  
<code>main:  
li $t0,0               # i=0  
li $t4,0               # i=0 for print loop   
li $s1,10              # $s1 = 10  
la $a1,array           # loads array to $a1  

LOOP:  
bge  $t0,$s1,print    # branch to print if i<10  
addi $t1,$t0,5        # i+5  

add $t2,$t1,$t1       # 2 * i  
add $t2,$t2,$t2       # 4 * i  
add $t2,$a1       # $t2=address of array[i]  
sw  $t3,0($t2)  
addi $t0,1        # i++  
j LOOP                  # jumps to top of loop  

print:  
bge $t4,exit      # branch to exit if i < 10  
add $t5,$t4,$t4       # 2 * i  
add $t5,$t5,$t5       # 4 * i  
add $t5,$a1       # $t2=address of array[i]  
sw  $t6,0($t5)  

li  $v0,1  
move $a0,$t6           #moves value to $a0 to be printed  
syscall  

addi $t4,1        # i++  
j print                 # jumps to top of print  


 exit:  
li $v0,10                      #load value for exit   
syscall                         #exit program  

解决方法

我看到3个错误:
add $t2,$t1,$t1       # 2 * i 
应该
add $t2,$t0,$t0       # 2 * i 
因为
$t1 = $t0 + 5
其次,
sw  $t3,0($t2)
应该
sw  $t1,0($t2)
最后,
sw  $t6,0($t5)
应该
lw  $t6,0($t5)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...