如何在数组MIPS中查找最小值和最大值

时间:2019-10-02 04:12:07

标签: mips

我可以找到最大值,但是当找到最小值时,我遇到了麻烦,我的最大值找到了最大值,但是无论如何我的最小值始终打印为0,我似乎无法找出是什么我的代码有问题,有人看到我的代码有缺陷吗?

例如:

我输入5个整数

5 10 15 20 25

数组:5,10,15,20,25

最小值:0 最多:25

.text
    .globl main

main:
    la $a0, ask             #ask user for integer thats going to be the size of the array
    li $v0, 4
    syscall

    li $v0, 5               #store that int
    syscall
    move $t1,$v0            #size of my array stored in $t1

    la $t0, array           #load address of our array
    li $t2, 0               #counter = 0
    lw $t3,($t0)            #initialize min = array[0]
    lw $t4,($t0)            #initialize max = array[0]


while:
    la $a0, intask          #ask user for integer
    li $v0, 4
    syscall

    li $v0, 5               #store that int
    syscall
    sw $v0, ($t0)           #store that int in the array


end:    add $t0, $t0, 4         #increment the array to the next index
    add $t2, $t2, 1         #increment the counter by 1
    blt $t2, $t1,while      #branch to while if counter < size of array

endw:
    la $a0,display          # Display "Array is: "
    li $v0,4            
    syscall

    li $t0, 0               # initilize array index value back to 0
    li $t2, 0                # initial size counter back to zero
    la $t0, array            # load address of array back into $t0

 sprint:
    lw $t6,($t0)            #load word into temp $t2
    move $a0, $t6           #store it to a safer place
    li $v0, 1               #print it out
    syscall

    la $a0,space            # Display " "
    li $v0,4            
    syscall

    add $t0, $t0, 4         #increment the array to the next index
    add $t2, $t2, 1         #increment the counter by 1

    blt $t2, $t1,sprint     #branch to while if counter < size of array

    li $t2, 0                # initial size counter back to zero
    la $t0, array            # load address of array back into $t0
    add $t0, $t0, 4         #increment the array to the next index
    add $t2, $t2, 1         #increment the counter by 1


 loop:  lw $t8,($t0)             # t8 = next element in array
    bge $t8, $t3, notMin     #if array element is >= min goto notMin
    move $t3,$t8             #min = array[i]
    j notMax

 notMin: ble $t8,$t4, notMax         #if array element is <= max goto notMax
    move $t4,$t8             #max = array[i]

 notMax:    add $t2,$t2,1            #incr counter
    add $t0,$t0, 4           #go up in index
    blt $t2, $t1,loop        #if counter < size, go to loop

 eprint:
    la $a0,nextline          # Display "\n"
    li $v0,4            
    syscall

    la $a0,min               # Display "min number is "
    li $v0,4            
    syscall

    move $a0, $t3            #displays min number in array
    li $v0,1
    syscall

    la $a0,nextline          # Display "\n"
    li $v0,4            
    syscall

    la $a0,max               # Display "max number is "
    li $v0,4            
    syscall

    move $a0, $t4            #displays max number in array
    li $v0,1
    syscall

    li $v0,10                # End Of Program
    syscall     

.data
array: .space 100
ask: .asciiz "How many numbers will be entered? no more than 15 numbers!: "
intask: .asciiz "Enter an Integer: "
min: .asciiz "The minimum number is: "
max: .asciiz "The maximum number is: "
display: .asciiz "Array: "
space: .asciiz " "
nextline: .asciiz "\n"

1 个答案:

答案 0 :(得分:1)

您正在将$t3(用于存储最小值)初始化为array[0],然后再将任何值存储在array中,因此$t3最终被初始化为0。

如果在将$t3输入值之后将array[0]初始化为array ,应该没问题。