Convert this C Code to an ARMv8 Code program: #include<stdio.h> long long int fa
ID: 3866016 • Letter: C
Question
Convert this C Code to an ARMv8 Code program:
#include<stdio.h>
long long int factorial(int n) //function to calculate factorial of integer numbers
{
if(n==0 || n==1) //factorials of 1 n 0 are 1
{
return 1;
}
return n*factorial(n-1); //using recursion
}
float power(float number,int p) //power function for number raised to integer power p.
{
if (p==0)
{
return 1; //zero power results in 1.
}
if(p==1)
{
return number;
}
return number*power(number,p-1);
}
int main()
{
double x,result ; //user input x and result variable for calculation
long long int a; //64 bit user input no. a
int n; //loop counter variable
printf("Enter a value of X between -5 and 5: "); //getting user input for x
scanf("%lf",&x); //storing x in double
while(x>5 || x<-5)
{
printf("Please enter a new value of X between -5 and 5: "); //getting user input for x
scanf("%lf",&x); //storing x in double
}
printf("Enter the value of A less than 16: "); //getting user input for a
scanf("%lli",&a); //storing a in long long int
while(a>16)
{
printf("Please enter a new value of A less than 16: "); //getting user input for a
scanf("%lli",&a); //storing a in long long int
}
for(n=0;n<=a;n++) //loop for carrying out summation from n=0 to n=a.
{
result=result+(double)(power(x,n)/factorial(n)); //calculating (x^n)/n! for each n and adding to previous summation
}
printf("The exponential value is: %lf ",result); //printing the value after summation.
return 0;
}
Explanation / Answer
ARM gcc 5.4 code:
factorial(int):
stp x29, x30, [sp, -48]!
add x29, sp, 0
str x19, [sp, 16]
str w0, [x29, 44]
ldr w0, [x29, 44]
cmp w0, wzr
beq .L2
ldr w0, [x29, 44]
cmp w0, 1
bne .L3
.L2:
mov x0, 1
b .L4
.L3:
ldrsw x19, [x29, 44]
ldr w0, [x29, 44]
sub w0, w0, #1
bl factorial(int)
mul x0, x19, x0
.L4:
ldr x19, [sp, 16]
ldp x29, x30, [sp], 48
ret
power(float, int):
stp x29, x30, [sp, -32]!
add x29, sp, 0
str s0, [x29, 28]
str w0, [x29, 24]
ldr w0, [x29, 24]
cmp w0, wzr
bne .L6
fmov s0, 1.0e+0
b .L7
.L6:
ldr w0, [x29, 24]
cmp w0, 1
bne .L8
ldr s0, [x29, 28]
b .L7
.L8:
ldr w0, [x29, 24]
sub w0, w0, #1
ldr s0, [x29, 28]
bl power(float, int)
fmov s1, s0
ldr s0, [x29, 28]
fmul s0, s1, s0
.L7:
ldp x29, x30, [sp], 32
ret
.LC0:
.string "Enter a value of X between -5 and 5: "
.LC1:
.string "%lf"
.LC2:
.string "Please enter a new value of X between -5 and 5: "
.LC3:
.string "Enter the value of A less than 16: "
.LC4:
.string "%lli"
.LC5:
.string "Please enter a new value of A less than 16: "
.LC6:
.string "The exponential value is: %lf "
main:
stp x29, x30, [sp, -64]!
add x29, sp, 0
str d8, [sp, 16]
adrp x0, .LC0
add x0, x0, :lo12:.LC0
bl printf
add x1, x29, 40
adrp x0, .LC1
add x0, x0, :lo12:.LC1
bl scanf
.L12:
ldr d1, [x29, 40]
fmov d0, 5.0e+0
fcmpe d1, d0
cset w0, gt
uxtb w0, w0
eor w0, w0, 1
uxtb w0, w0
cmp w0, wzr
beq .L10
ldr d1, [x29, 40]
fmov d0, -5.0e+0
fcmpe d1, d0
cset w0, mi
uxtb w0, w0
eor w0, w0, 1
uxtb w0, w0
cmp w0, wzr
bne .L11
.L10:
adrp x0, .LC2
add x0, x0, :lo12:.LC2
bl printf
add x1, x29, 40
adrp x0, .LC1
add x0, x0, :lo12:.LC1
bl scanf
b .L12
.L11:
adrp x0, .LC3
add x0, x0, :lo12:.LC3
bl printf
add x1, x29, 32
adrp x0, .LC4
add x0, x0, :lo12:.LC4
bl scanf
.L14:
ldr x0, [x29, 32]
cmp x0, 16
ble .L13
adrp x0, .LC5
add x0, x0, :lo12:.LC5
bl printf
add x1, x29, 32
adrp x0, .LC4
add x0, x0, :lo12:.LC4
bl scanf
b .L14
.L13:
str wzr, [x29, 52]
.L16:
ldrsw x1, [x29, 52]
ldr x0, [x29, 32]
cmp x1, x0
bgt .L15
ldr d0, [x29, 40]
fcvt s0, d0
ldr w0, [x29, 52]
bl power(float, int)
fmov s8, s0
ldr w0, [x29, 52]
bl factorial(int)
scvtf s0, x0
fdiv s0, s8, s0
fcvt d0, s0
ldr d1, [x29, 56]
fadd d0, d1, d0
str d0, [x29, 56]
ldr w0, [x29, 52]
add w0, w0, 1
str w0, [x29, 52]
b .L16
.L15:
adrp x0, .LC6
add x0, x0, :lo12:.LC6
ldr d0, [x29, 56]
bl printf
mov w0, 0
ldr d8, [sp, 16]
ldp x29, x30, [sp], 64
ret