Stack usage Calculation

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I am trying to calculate stack utilized by my program in MPLAB X IDE v.5.40/v5.35 with XC32 compiler

Please let me know if there is any compiler option available for this.
Or any method to calculate it using .map file.

Thanks in Advance!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Stack usage it not a simple thing to calculate on anything but a very simple program. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I do not know of a method to do this during compilation; however, there is a method to check stack usage during program operation.

See   AVRGCC: Monitoring Stack Usage

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On a simple program, how can one calculate it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Analyse all stack operations. Try doing it by hand - you'll then understand why it is difficult.

Depending on your actual C compiler, local variables are normally stored on the stack and if you have a large number of parameters in a function call. Then consider how deep your function calls will get. Then you'll have an idea of stack usage.

Last Edited: Fri. Jul 10, 2020 - 03:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some tools can do a call-tree analysis (ISTR Keil does/did?) - from that you, can tell which call tree uses the most stack.

 

But that can be fooled by indirect calls - and doesn't account for interrupts, etc.

 

So you still have to go back to #3 to see what happens in Real Life ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The MAP file will likely be no help calculating stack usage.  It is useful for global memory allocations and subroutine lengths although it likely will tell you where the stack is and how big it is.  (Note in some OS settings there can be multiple stacks for threads and/or processor modes etc.)

 

To get stack usage you have two options: 1) deduce it by analyzing worst case usage in the code, or 2) estimate it by high water marking.  The latter involves setting the stack area to a known value, running your code for a while, and then looking at where the stack initial value has been overwritten (i.e. used).

 

Option 1 can be very complicated, whereas option 2 is fairly easy but not conclusive.

C: i = "told you so";