简体   繁体   English

如何在MIPS中的整数和浮点数组中找到最大值和最小值?

[英]How to find Max and Min values in Integer and Float arrays in MIPS?

I am VERY much a beginner to MIPS and I am very confused. 我是MIPS的初学者,我非常困惑。 What I need to do is find the maximum and minimum values in both an integer array and a float array. 我需要做的是在整数数组和浮点数组中找到最大值和最小值。 I attempted to adapt an answer from sorting array in mips (assembly) to try to sort the array, but I'm not sure where to go from here, plus, I know my trying to sort the float array is very wrong. 我试图通过对mips(汇编)中的排序数组进行调整来尝试对数组进行排序,但是我不确定从何处去,再加上,我知道我尝试对float数组进行排序是非常错误的。 I apologize as I know what I have written is probably very dumb, but any help would be MUCH appreciated! 很抱歉,我知道我写的内容可能很愚蠢,但是任何帮助将不胜感激!

.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

.text

main:
la $t0, intArray
add $t0, $t0, 40

intOuterSort:
    add $t1, $0, $0
    la $a0, intArray

intInnerSort:
    lw $t2, 0($a0)
    lw $t3, 4($a0)
    slt $t4, $t2, $t3
    beq $t4, 0, intNext
    add $t1, $0, 1
    sw $t2, 4($a0)
    sw $t3, 0($a0)

intNext:
    addi $a0, $a0, 4
    bne $a0, $t0, intInnerSort
    bne $t1, $0, intOuterSort

la $t0, floatArray
add.s $f2, $f2, 40

floatOuterSort:
    add.s $f4, $0, $0
    la $a0, floatArray

floatInnerSort:
    li.s $f6, 0($a0)
    li.s $f8, 4($a0)
    slt $f10, $f6, $f8
    beq $f10, 0, floatNext
    add.s $f4, $0, 1
    s.s $f6, 4($a0)
    s.s $f8, 0($a0)

floatNext:
    add.s $a0, $a0, 4
    bne $a0, $f2, floatInnerSort
    bne $f4, $0, floatOuterSort

You definitely do not need to sort array in order to find max and min and your code is uselessly complex. 您绝对不需要对数组进行排序即可查找最大值和最小值,并且您的代码毫无用处。

Just traverse the array once and remember what are the max and min values. 只需遍历数组一次,并记住什么是最大值和最小值。

Here is a possible version for intSearch 这是intSearch的可能版本

.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
minInt:         .word 0x80000000
maxInt:         .word 0x7fffffff
minFloat:       .float -3.40e+38f
maxFloat:       .float 3.40e+38f

.text

intSearch:
        la   $t0, intArray        ; $t0->@intarray
        addi $t1, $0, 10          ; $t1->N(=10)
        add  $t2, $0, $0          ; $t2->i(=0)
        lw   $t3, minInt($0)      ; $t3->imax (initialised to a small value)
        lw   $t4, maxInt($0)      ; $t4->imin (initialised to a large value)

iloop:  lw   $t5, 0($t0)          ; $t5->intArray[i]
        bgt  $t5, $t4, skipImin   ; $t5 <=? iMin
        move $t5, $t4             ; yes update min
skipIMin:                         ; skip if $t5>iMin
        blt  $t5, $t3, skipIMax   ; $t5 >= iMax
        move $t5, $t3             ; yes update max
skipIMax:                         ; skip if $t5<iMax
        addi $t2, $t2, 1          ; i++
        addi $t0, $t0, 4          ; @intArray++
        blt  $t2, $t1, iloop      ; i<n -> iloop
#       min is in $t4, max in $t3

The float version is similar (and left as an exercise). 浮动版本与此类似(并保留为练习)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM