Convert the source code to ARM Assembly Code: int a = 5; int const one = 1; void
ID: 3811696 • Letter: C
Question
Convert the source code to ARM Assembly Code:
int a = 5;
int const>
void main()
int b = 30;
int c = 75;
int d;
int array[1] = {7};
d= a + b + c + one;
array = new int [10]; //dynamically allocating size
array[4] = d;
array[8] = a;
array[2] = c;
a = a + 15;
if( a>0 )
{
a=1;
}
else
{
c = factorial( b );
}
for( int f=0; f<4; f=f+1; )
{
a = a+1;
d = b + c;
}
d = factorial (a);
int factorial(int n)
{
if(n<1)
return 1;
else
return ( n * factorial(n-1) );
}
Explanation / Answer
ARM CODE-
main:
stp x29, x30, [sp, -96]!
add x29, sp, 0
adrp x0, __stack_chk_guard
add x0, x0, :lo12:__stack_chk_guard
ldr x1, [x0]
str x1, [x29, 88]
mov x1,0
mov w0, 5
str w0, [x29, 24]
mov w0, 1
str w0, [x29, 36]
mov w0, 30
str w0, [x29, 40]
mov w0, 75
str w0, [x29, 28]
ldr w1, [x29, 24]
ldr w0, [x29, 40]
add w1, w1, w0
ldr w0, [x29, 28]
add w0, w1, w0
add w0, w0, 1
str w0, [x29, 44]
ldr w0, [x29, 44]
str w0, [x29, 64]
ldr w0, [x29, 24]
str w0, [x29, 80]
ldr w0, [x29, 28]
str w0, [x29, 56]
ldr w0, [x29, 24]
add w0, w0, 15
str w0, [x29, 24]
ldr w0, [x29, 24]
cmp w0, 0
ble .L2
mov w0, 1
str w0, [x29, 24]
b .L3
.L2:
ldr w0, [x29, 40]
bl factorial(int)
str w0, [x29, 28]
.L3:
str wzr, [x29, 32]
.L5:
ldr w0, [x29, 32]
cmp w0, 3
bgt .L4
ldr w0, [x29, 24]
add w0, w0, 1
str w0, [x29, 24]
ldr w1, [x29, 40]
ldr w0, [x29, 28]
add w0, w1, w0
str w0, [x29, 44]
ldr w0, [x29, 32]
add w0, w0, 1
str w0, [x29, 32]
b .L5
.L4:
ldr w0, [x29, 24]
bl factorial(int)
str w0, [x29, 44]
mov w0, 0
adrp x1, __stack_chk_guard
add x1, x1, :lo12:__stack_chk_guard
ldr x2, [x29, 88]
ldr x1, [x1]
eor x1, x2, x1
cmp x1, 0
beq .L7
bl __stack_chk_fail
.L7:
ldp x29, x30, [sp], 96
ret
factorial(int):
stp x29, x30, [sp, -32]!
add x29, sp, 0
str w0, [x29, 28]
ldr w0, [x29, 28]
cmp w0, 0
bgt .L9
mov w0, 1
b .L10
.L9:
ldr w0, [x29, 28]
sub w0, w0, #1
bl factorial(int)
mov w1, w0
ldr w0, [x29, 28]
mul w0, w1, w0
.L10:
ldp x29, x30, [sp], 32
ret