Initial stack pointer not starting at the required offset (where are the extra byte offsets coming from?)

0

I have this following sample code in the start-up file for Cortex-M3 taken from Keil(compiling it with Microlib).

; <h> Stack Configuration
;   <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>
                EXPORT  __initial_sp
Stack_Size      EQU     0x00000100

                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp

And this area is finally placed into a RAM region starting at address 0x20000000 with size of the executable region say 0x400 in the scatter file.

When I get into the debugger I see the that the value at memory address 0x0 is 0x20000118 which is the initial stack pointer and even the register window shows the msp register as 0x20000118.

But my understanding was that the start of the stack would be from 0x20000100 because that is what the above code snippet is doing.

I am unable to get from where are these extra 0x18 bytes coming from.

Also, i just switch off the Microlib mode, now I see the initial stack pointer is 0x20000120.
Again, from where are these extra 0x20 bytes offset coming from to the stack pointer.

Why isn't stack starting from where I want it to be(0x20000100), instead having some extra offsets?

stack
arm
cortex-m3
keil
asked on Stack Overflow Nov 30, 2015 by Uchia Itachi

1 Answer

0

No, this code snippet doesn't say that initial stack pointer will be at 0x20000100.

Firstly, it EXPORTs symbol "__initial_sp". This only declares this symbol as "global" (accessed by other files). Next, the value 0x100 is assigned to symbol "Stack_Size". Next instructions are to create dummy "STACK" section which will be of "stack_size" size.

The initial stack pointer value will be calculated (usually) by linker script. You also need to see source code of vector table (in most cases it will be in a file called startup.s or similar) and see what symbol there is used as a first entry (is it really "__initial_sp"?).

Note, if you have (for example) 32KB of RAM and your RAM starts at 0x20000000, then you want (usually) your initial SP to be at 0x20008000 (end of RAM). If "stack size" is equal to "0x100" it means that you don't expect SP to be less than 0x20007F00. But, you can also have initial stack pointer at address that depends on size of other sections (for instance .heap or .data). This is why you can see differences when linking to standard library (it will change size of other sections).

answered on Stack Overflow Dec 2, 2015 by mkmk88

User contributions licensed under CC BY-SA 3.0