Question
Can someone tell me if I have errors in my code? Also please code the average: .data prompt1: .asciiz Enter the number of values:
Can someone tell me if I have errors in my code? Also please code the average:
.data
prompt1: .asciiz "Enter the number of values: "
prompt2: .asciiz "Enter an integer value: "
error: .asciiz "Error: n should be between 1 and 10 inclusive."
comma: .asciiz ", "
result: .asciiz "The even values are: "
avg: .asciiz "The average of the even values is: "
newline: .asciiz " "
.text
main:
# print prompt1 and read n
li $v0, 4 # system call code for print string
la $a0, prompt1 # load address of prompt1
syscall # print prompt1
li $v0, 5 # system call code for read integer
syscall # read integer into $v0
# check if n is between 1 and 10
blt $v0, 1, error # if n
bgt $v0, 10, error # if n > 10, go to error
# initialize variables for sum and count
li $t0, 0 # sum = 0
li $t1, 0 # count = 0
# read n values
loop:
# print prompt2 and read a value
li $v0, 4 # system call code for print string
la $a0, prompt2 # load address of prompt2
syscall # print prompt2
li $v0, 5 # system call code for read integer
syscall # read integer into $v0
# check if the value is even
andi $t2, $v0, 1 # t2 = v0 & 1
beq $t2, 0, add # if t2 == 0, the value is even, go to add
addi $t1, $t1, 1 # increment count
b loop # go to loop
add:
add $t0, $t0, $v0 # add the even value to sum
addi $t1, $t1, 1 # increment count
b loop # go to loop
# print the even values, if any
beq $t1, 0, no_even # if count == 0, there are no even values, go to no_even
li $v0, 4 # system call code for print string
la $a0, result # load address of result
syscall # print result
li $t1, 0 # initialize counter
print_loop:
li $v0, 1 # system call code for print integer
lw $a0, ($t0) # load the value from sum
syscall # print the value
addi $t1, $t1, 1 # increment counter
beq $t1, $v0, no_comma # if counter == n, there is no comma, go to no_comma
no_comma:
li $v0, 4 # system call code for print string
la $a0, comma # load address of comma
syscall # print comma
b print_loop # go to print_loop
Write the following in MIPS assembly and match the output exactly as shown below The program should prompt the user and read an integer value indicating n, which is the number of values to be entered. If the n value is not between 1 and 10 , then you should print an appropriate error message and exit. Otherwise, you should prompt the user and read each of the n values. You should then print the values that were even, separated by com-mas. If one or more values were even, then print the average of the even values. Enter the number of values: 5 Enter 5 integer values, one per line. 2 7 10 4 The even numbers are: 2,10,4 The average of the even numbers is: 2.66666675
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