Question

.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

Ok,what I am trying to do is find the min value out of three user inputted integers.

The program will run and allow the user to input their three integers.

What is happening is that instead of printing out the min value, all three values are printing out.

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

This is the section of code that is suppose to check which value is the smallest, it isn't.

I don't understand why all three integers are printing out. I thought that my error checks would prevent all three integers from being printed. I am running this program in QTSPIM.

Example: User punches in 4 , 3 , and 2 as their three ints. Console is showing 4, 3,and 2, instead of just printing the smallest int.

Was it helpful?

Solution

You'll need to exit out of the conditional execution sections once they execute. In other words:

    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:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top