Answered step by step
Verified Expert Solution
Question
1 Approved Answer
5 th TIME POSTING THIS QUESTION!!. PLease HELP ME WITH IT MY output keeps getting 4 . Ive copied an experts answer and they need
th TIME POSTING THIS QUESTION!!. PLease HELP ME WITH IT MY output keeps getting Ive copied an experts answer and they need more information I dont know how to seek help for recursion so I'll say USING MARS Please use it We are required to use recursion. 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.
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
THE GOAL IS to input the integer to get but all the programs that experts have sent me dont work. PLEASE HELP. TO help you I'll copy and paste the code of an example of what an expert did for the coode and an example from the book about recursion. PLease make sure you get theoutput USING MARS and take a screenshot of it
Heres the copy of the code of the expert.
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
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