Answered step by step
Verified Expert Solution
Link Copied!

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Contemporary Issues In Database Design And Information Systems Development

Authors: Keng Siau

1st Edition

1599042894, 978-1599042893

More Books

Students also viewed these Databases questions

Question

How to dance And to get in shape Xbxbxbc

Answered: 1 week ago