Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NO PSEUDO INSTRUCTION Write and test a MIPS program consisting of four functions

ID: 3738951 • Letter: N

Question

NO PSEUDO INSTRUCTION

Write and test a MIPS program consisting of four functions. & means "address of". In the following descriptions, the symbol 1. void main0: The main function must 1) print your name 2) call the readData function 3) call count function 4) complete the program. The main function must set up all parameters before calling each of the functions. 2. int readData (&array;): The starting address of an array is passed to the function as a parameter using Sa0. The function must prompt for and read and store integers in the array until either a zero is entered or 10 numbers are read. Once the tenth integer is entered, your program must stop reading. The function must return (using Sv0) the number of values read and stored in the array (10 or less). The zero input is not part of the array and must not be stored in the array. If the first input is a zero, then the array will be empty and the count returned is zero.

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


.data
namemsg: .asciiz "Linda chatting"
prompt: .asciiz "Enter a number(0 to stop): "
arr: .word 0:10
len: .word 0
avgMsg: .asciiz "The average is "
greatMsg1: .asciiz " There are "
greatMsg2: .asciiz " numbers greater than average"
.text
#display name


#set up parameters and call readData
la $a0, arr
jal readData
sw $v0, len #store the return value in len

#set up parameters and call count
la $a0, arr
lw $a1, len
jal count

#exit
li $v0, 10
syscall

#----------------------------
readData:
li $t0, 0 #counter
move $t1, $a0
loopReadData:
bgt $t0, 10, endReadData

#prompt and read int
li $v0, 4
la $a0, prompt
syscall


#read int and store in $t0
li $v0, 5
syscall
beqz $v0, endReadData #is the num entered 0, then stop

sw $v0, ($t1) #store num in array loc
add $t1, $t1, 4 #next location
add $t0, $t0, 1 #increment counter
b loopReadData

endReadData:
#return the count
move $v0, $t0
jr $ra


#----------------------------
count:
move $t0, $a0
move $t1, $a1

li $t2, 0 #counter
move $t3, $a0 #current array loc
mtc1 $t2, $f0 #sum

#find the sum
sumLoop:
bgt $t2, $t1, endSumLoop
lwc1 $f2, ($t3) #load the current int
cvt.s.w $f2, $f2 #conver t to single precision float
add.s $f0, $f0, $f2 #add to sum
add $t3, $t3, 4 #next loc
add $t2, $t2, 1 #increment counter
b sumLoop

endSumLoop:
#divide by count to get avg
mtc1 $a1, $f2
cvt.s.w $f2, $f2
div.s $f0, $f0, $f2 #averate in $f0

#display the average
li $v0, 4
la $a0, avgMsg
syscall

li $v0, 2
mov.s $f12, $f0
syscall

#count num greater than avg
li $t2, 0
move $t3, $t0 #current array loc
li $t4, 0 #initialize count of no. greater than avg
countLoop:
bgt $t2, $t1, endCountLoop
lwc1 $f2, ($t3) #load the current int
cvt.s.w $f2, $f2 #conver t to single precision float
c.le.s $f2, $f0 #compare the current num with avg
bc1t next #go to next if its <= avg
add $t4, $t4, 1 #count this number as it is greater than avg
next:
add $t2, $t2, 1 #increment counter
add $t3, $t3, 4 #next location
b countLoop
  
endCountLoop:   
#display hte results
li $v0, 4
la $a0, greatMsg1
syscall

li $v0, 1
move $a0, $t4
syscall

li $v0, 4
la $a0, greatMsg2
syscall
jr $ra

#----------------------------

output
Enter a number(0 to stop): 3
Enter a number(0 to stop): 6
Enter a number(0 to stop): 2
Enter a number(0 to stop): 4
Enter a number(0 to stop): 0
The average is 3.75
There are 2 numbers greater than average
-- program is finished running --