MIPS找到最低价值?

时间:2012-12-30 15:13:16

标签: assembly mips

我尝试过以下代码,对于前两个整数,它工作正常。但是当我们在第3个提示中取最小数字时,它不会被视为最小数字。这是我的代码。我犯的那个错误。(抱歉我的英语不好)

非常感谢..

    .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 "

1 个答案:

答案 0 :(得分: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: