【问题标题】:MIPS find lowest value?MIPS 找到最低值?
【发布时间】:2012-12-30 23:38:47
【问题描述】:

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

非常感谢..

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

【问题讨论】:

    标签: assembly mips


    【解决方案1】:

    您尝试选择最小数字的位:

    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: 
    

    【讨论】:

    • 亲爱的先生..修改后的相同解决方案.. L1: blt $t2, $t1, L2 move $t2, $t1 L2: li $v0, 4 # print answer la $a0, answer系统调用
    • 好吧,您也没有在调用期间保存任何这些寄存器,但由于您实际上并没有说出您得到什么结果,所以很难回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2018-08-18
    • 2021-09-11
    • 2021-07-09
    • 2016-11-21
    • 2023-01-13
    • 2015-01-31
    相关资源
    最近更新 更多