Question: Implement a MIPS assembly language program that defines main, and function1 procedures. The function1 is recursive and should be defined as: function1 (n) = 2

Implement a MIPS assembly language program that defines "main", and "function1" procedures. The function1 is recursive and should be defined as:

function1 (n) = 2 * n if n <= 4

= n * function1 (n - 2) + function1 (n - 3) + n otherwise

The main asks a user to enter an integer for n and calls the function1 by passing the n value, then prints the result.

------------------------------------------------------------------------------------

So far, I tried to split this question up and for the following simplified C code below, I wrote the MIPS code below it as well. Kindly convert my below MIPS code to the above formula so that it gives the right answer as in the Sample Output.

if ( n <= 2) {

return 2;

} else {

int comp = (-3) * function1(n - 2) + 7 * function1 (n-3) +15;

return comp;

}

}

-----------------------------------------------------------------------------------------------------------

The Sample Output:

Enter an integer: 8 The solution is: 527

---------------------------------------------------------------------------------------------------- .data

prompt1: .asciiz "Enter an integer: " message1: .asciiz "The solution is: " next_line: .asciiz " "

#program code is contained below under .text

.text .globl main #define a global function main

#the program begins execution at main()

############################################################################ # Procedure main # Description: # parameters: # return value: # registers to be used: ############################################################################ main:

#Print "Enter an integer: " la $a0, prompt1 li $v0, 4 syscall

#$v0 = user input li $v0, 5 syscall

#$n = $v0 move $a0, $v0

#Adjust sp/ store ra addi $sp, $sp, -4 sw $ra, 0($sp)

#int function1($a0) jal function1

#Adjust sp/ load ra lw $ra, 0($sp) addi $sp, $sp, 4

#save function1 return value move $s0, $v0 #Print "The solution is:" la $a0, message1 li $v0, 4 syscall

#print ans move $a0, $s0 li $v0, 1 syscall

#print new line la $a0, next_line li $v0, 4 syscall

#end #li $v0, 10 #syscall

############################################################################ # Procedure function1 # Description: ----- # parameters: $a0 = , $a1 = # return value: $v0 = # registers to be used: ############################################################################ function1:

addi $sp, $sp, -12 #Save $ra, $a0, $s1, and $s2 on the stack sw $ra, 0($sp) sw $a0, 4($sp) sw $s1, 8($sp)

slti $t0, $a0, 4 #if ($a0 <3) beq $t0, $zero, else

addi $v0, $zero, 2 #return 2 j return

else: addi $a0, $a0, -2 #intfunction1($a0 - 2) jal function1

addi $t1, $zero, -3 #$s1 = (-3) * function1 ($a0 - 2) mult $t1, $v0 mflo $s1

addi $a0, $a0, -1 #int function1 ($a0 - 3) jal function1

addi $t1, $zero, 7 #t3 = (7) * function1 ($a0 - 3) mult $t2, $v0 mflo $t3

add $t4, $s1, $t3 #$v0 = $t4 + 15 = $s1 + $t3 + 15 = (-3) * function1($a0 - ) + (7) addi $v0, $t4, 15 return:

lw $s1, 8 ($sp) lw $a0, 4 ($sp) lw $ra, 0 ($sp) addi $sp, $sp, 12

jr $ra

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!