The recorded temperatures in London during a period of 15 days was 18 degrees Celsius, 25 degrees Celsius,34 degrees Celsius,22 degrees Celsius,2 degrees Celsius, 43 degrees Celsius,30 degrees Celsius,20 degrees Celsius,08 degrees Celsius, 28 degrees Celsius, 24 degrees Celsius, 27 degrees Celsius, 40 degrees Celsius,29 degrees Celsius and 03 degrees Celsius. Write a program using MIPS to calculate the average temperature of the country during the month of December.
.data
array: .word 18, 25, 34, 22, 2, 43, 30, 20, 8
.word 28, 24, 27, 40, 29, 3
days: .word 15
average: .word 0
.text
.globl main
main:
# Loop through the array to calculate sum
la $t0, array # array starting address
li $t1, 0 # loop index, i=0
lw $t2, days # days
li $t3, 0 # initialize sum=0
sumLoop:
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, sumLoop # if i<days, continue
# Calculate average
div $t5, $t3, $t2 # average = sum / length
sw $t5, average
li $v0, 1 # print integer
move $a0,$t5 # $a0 = sum
syscall
# Done, terminate program.
li $v0, 10 # terminate
syscall # system call
Comments
Leave a comment