MIPS:找到三个整数的MIN值。输出错误。为什么?

时间:2014-02-12 16:58:39

标签: assembly mips qtspim

.data
prompt: .asciiz "Enter in 3 integers, one per line, last one being a negative number: "
sum:    .asciiz "The sum is: "
min:    .asciiz "The minimum is: "
max:    .asciiz "The maximum is: "
mean:   .asciiz "The mean is: "
variance:   .asciiz "The variance is: "
newline:    .asciiz "\n"
count:    .word 3
array:    .space 12
.text
main:
    #prints prompt msg
    li    $v0, 4
    la    $a0, prompt
    syscall

    #takes first integer from user and stores into $t1
    li    $v0, 5
    syscall
    move     $t1, $v0


    #takes second integer from user and stores into $t2
    li    $v0, 5
    syscall
    move     $t2, $v0


    #takes third integer from user and stores into $t3
    li    $v0, 5
    syscall
    move    $t3, $v0


    #min/max utilizing array
    la    $t0, array        #initializing array at a[0]
    li    $t4, 0            #min
    li    $t5, 0            #max
    li    $t6, 0            #i
    li    $t7, 0
    sw    $t1, 4($t0)        #user first input stored in a[1]
    sw    $t2, 8($t0)        #user second input stored in a[2]
    sw    $t3, 12($t0)        #user third input stored in a[3]


blt $t1, $t2, Else
ble $t1, $t3, Else2
j T1P

Else:
    blt $t2, $t3, Else2
    j T2P

Else2:
    blt $t3, $t2, T3P

T1P:
    move $a0, $t1
    li   $v0, 1
    syscall

T2P:
    move $a0, $t2
    li   $v0, 1
    syscall

T3P:
    move $a0, $t3
    li   $v0, 1
    syscall

好的,我要做的是找到三个用户输入的整数中的最小值。

程序将运行并允许用户输入三个整数。

正在发生的是,不是打印出最小值,而是打印出所有三个值。

blt $t1, $t2, Else
    ble $t1, $t3, Else2
    j T1P

    Else:
        blt $t2, $t3, Else2
        j T2P

    Else2:
        blt $t3, $t2, T3P

    T1P:
        move $a0, $t1
        li   $v0, 1
        syscall

    T2P:
        move $a0, $t2
        li   $v0, 1
        syscall

    T3P:
        move $a0, $t3
        li   $v0, 1
        syscall

这是代码段,用于检查哪个值最小,但不是。

我不明白为什么所有三个整数都打印出来。我认为我的错误检查会阻止打印所有三个整数。我在QTSPIM中运行此程序。

示例:用户在4,3和2中打出三个整数。控制台显示4,3和2,而不是仅打印最小的int。

1 个答案:

答案 0 :(得分:2)

执行后,您需要退出条件执行部分。换句话说:

    blt $t1, $t2, Else
    ble $t1, $t3, Else2
    j T1P

Else:
    blt $t2, $t3, Else2
    j T2P

Else2:
    blt $t3, $t2, T3P

T1P:
    move $a0, $t1
    li   $v0, 1
    syscall
    j END

T2P:
    move $a0, $t2
    li   $v0, 1
    syscall
    j END

T3P:
    move $a0, $t3
    li   $v0, 1
    syscall

END: