cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Stack Behavior Question

bruce
Associate II
Posted on August 15, 2008 at 19:49

STM32 Stack Behavior Question

4 REPLIES 4
bruce
Associate II
Posted on May 17, 2011 at 12:41

Correction, the stack marker I pushed was 0x3456.

bruce
Associate II
Posted on May 17, 2011 at 12:41

I'm trying to understand the behavior of the stack in the STM32.

I'm using the gcc based tools from Anglia.

For example, my map file shows:

0x20002aa0 __stack_start__ = .

0x20002ba4 __stack_end__ = (__stack_start__ +

When running, if I Read at 0, to get the stack ptr, it always returns with 0x20002ba4. Does it make sense that the stack ptr would be pointing to the end of the stack?

Then I tried to do some things to estimate stack usage:

In startup.c, before the call to main(), I tried to push a marker word on the stack like so:

__asm__ (''movw r4, #9029''); // stack marker = 0x2345

__asm__ (''push {r4}''); // push marker on stack

Then, after my program is running, I dumped a portion of the stack, and found my marker

CMD> dump 20002aa0 16

00: 20002aa0: 1

01: 20002aa4: 61

02: 20002aa8: 8003e57

03: 20002aac: 5

04: 20002ab0: 8001923

05: 20002ab4: 5fa0004

06: 20002ab8: 20002ab8

07: 20002abc: 6

08: 20002ac0: 8004168

09: 20002ac4: 5fa0004

10: 20002ac8: 8001ca7

11: 20002acc: 2000051c

12: 20002ad0: 8000c13

13: 20002ad4: 8004168

14: 20002ad8: 20002ad4

15: 20002adc: 20002ad4

16: 20002ae0: 16

17: 20002ae4: 3456

18: 20002ae8: 81000000

19: 20002aec: 1234

20: 20002af0: 97f414d6

21: 20002af4: 9494ae5a

Sorry for the rambling post, but here are my questions.

1. Does the stack grow up from the bottom, or down from the top?

2. Does it make sense that the stack pointer appears at the end of the stack rather than the start?

3. Is the ''marking'' technique I'm using here useful to see how big the stack gets?

lanchon
Associate II
Posted on May 17, 2011 at 12:42

1) don't know, get the cortex-m3 manual from the arm website.

2) yes it does, many micros grow the stack downward from the highest address. a possible rationale: if the program uses ram in a sequential upward fashion and the stack does the opposite growing down from above, the memory doesn't have to be pre-partitioned between data and stack, and can be allocated dynamically according to demand. (but this is kind of historical, I think nobody does this sharing anyway.)

3) instead of marking, why not just read the stack pointer? (you can keep a min (or max) value in a global.)

tibo
Associate II
Posted on May 17, 2011 at 12:42

your map file shows the stack variable name of the linker-script.

The linker script allocates stack memory for the stack. This has

nothing to do with the stack implemention of the processor (as

the linker script is for many different processors...). So

__stack_start__ means the smallest ram address.

In the startup routine the stack pointer is set to __stack_end__. The

stack in CM3 is from the top. The next used address is always

''stackpointer - 1'', so at the beginning the stack address points

to the ''biggest stack address + 1''. Push allways first decrements the

stack and then stores the value.

In your example you have to search on the biggest address. I suppose

you will find your marker on 0x2ba0 (if the startup file does not push

anything else before).

Pushing something on the stack as a marker should be no problem as

long as you pop it before returning from the function...