cancel
Showing results for 
Search instead for 
Did you mean: 

Why Initial SP value do we need?

Carter Lee
Associate III
Posted on October 19, 2017 at 17:30

Hi,

I have some question about the relationship which is purpose of Initial SP 0x0000 and Reset 0x004 vector and Flash memory address.

0690X00000608ieQAA.png

For example, let's assume that  I made one simple firmware and its size is 0x1000.

and I uploaded to Flash memory, the address will be supposed to be 0x08001000.

Q1. Why does Stack pointer should be in SRAM area not Flash memory?

Q2. Why Initial SP value do we need and need to know?

this is another example,

the following is startup.dis

0690X00000608WpQAI.png

From here you can see the 20004fff, I think it will be initial stack pointer,

But I can't understand the purpose of set the initial SP value to 0x800000.

There is no any action about 20004fff.

Q3, What does 20004fff mean? how and why 20004fff value is in 8000000?

#startup
6 REPLIES 6
Posted on October 19, 2017 at 18:20

A1: RAM, because you can't read/write FLASH rapidly/repeatedly

A2: In case you call a subroutine, and that routine has to push LR, or other registers it might use. Also allows interrupts/faults to occur from the outset.

A3: It would be better as 0x20005000, are you using GNU/GCC, because a misaligned stack has a significant penalty on speed. The SP is decremented prior to use. It is a value for top-of-stack placed in the vector table, typically in startup_stm32fxxx.s

So let me ask you some questions..

Are you a teacher/educator?

Have you studied any books on CPU architecture?

What level of education do you have, and in what subjects?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
Posted on October 19, 2017 at 19:17

In microcontroller, the SRAM area is used for dynamic data. Usually it is split in 2 regions, the stack and the heap.

The heap is a fixed area where global variables are stored, buffers, arrays, etc.. 

The stack is... last item put in stack will be first popped from the stack. The stack is a pointer which auto increment/decrement when push/pop. When going to interrupt, some of the core registers will be backed up in the stack. Once the interrupt function is completed, the core context will be restored (popped) for the core to continue. The trick is to know how big the stack RAM area is needed.... all this is estimated by the linker. Linker usually have a memory description file to tell how big the stack and heap should be. Some linker estimate the stack size by recursively walking all the functions from main() and adding the interrupts to this budget... as long as you don't let the linker losing track by using function pointers or the ... function (such as printf(...) )

Posted on October 20, 2017 at 10:21

Thanks I thinkthe following image is what you want to say.

http://www.geeksforgeeks.org/wp-content/uploads/Memory-Layout.gif

But I still want to know why 0x00000000 is assigned as being Initial SP not the other address?

Posted on October 20, 2017 at 10:42

But I still want to know why 0x00000000 is assigned as being Initial SP not the other address?

No, 0x00000000 is not the initial SP, it is the address where the initial SP is taken from.

This happens after a reset, when all internal registers have been set to default, and it is the task of the application program to put a meaningful value at this address.

This behavior is defined by ARM, for all Cortex M implementation. It just has to start somewhere ...

The address 0x00000000 might be shadowed/mapped to other addresses in a vendor-dependant manner.

Posted on October 20, 2017 at 12:48

The Vector Table is a list of Addresses, it provides indirect or vectored behaviour. 

The CPU is an automated machine, it wants to do very simple things, at reset it wants to load content for SP and PC registers, and then starts executing. 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
William Chang1
Associate II
Posted on May 07, 2018 at 10:25

Q3, What does 20004fff mean? how and why 20004fff value is in 8000000?

0x2000 4fff is the value for stack pointer.

why 0x2000 4fff in 0x0800 0000?

when the MCU reset happen, It will fetch the value stored in 0x0800 0000 into the R13(SP),

then It fetch the value stored in 0x800 0004 into PC. 

The value as you know is the address of reset handler.