-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