I am trying to read the output of gcc written in ARM assembly.
The following output seems strange to me.
int succ (int *arr)
{
return arr[0] + 1;
}
int main (int argc)
{
int a[1] = { argc };
return succ (a);
}
By compilation command gcc -fno-inline -O2 -S
, the following assembly program is generated.
I omit the out put about succ
because it is not related to my question (just for stop elimination of unused variable a
).
.global main
.type main, %function
main:
@ args = 0, pretend = 0, frame = 8
@ frame_needed = 0, uses_anonymous_args = 0
str lr, [sp, #-4]!
sub sp, sp, #12
add r3, sp, #8
str r0, [r3, #-4]!
mov r0, r3
bl succ
add sp, sp, #12
@ sp needed
ldr pc, [sp], #4
.size main, .-main
This main
function seem to allocate 3 byte for local array a
(sub sp, sp, #12).
But my C program allocate just one element of int for a
.
Other temporal values don't seem to use stack.
Why this program require more stack volume than I expected?
My gcc version is 4.8.5. I tried in raspbian in rasberry-pi 3.
UPDATED
When I modified the length of array a
to 2 (int a[2} = {argc, argc}
), the volume doesn't changed (just 3 byte).
For length 3 (int a[3} = {argc, argc, argc}
), 5 byte are allocated in stack.
It is curious to me.