因此,我目前在 MIPS 中有一项任务。我的任务也是:
1) 编写一个从数组的第一个元素开始的循环 2) 然后依次将每个元素加 1 并将结果存储回数组 3) 如果遇到零,则退出程序
这是我已经拥有的:
.data #by default, the "data segment" starts at address 0x10010000
.word 1
.word 2
.word 3
.word 4
.word 5
.word 6
.word 7
.word 8
.word 9
.word 0
.text #instructions start below
lui $s0, 0x1001 # $s0 holds base address of the array
addi $s1, $zero, 0 # $s1 holds the index of the array
jal increment # call loop procedure
increment:
beq $s0, $zero, Exit # if $s0 value is zero, branch and go to else
addi $s0, $s0, 1 # adds 1 to each element
addi $s1, $s1, 1 # i = i + 1
j increment # jump back to loop
Exit:
infinite: j infinite
我运行它时遇到的问题是它一直在运行。而且我知道第 10 个单词 (.word0) 包含值 0。
我的代码哪里出错了?
非常感谢
@Robert B,这就是我现在所拥有的:
main: #instructions start below
la $s0, myData # $s0 holds base address of the array
addi $s1, $zero, 0 # $s1 holds the index of the array
loop:
beq $s0, $zero, else # if $s0 value is zero, branch and go to else
addi $s0, $s0, 2 # adds 2 to each element
addi $s1, $s1, 1 # i = i + 1
j loop # call loop procedure
else:
addi $v0, $zero, 10 # terminate execution
syscall