Question: . data input: . asciiz Input: . text . globl main main: # Prompt user for input li $v 0 , 4 la $a
data
input: asciiz "Input:
text
globl main
main:
# Prompt user for input
li $v
la $a input
syscall
# Get user input
li $v
syscall
move $a $v
# Call MyfunN
jal Myfun
# Print the result
li $v
syscall
# Exit
li $v
syscall
# Function to recursively calculate MyfunN
Myfun:
# Save the return address and the argument $a which contains N
addi $sp $sp # Allocate stack space for items
sw $ra$sp # Save return address
sw $a$sp # Save argument N
# Base case: if N return
slti $t $a # Check if N
bne $t $zero, basecase # if N branch to end of function
# Recursive case: MyfunNN
# Calculate N and call Myfun recursively
addi $a $a
jal Myfun
move $v $v
# After returning from recursive call, retrieve N and calculate N
lw $t$sp # Load original N
add $t $t $t # $t N
# Add N to the result of the recursive call
add $v $v $t # $v $v $t
basecase:
li $v
j endmyfun
endmyfun:
# Retrieve the original return address and restore the stack
lw $ra$sp # Load the return address
addi $sp $sp # Deallocate stack space
# Return to caller
jr $ra # Jump back to the return address
When I input any number less than it returns the input value, whenever I input it outputs instead of when I input it outputs instead of
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
