Question: COMPLETE THIS MIPS CODE, THE RESULTS SHOULD BE 3, 11, 36, 112, 341 # an example of recursive function # f(n) = 3*f(n-1)+n for n>=2,

COMPLETE THIS MIPS CODE, THE RESULTS SHOULD BE 3, 11, 36, 112, 341

# an example of recursive function # f(n) = 3*f(n-1)+n for n>=2, f(1) = 3 # Compute f(n) recursively. # Results: f(1) = 3, f(2) = 11, f(3)=36, f(4) = 112, f(5) = 341

.globl main

# Prompt for a non-negative integer # and invoke this recursive function.

main: addi $sp, $sp, -4 # Make space on stack. sw $ra, 0($sp) # Save return address.

la $a0, prompt li $v0, 4 syscall # Display prompt.

li $v0, 5 syscall # Get integer response.

move $a0, $v0 # Call factorial function. jal recursive

move $a0, $v0 li $v0, 1 syscall # Print integer result.

la $a0, endl li $v0, 4 syscall # Print endl.

li $v0, 0 # Return zero. lw $ra, 0($sp) # Restore return address. addi $sp, $sp, 4 # Restore stack pointer. li $v0, 10 # system call for exit syscall # we are out of here.

.data

prompt: .asciiz "Enter a non-negative integer: " endl: .asciiz " " space: .asciiz " " .text .globl recursive

# Preconditions: # 1st parameter (a0) non-negative integer, n # Postconditions: # result (v0) f(n)

recursive: addi $sp, $sp, -8 # Make space on stack. sw $ra, 0($sp) # Save return address.

lw $ra, 0($sp) # Restore return address. addi $sp, $sp, 8 # Restore stack pointer. jr $ra # Return.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!