-1
我已经看到这个问题已经被问到了,但我试图找出为什么这个逻辑不起作用。我已经试过这段代码,将它翻译成C++并且工作正常。但在这里它正在打印数组的最后一个元素。帮帮我?在MIPS中查找数组的最小元素
.data
Array: .word 500 100 250 150
Len: .word 4
Sum: .word 0
Average: .word 0
NewLine: .asciiz "\n"
Min: .word 9999
Max: .word -9999
.text
la $t0, Array # Base address
li $t1, 0 # i = 0
lw $t2, Len # $t2 = Len
li $t3, 0 # Sum = 0
li $t5, 0 # Average = 0
la $t6, NewLine
lw $t7, Min # $t7 = min
lw $t8, Max # $t8 = max
while:
lw $t4, ($t0) # Array[i]
add $t3, $t3, $t4 # sum += Array[i]
blt $t4, $t7, else # If first element is < 9999, go to else
else: move $t7, $t4 # Min = Array[i]
add $t1, $t1, 1 # Increment index by 1
add $t0, $t0, 4 # Go to next array element
blt $t1, $t2, while # Do this cycle till i < $t2 (length)
sw $t3, Sum
div $t5, $t3, $t2 # Calculate avg.
sw $t5, Average
# Print sum
li $v0, 1
move $a0, $t3
syscall
# Print new line
li $v0, 4
move $a0, $t6
syscall
# Print average
li $v0, 1
move $a0, $t5
syscall
# Print new line
li $v0, 4
move $a0, $t6
syscall
# Print min element
li $v0, 1
move $a0, $t7
syscall
对不起,我不明白你用代码实现了什么(我是MIPS的初学者)。如果我使用blt $ t7,$ t4,否则,我应该在“其他”标签中写什么?我翻译我的代码是这样的: 如果500 <9999那么最小值为500, 如果100 <500那么最小值为100, ,当250 <100时则分支不转到else,最小值保持为100.我不在这个声明中看到这里的问题? – illuminated
不要在C(或任何其他)语言中将“else”看作“else”。这只是一个标签,你可以将它重命名为“myelse”,没有什么会改变。 当您的代码转换为C,它就像: 如果(T4
solohane
是的,但是这不是在$ t7中存储$ t4(最小的元素)。 – illuminated