Keil Logo

Calculating Stack Size

The C251 compiler cannot calculate exact stack sizes of your application since the compiler does not detect the actual program flow and does not know about the interrupt functions in your system.

The 251 hardware stack is used to store reentrant variables, return addresses, and PUSH/POP instructions. The size of the hardware stack area is defined in the file START251.A51. The default value of the system stack is 100 bytes which is usually enough for most non-reentrant applications.

If you use heavily reentrant code in your application, you should increase the value for the hardware stack to 500 bytes. If you want to reduce the stack size, we suggest the following procedure for calculating the user stack size of your system:

  1. Stop the program execution at the main entry and fill the memory area allocated to the hardware stack with a constant value (i.e. 0xA5).

    The address range of the user stack is listed in the linker map file (*.MAP) under the headline MEMORY MAP OF MODULE. The segment ?STACK lists the address range for the hardware stack.
  2. Execute all functions of your application and check how many constants are overwritten.

    We recommend that you have at least enough space to execute the most complex interrupt function of your system. A good value for the spare space on the user stack is 20 bytes

If your application behaves strangely and if you think that the stack size could be a problem, you may increase the user stack size and try again.

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.