Question: I am trying to create a mips program that prompts a user to enter values from 0 . . 1 0 0 until a value

I am trying to create a mips program that prompts a user to enter values from 0..100 until a value of -1 is entered. Return the average of the numbers to the calling program. I am using Mars 4.5
This is the code i have currently but its not giving me average:
.data
prompt: .asciiz "Enter a value between 0 and 100(-1 to stop): "
newline: .asciiz "
"
invalid_msg: .asciiz "Invalid input. Please enter a value between -1 and 100.
"
.text
.globl main
main:
# Prepare for loop
li $t0,0 # i =0
li $t1,0 # sum =0
li $t2,0 # count =0
loop:
# Print prompt
li $v0,4
la $a0, prompt
syscall
# Read input
li $v0,5
syscall
move $t3, $v0 # Store input in $t3
# Check if input is sentinel value
beq $t3,-1, end_loop
# Check if input is valid
blez $t3, invalid_input
bgt $t3,100, invalid_input
# Add input to sum
add $t1, $t1, $t3 # Add to sum
addi $t2, $t2,1 # Increment count
j loop
end_loop:
# Calculate average
beq $t2, $zero, no_valid_inputs
div $t1, $t2 # Calculate average
# Print average
li $v0,1
move $a0, $t1
syscall
j end_program
invalid_input:
# Print invalid input message
li $v0,4
la $a0, invalid_msg
syscall
j loop
no_valid_inputs:
# Print message for no valid inputs
li $v0,4
la $a0, newline
syscall
j end_program
end_program:
# Exit program
li $v0,10
syscall
This was my I/O: Enter a value between 0 and 100(-1 to stop): 5
Enter a value between 0 and 100(-1 to stop): 5
Enter a value between 0 and 100(-1 to stop): 5
Enter a value between 0 and 100(-1 to stop): 5
Enter a value between 0 and 100(-1 to stop): -1
20

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!