Answer to Question #178206 in Assembler for Kent

Question #178206

MIPS. Find the average of an array of integers.

1
Expert's answer
2021-04-04T12:14:11-0400
       .data
array:    .word   8, 5, 34, 20, 2, 43, 30, 20, 8
          .word  20, 21, 27, 40, 29, 3
size:   .word  15
average: .word 0
# code segment
       .text
       .globl main

main:
# Loop through the array to calculate sum
            la $t0, array                # array starting address
            li $t1, 0                    # index, i=0
            lw $t2, size                 # len
            li $t3, 0                    # begining sum=0

Loop:
            lw $t4, ($t0)             # get array[i]
            add $t3, $t3, $t4         # sum = sum + array[i]
            add $t1, $t1, 1           # i = i+1
            add $t0, $t0, 4           # update array address
            blt $t1, $t2, Loop        # IF(i< size) continue
                                      # ELSE
# Calculate AVG
            div $t5, $t3, $t2          # average = sum / length
            sw $t5, average

            li $v0, 1               # print integer
            move $a0, $t5            # $a0 = sum
            syscall             # make the syscall

# Terminate program.
            li $v0, 10            # finished .. stop .. return
            syscall              # make the syscall

 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS