Convert the following C code into ARMv8 Assembly: #include<stdio.h> int fac(int
ID: 3858898 • Letter: C
Question
Convert the following C code into ARMv8 Assembly:
#include<stdio.h>
int fac(int n);
int main() {
int a,i,fact,sum = 0;
printf("Enter a value is <9 and non negative integer : ");
// Takeing input from the user
scanf("%d",&a);
//for loop 'a' no.of times
for(i=0;i<=a;i++){
fact = fac(i); //fac funtion to calculate the factorial
sum += fact;
}
// Printing the series sum
printf(" The series sum = %d",sum);
}
// fac function
int fac(int n)
{
if (n >= 1)
return n*fac(n-1);
else
return 1;
}
Explanation / Answer
.LC0:
.string "Enter a value is <9 and non negative integer :"
.LC1:
.string "%d"
.LC2:
.string " The series sum = %d"
main:
stp x29, x30, [sp, -32]!
add x29, sp, 0
str wzr, [x29, 24]
adrp x0, .LC0
add x0, x0, :lo12:.LC0
bl puts
add x1, x29, 16
adrp x0, .LC1
add x0, x0, :lo12:.LC1
bl scanf
str wzr, [x29, 28]
.L3:
ldr w0, [x29, 16]
ldr w1, [x29, 28]
cmp w1, w0
bgt .L2
ldr w0, [x29, 28]
bl fac(int)
str w0, [x29, 20]
ldr w1, [x29, 24]
ldr w0, [x29, 20]
add w0, w1, w0
str w0, [x29, 24]
ldr w0, [x29, 28]
add w0, w0, 1
str w0, [x29, 28]
b .L3
.L2:
adrp x0, .LC2
add x0, x0, :lo12:.LC2
ldr w1, [x29, 24]
bl printf
mov w0, 0
ldp x29, x30, [sp], 32
ret
fac(int):
stp x29, x30, [sp, -32]!
add x29, sp, 0
str w0, [x29, 28]
ldr w0, [x29, 28]
cmp w0, wzr
ble .L6
ldr w0, [x29, 28]
sub w0, w0, #1
bl fac(int)
mov w1, w0
ldr w0, [x29, 28]
mul w0, w1, w0
b .L7
.L6:
mov w0, 1
.L7:
ldp x29, x30, [sp], 32
ret
I am hoping its self undestandable, the program is clearly writtent o be self understandable even with little proficiency in ARM. If not, you just need an arm instruction table to understand, which is easily available everywhere on internet. Explaining every instruction is not quite possible here.