# array of doubles # this program reads in array elements, prints the elements, # adds up the array elements, finds the max, and prints the total and the max # .data array: .space 40 # array of 5 doubles prompt: .asciiz "Enter an element: " totmsg: .asciiz "\n The total is: " maxmsg: .asciiz "\n The max is: " prmsg: .asciiz "\n The array elements are: " blanks: .asciiz " " .globl main .text main: lui $t0, 0x1001 # $t0 <- base addr of data segment ori $t1, $0, 5 # $t1: size of array sub.d $f4, $f4, $f4 # $f4: sum, sum = 0 ori $t3, $0, 0 # $t3: loop counter, initally 0 move $t4, $t0 # $t4: will contain addr of array element readloop: li $v0, 4 # syscall code for print string la $a0, prompt # load addr of prompt in $a0 syscall # print prompt msg li $v0, 7 # syscall code for read double syscall # read a double s.d $f0, 0($t4) # store it in the array addi $t4, $t4, 8 # addr of next array element addi $t3, $t3, 1 # increment loop counter blt $t3, $t1, readloop # if not done keep looping ori $t3, $0, 0 # reinitialize loop counter to 0 move $t4, $t0 # reinitialize pointer to first array element li $v0, 4 # syscall code for print string la $a0, prmsg # load addr of result in $a0 syscall # print result msg sub.s $f8, $f8, $f8 # $f8 is sum, sum = 0 l.d $f6, 0($t4) # $f6: next array element mov.d $f10, $f6 # $f10 is max, initialize to 1st array element loop: add.d $f8, $f8, $f6 # sum is $f8, add array element to sum mov.d $f12, $f6 # move array element to $f12 li $v0, 3 # code to print a double syscall # print the element li $v0, 4 # syscall code for print string la $a0, blanks # load addr of blanks in $a0 syscall # print blanks c.lt.d $f10, $f6 # max < current array element? bc1f notbig # branch if current <= max mov.d $f10, $f6 # otherwise set new max = current element notbig: addi $t3, $t3, 1 # increment loop counter addi $t4, $t4, 8 # addr of next array element l.d $f6, 0($t4) # $f6: next array element blt $t3, $t1, loop # if not done keep looping li $v0, 4 # syscall code for print string la $a0, totmsg # load addr of totmsg in $a0 syscall # print total msg mov.d $f12, $f8 # move sum to $f12 for printing li $v0, 3 # code to print a double syscall # print the sum li $v0, 4 # syscall code for print string la $a0, maxmsg # load addr of maxmsg in $a0 syscall # print max msg mov.d $f12, $f10 # move max to $f12 for printing li $v0, 3 # code to print a double syscall # print the max li $v0, 10 # terminate execution and syscall # return control to system