Convert C code into MIPS /*factorial declaration recursive and non-recursive */
ID: 3711235 • Letter: C
Question
Convert C code into MIPS/*factorial declaration recursive and non-recursive */
#include <stdio.h>
//function declaration
int nonRecFact(int n);
int main(){ //variable declaration int n, f; //input printf("Enter n: "); scanf("%d", &n); //recursive fact //non-recursive fact f = nonRecFact(n); printf("Non-Recursive fact: %d ", f); return 0; }
//function definition
int nonRecFact(int n){ int i, f = 1; for(i = 1; i <= n; i++) f *= i; return f; } Convert C code into MIPS
/*factorial declaration recursive and non-recursive */
#include <stdio.h>
//function declaration
int nonRecFact(int n);
int main(){ //variable declaration int n, f; //input printf("Enter n: "); scanf("%d", &n); //recursive fact //non-recursive fact f = nonRecFact(n); printf("Non-Recursive fact: %d ", f); return 0; }
//function definition
int nonRecFact(int n){ int i, f = 1; for(i = 1; i <= n; i++) f *= i; return f; } Convert C code into MIPS
/*factorial declaration recursive and non-recursive */
#include <stdio.h>
//function declaration
int nonRecFact(int n);
int main(){ //variable declaration int n, f; //input printf("Enter n: "); scanf("%d", &n); //recursive fact //non-recursive fact f = nonRecFact(n); printf("Non-Recursive fact: %d ", f); return 0; }
//function definition
int nonRecFact(int n){ int i, f = 1; for(i = 1; i <= n; i++) f *= i; return f; }
Explanation / Answer
.LC0:
.string "Enter n: "
.LC1:
.string "%d"
.LC2:
.string "Non-Recursive fact: %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 nonRecFact(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
nonRecFact(int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-8], 1
mov DWORD PTR [rbp-4], 1
.L5:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-20]
jg .L4
mov eax, DWORD PTR [rbp-8]
imul eax, DWORD PTR [rbp-4]
mov DWORD PTR [rbp-8], eax
add DWORD PTR [rbp-4], 1
jmp .L5
.L4:
mov eax, DWORD PTR [rbp-8]
pop rbp
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.