Write a procedure to calculate Average of numbers(integers) using Arrays. Send base address of array in register $a1 and Array length in register $a2 to the procedure and return Average in register $v0 to main program.
in assembly
solution:
code.asm
.data
numberList : .word 1 2 3 4 5 6 7 8 9 10 55
sum_p: .asciiz "Sum: "
arraySize : .word 11
average_p: .asciiz "Average: "
# To Do: create static array of 10 integers
sum_var: .word 0 # sum variable initialized to 0
count_var: .word 0 # count variable initialized to 0
###########################################################
.text
main:
la $a0,numberList
lw $a1,arraySize
jal getAverage
move $t3,$v0 #move result to t3
li $v0, 4
la $a0, average_p
syscall
move $a0, $t3
li $v0, 1
syscall
mainEnd:
li $v0, 10
syscall # Halt
getAverage:
move $t8,$a1 #move size of array
move $t0,$a0 #move array pointer
li $t3,0
li $t5,0 #for sum
read_values_loop:
bge $t3,$t8,read_values_loop_exit
mul $t4,$t3,4
add $t4,$t4,$t0
lw $t6,0($t4)
add $t5,$t5,$t6
addi $t3,$t3,1
j read_values_loop
read_values_loop_exit:
div $v0,$t5,$t8 #get average and store to v0
jr $ra # jump back to the main
output
please give me thumb up
Get Answers For Free
Most questions answered within 1 hours.