I need help with this code please its supposed to take 'n' numbers and take the average of all of the numbers in MIps assembly code the problem is that it is not working here is the code: .dataprompt: .asciiz "Enter the number of real numbers: ".text.globl mainmain:li $v0, 4la $a0, promptsyscallli $v0, 5syscallmove $t0, $v0li $t1, 0loop:beq $t0, $t1, endli $v0, 6syscalladdi $t1, $t1, 1j loopend:li $v0, 10syscall # Initialize $t0 to hold the sumadd $t0, $zero, $zero # Initialize $t1 to hold the countadd $t1, $zero, $zero # Start the looploop2: # Load the current number lw $t2, 0($a0) # Add the current number to the sum add $t0, $t0, $t2 # Increment the counter addi $t1, $t1, 1 # Move to the next number addi $a0, $a0, 4 # Continue the loop if there are more numbers bne $t1, $a1, loop2 # Calculate the average div $t0, $t1 # Move the result to $v0, mflo $v0
I need help with this code please its supposed to take 'n' numbers and take the average of all of the numbers in MIps assembly code
the problem is that it is not working
here is the code:
.data
prompt: .asciiz "Enter the number of real numbers: "
.text
.globl main
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $t0, $v0
li $t1, 0
loop:
beq $t0, $t1, end
li $v0, 6
syscall
addi $t1, $t1, 1
j loop
end:
li $v0, 10
syscall
# Initialize $t0 to hold the sum
add $t0, $zero, $zero
# Initialize $t1 to hold the count
add $t1, $zero, $zero
# Start the loop
loop2:
# Load the current number
lw $t2, 0($a0)
# Add the current number to the sum
add $t0, $t0, $t2
# Increment the counter
addi $t1, $t1, 1
# Move to the next number
addi $a0, $a0, 4
# Continue the loop if there are more numbers
bne $t1, $a1, loop2
# Calculate the average
div $t0, $t1
# Move the result to $v0
, mflo $v0
Step by step
Solved in 2 steps