Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program to find y = x^n using recursion, where x and n are non-zero po

ID: 3854158 • Letter: W

Question

Write a C program to find y = x^n using recursion, where x and n are non-zero positive values less than 10. The user will input the value of x and n using scanf. The output can be printed using printf. Upload the C program to Blackboard. Convert the C program to LEGv8 assembly code. The scanf and printf functions does not have to be converted to assembly code. Do not use any predefined functions in your C code or assembly code. Write the assembly code in your homework document. Comment your C code and assembly code.

Explanation / Answer


#include <iostream>
#include <stdio.h>
using namespace std;
int pow(int x, int n)
{
int count = 1, sum = 1; //Initialization

/* Loop Begins */
while(count<=n) // condition checks
{
sum = sum * x;
count++; //Incrementing of values
} /* Loop Ends*/
return sum; // return the value of sum
}
int main()
{
int x, n; // Declaration of variable
printf(" Enter the value of x: "); // Asking user to give the value of x   
scanf("%d", &x); // receving value for x
printf(" Enter the value of n: ");
scanf("%d", &n);
int sum = pow(x,n); // function call
printf(" The Power of x = %d, n = %d sum = %d", x, n, sum); // displaying the output
return 0;
}


OUTPUT


Enter the value of x:
Enter the value of n:
The Power of x = 2, n = 4
sum = 16
-----------------------------------------------------------------------------------------------------------------------------------------------

pow(int, int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-24], esi
mov DWORD PTR [rbp-4], 1
mov DWORD PTR [rbp-8], 1
.L3:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-24]
jg .L2
mov eax, DWORD PTR [rbp-8]
imul eax, DWORD PTR [rbp-20]
mov DWORD PTR [rbp-8], eax
add DWORD PTR [rbp-4], 1
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-8]
pop rbp
ret
.LC0:
.string " Enter the value of x: "
.LC1:
.string "%d"
.LC2:
.string " Enter the value of n: "
.LC3:
.string " The Power of x = %d, n = %d sum = %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 edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
lea rax, [rbp-12]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call scanf
mov edx, DWORD PTR [rbp-12]
mov eax, DWORD PTR [rbp-8]
mov esi, edx
mov edi, eax
call pow(int, int)
mov DWORD PTR [rbp-4], eax
mov edx, DWORD PTR [rbp-12]
mov eax, DWORD PTR [rbp-8]
mov ecx, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC3
mov eax, 0
call printf
mov eax, 0
leave
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L9
cmp DWORD PTR [rbp-8], 65535
jne .L9
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L9:
nop
leave
ret
_GLOBAL__sub_I__Z3powii:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote