Reputation: 109
I am VERY much a beginner to MIPS and I am very confused. 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. 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
Upvotes: 0
Views: 1687
Reputation: 11547
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
.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).
Upvotes: 2