This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

volatile variable position in the stack (ABI std)

Hello everyone,

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:

    int a=2;
	int b=3;
	volatile int r=8;
	
	r=assembly_function(a,b);
	
	...

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