0

我正在尝试创建一个循环遍历数组以达到最终值 0 的程序。

在遍历数组中的每个元素时,我需要将值增加 2 并将最终结果存储在 $v0 中。(我不知道该怎么做)

到目前为止,这是我的代码:

.data  
list: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 0

.text 

li  $s0, 0x10010000          

increment:         
beq $s0, $zero, EndLoop      
lw $s1, 0($s2) 
sw $s1, 0($s2) 
la $s2, list 
la $s1, list   
lb $s2, 0($s1)           
addi $s2, $s2, 2         
sb $s2, 0($s1)           
addi $s1, $s1, 1         

j increment                

EndLoop: 

我的问题是:

我不断收到错误消息,提示运行时异常,地址超出范围。知道为什么吗?

谁能指出我将最终值存储在 $v0 中的正确方向?

4

1 回答 1

1

I Keep getting an error saying runtime exception, address out of range. Any idea why?

Sure, here:

.text 

li  $s0, 0x10010000          

increment:         
beq $s0, $zero, EndLoop      
lw $s1, 0($s2)

Problems:

  1. You don't seem to define where your program is supposed to start its execution. I'd expect some label at the beginning of the code, but I'm seeing nothing. Is the relevant part simply not shown in the question?

  2. Your code is attempting to read from a memory location whose address is contained in register s2, however your code does not initialize this register.

  3. Also, you never modify s0, so the loop is hopelessly endless.

Could anyone point me in the right direction about storing the final values in $v0?

I see no problem with storing anything in v0.

于 2013-02-22T01:43:17.047 回答