Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 until a value of is entered. Return the average of the numbers to the calling program. I am using Mars
This is the code i have currently but its not giving me average:
data
prompt: asciiz "Enter a value between and to stop:
newline: asciiz
invalidmsg: asciiz "Invalid input. Please enter a value between and
text
globl main
main:
# Prepare for loop
li $t # i
li $t # sum
li $t # count
loop:
# Print prompt
li $v
la $a prompt
syscall
# Read input
li $v
syscall
move $t $v # Store input in $t
# Check if input is sentinel value
beq $t endloop
# Check if input is valid
blez $t invalidinput
bgt $t invalidinput
# Add input to sum
add $t $t $t # Add to sum
addi $t $t # Increment count
j loop
endloop:
# Calculate average
beq $t $zero, novalidinputs
div $t $t # Calculate average
# Print average
li $v
move $a $t
syscall
j endprogram
invalidinput:
# Print invalid input message
li $v
la $a invalidmsg
syscall
j loop
novalidinputs:
# Print message for no valid inputs
li $v
la $a newline
syscall
j endprogram
endprogram:
# Exit program
li $v
syscall
This was my IO: Enter a value between and to stop:
Enter a value between and to stop:
Enter a value between and to stop:
Enter a value between and to stop:
Enter a value between and to stop:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started