ASSIGNMENT DESCRIPTION: Implement a MIPS assembly language program that defines
ID: 3889253 • Letter: A
Question
ASSIGNMENT DESCRIPTION: Implement a MIPS assembly language program that defines "main", and "function1" procedures. (PLEASE DO NOT PASTE THE BELOW C CODE INTO A MIPS COMPILER. I NEED THE PROGRAM TO WORK FOR SPIM) 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. If your program causes an infinite loop, press Control and 'C' keys at the same time to stop it.
This is how the program looks in C:
// The function1 is a recursive procedure defined by:
// function1(n) = 2*n if n <= 4
// = n*function1(n-2) + function1(n-3) + n otherwise.
int function1(int n)
{
if (n <= 4)
{
return 2*n;
}
else
{
int comp = n*function1(n-2) + function1(n-3) + n;
return comp;
}
}
// 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;
}
SAMPLE OUTPUT:
Enter an integer:
8
The solution is: 527
--------------------------------------------------
Each procedure needs to have a header using the following format:
############################################################################
# Procedure findMax
# Description: -----
# parameters: $a0 = address of array, $a1 = length
# return value: $v0 = max
# registers to be used: $s3 and $s4 will be used.
############################################################################
Explanation / Answer
function1(int):
push rbp
mov rbp, rsp
push rbx
sub rsp, 40
mov DWORD PTR [rbp-36], edi
cmp DWORD PTR [rbp-36], 4
jg .L2
mov eax, DWORD PTR [rbp-36]
add eax, eax
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-36]
sub eax, 2
mov edi, eax
call function1(int)
imul eax, DWORD PTR [rbp-36]
mov ebx, eax
mov eax, DWORD PTR [rbp-36]
sub eax, 3
mov edi, eax
call function1(int)
lea edx, [rbx+rax]
mov eax, DWORD PTR [rbp-36]
add eax, edx
mov DWORD PTR [rbp-20], eax
mov eax, DWORD PTR [rbp-20]
.L3:
add rsp, 40
pop rbx
pop rbp
ret
.LC0:
.string "Enter an integer: "
.LC1:
.string "%d"
.LC2:
.string "The solution is: %d "
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rax, [rbp-8]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call scanf
mov eax, DWORD PTR [rbp-8]
mov edi, eax
call function1(int)
mov DWORD PTR [rbp-4], eax
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
mov eax, 0
leave
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.