Can someone help me with this MIPS problem, I\'ve asked for help 2 times for thi
ID: 3600410 • Letter: C
Question
Can someone help me with this MIPS problem, I've asked for help 2 times for this problem.
Write a MIPS code that finds the minimum, maximum, sum and average of an array A. In your code, you need to load array A base address in the $s1 register. Once your code completes its execution, minimum, maximum, sum and average will be stored in $s2, $s3, $s4 and $s5 registers, respectively.
Hint:
You can declare arrays with initialization list with the following MIPS declaration.
.data
A: .word 10, 160, 20, 70, 60, 140,150, 80, 90,100, 110, 30, 40, 120,130, 50
Asize: .word 16
ShiftAmount: .word 4
You can assume that the array size is power of 2. You can use shift operation for division calculation. You can use the ShiftAmount variable to specify the total shift.
Also I need to compute the median of the array.
Explanation / Answer
MIPS CODE
.data
A: .word 10, 160, 20, 70, 60, 140,150, 80, 90,100, 110, 30, 40, 120,130, 50
Asize: .word 16
ShiftAmount: .word 4
min: .word
max; .word
sum: .word 0
avg: .word 0
.code
la $t0, A ; base address of array
la min, 0($t0) ; initialize min with first element
la max, 0($t0) ; initialize max with first element
li $t1, 0 ; initiate counter i to access elements as A[i]
LOOP:
sll $t4,$t1,2 ; left shift by 2 bits will multiply i by 4 which gives offset of element to be accessed since ith element is stored at 4*i offset from base address
add $t2, $t0,$t4 ; add this offset to base address
lw $t3, 0($t2) ; t3 contains A[i]
cmp $t3, min
blt min, 0($t3) ; if A[i] < min then min = A[i]
cmp $t3, max
bgt max, 0($t3) ; if A[i] > max then max = A[i]
add sum, 0($t3)
addi $t1, #1 ; increment the counter by 1
cmp $t1, #16
glt LOOP ; if counter <16 then loop again otherwise exit the loop
la $t1, sum ; load sum into t1
sra $t4,$t1,4 ; right shift sum by 4 bits to divide it by 16(24) [avg = sum/16]
lw avg, 0($t4)
EXIT:
_exit
min, max,sum & avg contains corresponding minimum, maximum, sum & average of the array.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.