0

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.

nomaddo
  • 416
  • 3
  • 12
  • 2
    [Stack allocation, why the extra space?](http://stackoverflow.com/q/9862017/995714), [Why does the compiler allocate more than needed in the stack?](http://stackoverflow.com/q/37770751/995714), [Why gcc disassembler allocating extra space for local variable?](http://stackoverflow.com/q/20865235/995714), [Why more memory is assigned to a local variable in stack than required in C++?](http://stackoverflow.com/q/18717162/995714) – phuclv Mar 06 '17 at 03:41
  • the x86 and arm backends are two separate things. – old_timer Mar 06 '17 at 13:37
  • 1
    `sub sp, sp, #12` "allocates" 12 bytes, or 3 words (ARM word = 32 bits = 4 bytes). 1 byte = 8 bits on pretty much everything what you can currently meet in real world (aside some museum or intentional obscurity). – Ped7g Mar 06 '17 at 14:04
  • 1
    Also, [Why AACPS requires 8 byte stack alignment](http://stackoverflow.com/questions/25282466/why-procedure-call-standard-for-the-arm-architecture-aapcs-requires-sp-to-be). Indeed making `succ` static changes things considerably. – artless noise Mar 06 '17 at 15:48
  • Making it `static` gives the compiler the hint that it will not call external functions and a stack frame is not needed (to save `lr`). The compiler also tries to keep your array 64bit aligned. The array is more efficient if access starts at 64bits. If you are concerned, compile with `-Os` or use other code generation options. – artless noise Mar 06 '17 at 16:57

1 Answers1

0

I saw something like this the other day too. I suspect that what is going on is that it has put lr on the stack. It needs to align that to a 64 bit boundary so it needs 4 more bytes on the stack. Then it needs 4 bytes for a, and needs to align that so adds 4 bytes on the stack, not realizing that lr plus a was all they needed to allocate.

I will have to see if I can reproduce this differently from how you are causing it.

old_timer
  • 69,149
  • 8
  • 89
  • 168