Answered step by step
Verified Expert Solution
Link Copied!

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

5th TIME POSTING THIS QUESTION!!. PLease HELP ME WITH IT MY output keeps getting 4. 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 4.5(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 1 to n, and prints the result.
For example, if the user entered 6, the program should add 1+2+3+4+5+6 and output 21.
Make sure to have prompts for user input and output.
SUBMIT:
1).asm file
2) Output of the program with user input of 7.
THE GOAL IS to input the integer 7 to get 28 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 28 USING MARS 4.5 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 1 to n is: "
.text
.globl main
main:
# Print prompt
li $v0,4
la $a0, prompt
syscall
# Read integer from user
li $v0,5
syscall
move $a0, $v0 # Move the read integer to $a0 for recursion function
# Call the recursive function
jal sum_recursion
# Print result prompt
li $v0,4
la $a0, result
syscall
# Print the result
move $a0, $v0 # Result of recursion is in $v0, move it to $a0
li $v0,1
syscall
# Exit program
li $v0,10
syscall
# Recursive function to calculate sum from 1 to n
sum_recursion:
# Base case: if n =0, return 0
blez $a0, base_case
# Recursive case: return n + sum_recursion(n -1)
addi $sp, $sp,-8 # Allocate space on stack for return address and argument
sw $ra,0($sp) # Store return address
sw $a0,4($sp) # Store current argument n
addi $a0, $a0,-1 # Decrement n
jal sum_recursion # Recursive call
lw $a0,4($sp) # Load the original n
lw $ra,0($sp) # Restore return address
addi $sp, $sp,8 # Deallocate stack space
add $v0, $v0, $a0 # Add n to the result of recursion
jr $ra # Return to caller
base_case:
li $v0,0 # Load 0 into $v0 for base case result
jr $ra # Return to caller
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions