在MIPS中寻找阵列中的最小和最大?

时间:2019-02-24 02:28:07

标签: assembly mips

我必须为自己的课程编写一个程序,该程序将搜索整数数组并在MIPS中输出该数组中最小和最大的整数。

对于输出,最大值和最小值都为0。

为什么会这样?

    .data
    intArray: .word 3, 53, -76, 34, 643, -234, 143, 2, -33, 64
    floatArray: .float 43.53, 45.2244, 25, 64.035, 328, 23.85, 23.86544, 93.4, 46.6543256, 0.00345
    intMinOut: .asciiz "The smallest number in the integer array is: "
    intMaxOut: .asciiz "The largest number in the integer array is: "
.text

main:
    la $t0, intArray
    addi $t1, $zero, 9 
    addi $t2, $zero, 0
    sw $t3, intArray($zero)
    sw $t5, intArray($zero)

    findSmallest:
        beqz $t1, findLargest
        sub $t1, $t1, 1
        blt $t5, $t3, intSmaller 
        addi $t2, $t2, 1
        j findSmallest

    intSmaller:
        lw $t4, intArray($t2) 
        move $t3, $t4 
        addi $t2, $t2, 1 
        j findSmallest 

    findLargest:
        addi $t1, $zero, 9
        addi $t2, $zero, 0
        intLgLoop:
            beqz $t1, print 
            sub $t1, $t1, 1
            bgt $t3, $t5, intLarger
            addi $t2, $t2, 1
            j intLgLoop

    intLarger:
        lw $t4, intArray($t2)
        move $t5, $t4
        addi $t2, $t2, 1
        j intLgLoop

    print:
        la $a0, intMinOut
        li $v0, 4
        syscall
        move $a0, $t3
        li $v0, 1
        syscall
        la $a0, intMaxOut
        li $v0, 4
        syscall
        move $a0, $t5
        li $v0, 1
        syscall
        b end

end:
    .macro done
    li $v0, 10
    syscall
    .end_macro
    done

0 个答案:

没有答案