What is the behavior of the program below as far as stack usage is concerned. We use inner blocks to lexically isolate variable and to also force the function main to release stack space after we exit a particular scope. This is necessary as we start the < real > program by calling a function pointer stored in ROM, we want the function to basically have acess to as much stack as it needs. I am little worried about this code, as I am not really sure that GCC behaves as I expect it to especially with optimization turned on. I expect that when enterring the second inner block where I referred to r3, stack usage is just 2 bytes for the loop variable. Can I assume that GCC will put locally declared variable withing braces on the stack and pop them as we leave the scope ?
The assembley language does not look like what I was expecting, it seems like main uses 72 bytes of stack which is consistent with the fact all the locally declared variables within main are not popped off the stack between inner blocks. Am I understanding this correctly ? typedef unsigned char uint8_t; volatile uint8_t r1[20]; int main(void) { volatile uint8_t i = 0; for (i=0;i<20;i++) { r1[i] = i; } { volatile uint8_t r2[20]; r2[19] = 0x34; } { volatile uint8_t r3[50]; r3[49] = 45; } //Start real program. Boot_rom->start() ; } .file "local.c" .arch msp430x1132 .text .p2align 1,0 .global main .type main,@function /*********************** * Function `main' ***********************/ main: /* prologue: frame size = 72 */ .L__FrameSize_main=0x48 .L__FrameOffset_main=0x4c mov #(__stack-72), r1 mov r1,r4 /* prologue end (size=3) */ mov.b #llo(0), @r4 mov.b #llo(0), @r4 .L2: cmp.b #llo(20), @r4 jlo .L5 jmp .L3 .L5: mov.b @r4, r15 and.b #-1,r15 mov.b @r4, _r1(r15) add.b #llo(1), @r4 jmp .L2 .L3: mov.b #llo(52), 21(r4) mov.b #llo(45), 71(r4) /* epilogue: frame size=72 */ add #72, r1 br #__stop_progExec__ /* epilogue end (size=4) */ /* function main size 31 (24) */ .Lfe1: .size main,.Lfe1-main /********* End of function ******/