2012-12-30 72 views
0

我試過下面的代碼和前兩個整數數字它工作正常。但是當我們在第三個提示中取最小的數字時,它不會被視爲最小的數字。這是我的代碼。其中,這個錯誤我犯了(對不起我的英語不好)MIPS找到最低值?

非常感謝..

.text 
.align 2 
.globl main 

main: 
# this program prints out the lowest value of three numbers input 

li $v0, 4 
la $a0, prompt1 
syscall 

li $v0, 5 # read keyboard into $v0 (number x is number to test) 
syscall 
move $t0,$v0 # first number in $t0 

li $v0, 4 
la $a0, prompt2 
syscall 

li $v0, 5 # read keyboard into $v0 (number x is number to test) 
syscall 
move $t1,$v0 # second number in $t1 

li $v0, 4 
la $a0, prompt3 
syscall 

li $v0, 5 # read keyboard into $v0 (number x is number to test) 
syscall 
move $t2,$v0 # third number in $t2 

blt $t1, $t0, L1 
move $t1, $t0 # smallest number in $t1 

blt $t2, $t1, L1 
move $t2, $t1 

L1: 
li $v0, 4 # print answer 
la $a0, answer 
syscall 

li $v0, 1 # print integer function call 1 
move $a0, $t1 # integer to print 
syscall 

end: jr $ra 

.data 
prompt1: .asciiz "Enter the first number " 
prompt2: .asciiz "Enter the second number " 
prompt3: .asciiz "Enter the third number " 
answer: .asciiz "\nThe smallest number is " 

回答

0

,你儘量選擇最小的數位:

blt $t1, $t0, L1 
move $t1, $t0 # smallest number in $t1 

blt $t2, $t1, L1 
move $t2, $t1 

L1: 

你只有一個標籤,所以如果遵循第一個分支,則完全跳過與第三個數字的比較。

你會需要更多的東西一樣:

blt $t1, $t0, L1 
move $t1, $t0 # smallest number in $t1 

L1: 

blt $t2, $t1, L2 
move $t2, $t1 

L2: 
+0

這個修改.. L1之後親愛sir..same解決方案: BLT $ T2,T1 $,L2 舉動$ T2,T1 $ L2: li $ v0,4#打印答案 la $ a0,回答 syscall – 9pixle

+0

好吧,你也沒有保存任何這些寄存器的通話,但是因爲你沒有真正說出你得到了什麼結果,所以很難回答。 – JasonD

+0

thnks虐待.. – 9pixle