Increasing stack size

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Increasing stack size

1,079 Views
admin
Specialist II

Hi all,

 

I have a project which runs on MC9S08DZ60. This MCU has 4Kb of RAM.

In my current phase of project, my stack size is 0x700 bytes.

 

I increment the stack size when I see bad behavior of the sw (PC goes to wrong location/variables values are changed).

 

I hardly have nested function calls. I noticed that I increase stack size if I add few function calls/new features. Why is the reason for
increasing the stack size if functions nested calls are 2-3 levels?

 

Thanks.

 

Labels (1)
0 Kudos
Reply
4 Replies

572 Views
eckhard
Contributor V

Hello,

hard to say without seeing your code, but you should be aware that local variables are on the stack to. So if you have a lot or big local variables in your functions, your stack may increase this much.

Eckhard

0 Kudos
Reply

572 Views
ngsoftuser
Contributor III

Hi,

I have many global variables structures and array which are on RAM but not on the stack. The problem is that when I add new functionallity to the project, in some cases i need to increase stack size.

Even if I have many functions which have many local variables, I see no reason to increase the stack size to 0x800 bytes (my currently used RAM is ~3.4KbB and Flash is 15Kb).

Correct me if i am wrong, but when the CPU leaves function, all the variables are removed from the stack.
So what else causes the stack size to increase?

I read in a response (Re: Put a function into the stack) that when running function from stack is like running it from RAM.
I know that the stack is basically on the RAM. So do I need to increase stack size when I add more global variables?

Thanks.

0 Kudos
Reply

572 Views
bigmac
Specialist III

Hello eran yasso,

Global variables (and also static variables) occupy a fixed,  linker determined location in RAM.  The number of global variables should have no bearing on stack size requirements.

The stack is used for temporary storage of data, such as -

  1. Return address data for each function call.
  2. Register values and return address data during an interrupt.
  3. Function parameter data.
  4. Local variables within a function.
  5. Additional temporary variables required by library functions and operations, e.g. arithmetic operations.

The last item is quite significant if you are using floating point arithmetic, and even more so if you are using trigonometric or other complex library functions.

Yes, the stack space is eventually released.  The required stack size will depend of the degree of function nesting, in conjunction with the factors listed above.

Regards,

Mac

0 Kudos
Reply

572 Views
ngsoftuser
Contributor III

Hello bigmac,

Thank you for your raply. I agree with all what you say. The point is that I still have to increase

The stack size when I add more functionallity and which i have no idea why.

I added my map file maybe you can see the reason for increasing stack size.

Thank you.

0 Kudos
Reply