Question: THe code I did is wrong and wont give me the correct answer. IM USING MIPS 4 . 5 THis is the 4 th time
THe code I did is wrong and wont give me the correct answer. IM USING MIPS THis is the th time asking again Write a MIPS program that asks the user for an integer n uses a recursive function to find the sum of all numbers from to n and prints the result. Enter an integer n: The sum of numbers from to is:
program is finished running
Enter an integer :
Enter an integer n :
Enter an integer n: Enter an integer:
The sum of numbers from to is:
Clear
program is finished running
Enter an integer:
Enter an integer n: The sum of numbers from to n is:
The sum of numbers from to n is: program is finished running
For example, if the user entered the program should add and output
Make sure to have prompts for user input and output.
SUBMIT:
asm file
Output of the program with user input of So
Heres my code that I got from someone. PLEASE MAKE SURE YOU CONFIRM IT USING THE MIPS PROGRAM MARS and show the screenshot plese output please. I have a picture of what I keep getting and the picture of the question.
data
prompt: asciiz "Enter an integer n:
result: asciiz "The sum of numbers from to n is:
text
globl main
main:
# Print prompt
li $v
la $a prompt
syscall
# Read integer from user
li $v
syscall
move $a $v # Move the read integer to $a for recursion function
# Call the recursive function
jal sumrecursion
# Print result prompt
li $v
la $a result
syscall
# Print the result
move $a $v # Result of recursion is in $v move it to $a
li $v
syscall
# Exit program
li $v
syscall
# Recursive function to calculate sum from to n
sumrecursion:
# Base case: if n return
blez $a basecase
# Recursive case: return n sumrecursionn
addi $sp $sp # Allocate space on stack for return address and argument
sw $ra$sp # Store return address
sw $a$sp # Store current argument n
addi $a $a # Decrement n
jal sumrecursion # Recursive call
lw $a$sp # Load the original n
lw $ra$sp # Restore return address
addi $sp $sp # Deallocate stack space
add $v $v $a # Add n to the result of recursion
jr $ra # Return to caller
basecase:
li $v # Load into $v for base case result
jr $ra # Return to caller
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
