NEED HELP NOW PLEASE! I WILL GIVE YOU ALL OF MY CHEGG POINTS! GARUNTEED. Impleme
ID: 3751681 • Letter: N
Question
NEED HELP NOW PLEASE!
I WILL GIVE YOU ALL OF MY CHEGG POINTS! GARUNTEED.
Implement a MIPS assembly language program that defines "main", and "function1" procedures.
The function1 is recursive and should be defined as:
function1(n) = (3*n)-5 if n <= 3
= (n-1)*function1(n-1) + function1(n-2) - 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.
HERE IS THE C PROGRAM, PLEASE CONVERT TO MIPS. DO NOT USE C TO MIPS CONVERTER BECAUSE IT NEEDS TO BE ABLE TO RUN ON SPIM.
THANK YOU SO MUCH I NEED IT WITHIN THE NEXT FEW HOURS, AND I SWEAR I WILL GIVE YOU ALL MY CHEGG POINTS.
TEST CASE:
Enter an integer:
8
The solution is: 7842
// The function1 is a recursive procedure/function defined by: // function1(n) = (3*n)-5 if n <= 3 // = (n-1)*function1(n-1) + function1(n-2) - n otherwise. int function1(int n) { if (n <= 3) { int ans1 = (3*n)-5; return ans1; } else { int ans1 = (n-1)*function1(n-1) + function1(n-2) - n; return ans1; } } // The main calls function1 by entering an integer given by a user. void main() { int ans, n; printf("Enter an integer: "); // read an integer from user and store it in "n" scanf("%d", &n); ans = function1(n); // print out the solution computed by function 1 printf("The solution is: %d ", ans); return; }
Explanation / Answer
// MIPS assembly for the give c program
function1(int):
slt $2,$4,4
beq $2,$0,$L2
sll $3,$4,1
addu $2,$3,$4
j $31
addiu $2,$2,-5
$L2:
addiu $sp,$sp,-40
sw $31,36($sp)
sw $17,32($sp)
sw $16,28($sp)
move $17,$4
addiu $16,$4,-1
move $4,$16
jal function1(int)
nop
mult $16,$2
mflo $16
addiu $4,$17,-2
jal function1(int)
nop
addu $2,$16,$2
subu $2,$2,$17
lw $31,36($sp)
lw $17,32($sp)
lw $16,28($sp)
j $31
addiu $sp,$sp,40
$LC0:
.ascii "Enter an integer:"
$LC1:
.ascii "%d"
$LC2:
.ascii "The solution is: %d "
main:
addiu $sp,$sp,-40
sw $31,36($sp)
lui $4,%hi($LC0)
addiu $4,$4,%lo($LC0)
jal puts
nop
addiu $5,$sp,24
lui $4,%hi($LC1)
addiu $4,$4,%lo($LC1)
jal scanf
nop
lw $4,24($sp)
jal function1(int)
nop
move $5,$2
lui $4,%hi($LC2)
addiu $4,$4,%lo($LC2)
jal printf
nop
move $2,$0
lw $31,36($sp)
nop
j $31
addiu $sp,$sp,40
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.