when i'm mixing c code with assembly for cortex m3 processor, I have something that i do not fully understand.
My code behave like this:
volatile int r=8;
and compliant to the abi standard, a,b are mapped into registers and the r variable into stack. So my dissasebly code for the allocation of r variable is:
0x00000196 2008 MOVS r0,#0x08
0x00000198 9000 STR r0,[sp,#0x00]
Inside my assembly function i want to push something to the stack, for example to pass them into my main function. Lets say i push r3 and r4 so my stack
at the end of my assembly_fuction contains r3,r4 and the r variable before starting the function that now is at r0.
when i come back to my main.c, the return variable is saved into r0 (abi std) so my compiler now update the new value of r variable inside the stack, but in this way:
0x000001A2 9000 STR r0,[sp,#0x00]
So actually overwriting the r3 value.
Why the compiler is not smart enough to know that into the stack was pushed 2 variable so the correct form for update r variable is str,r0, [sp #0x08]?
So my problem is, how can i understand without actually debugging, that where my compiler puts the volatile variable? In this example the flow is very easy, but
in some others i could have many volatile variable with lots of push/pops of the stack. I'd like to know more about this behavior.
Thank you so much
Sure, the stack pointer _after_ a function call must be the same as _before_. Sorry, I thought this is obvious. Else you would need some kind of technique to tell the compiler.And again, please check for "call by reference" because the thing you try to do, cannot be done, at least not in C.Another thing: "push" does mean: store on stack and reduce the stack pointer.So r0 is _not_ "pushed" after the function call. It is rather stored in its stack location.
BTW: I checked with GCC, and yes (strange to me) "volatile" seems to force the compiler to store the variable on the stack instead of using a register.