I'm trying to make an multithreading kernel for atmega328p micro an for that i need to know how the stack pointer it works.
Asked
Active
Viewed 329 times
-2
-
It would help if you show us some code snippets and sample input/output. See https://stackoverflow.com/help/mcve – hikerjobs Mar 07 '18 at 21:31
-
1Optimization might make only one return point for nested function calls. Or even inline all of these... – Eugene Sh. Mar 07 '18 at 21:33
-
I have edited to improve the English - please check I haven't mangled your meaning. I have also changed the tags - the language isn't that significant (and you will get a lot of push-back about using *both* C and C++ tags), and the experts on this are likely to be the Arduino experts, not C experts. If you want a language tag too, please pick *one*, not both. – Martin Bonner supports Monica Mar 07 '18 at 21:35
-
/* The output of the code is: 8700 8700 8700 8700 The right output need to be: 8700 8698 8696 8694 */ void func3() { Serial.println(SP); } void func2() { Serial.println(SP); func3(); } void func1() { Serial.println(SP); func2(); } void setup() { Serial.begin(9600); Serial.println(SP); func1(); } void loop() { } – Radu Mar 07 '18 at 22:07
-
Don't post your code in comments or as a new answer, edit your existing question, as I did now. – Matteo Italia Mar 08 '18 at 07:00
-
2Also asked at: http://forum.arduino.cc/index.php?topic=533773 If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. – per1234 Mar 08 '18 at 07:04
1 Answers
1
Most probably, your functions are rightly being inlined, so everything actually gets done straight in setup()
, with no function calls involved. If you want to forcefully disable inlining for them (to see the stack pointer change), you can apply the gcc noinline
attribute.
void __attribute__ ((noinline)) func1() {
...
}
If this still doesn't work, it's possible that gcc is applying tail call optimization anyhow. In that case, a simple method to make your functions not prone to this optimization is to print SP
both before and after the call.

Matteo Italia
- 123,740
- 17
- 206
- 299
-
Hi, seams like the answer are both, put the __attribute__ ((noinline)) before function name and to print SP both before and after call. I tried to do this with Atmel Studio and here is work fine, i will try to make three task, each other will power on an led if it works i will put the code here to help anyone who want to do the same thing. Thanks for help. – Radu Mar 08 '18 at 19:07
-
Yep, I expect that without `noinline` everything just got inlined, and even with it the tail calls got optimized to jumps. Adding something to do after the call makes tail call optimization optimization. Now if my answer helped you consider marking it as accepted (the tick near the upvote/downvote) buttons - it gives 5 points to you and 15 to me, and signals that the question did in fact have a solution. Thank you! – Matteo Italia Mar 09 '18 at 06:59