5

In terms of microcontrollers and embedded systems with C startup code, one of the functions of the C startup code is to initialize the stack pointer.

Is this initial stack pointer address and C startup code generally defined and provided by the chip vendor?

Or is it up to us as the firmware / software developers to manually modify or create the C startup code and specify the stack pointer? This part confuses me.

Engineer999
  • 3,683
  • 6
  • 33
  • 71
  • The initial stack pointer state should be specified in the data sheet of the microcontroller. – tkausl Feb 20 '19 at 18:53
  • The quick answer is you do. Generally, the stack pointer is one of the first things defined in startup code. Where in memory you place the stackpointer is generally strongly hinted at by your datasheet, but you ( or whomever writes crt0 or init kernel or whatever) gets to choose. Your board will jump to some hardwired address in memory and start executing code on powerup. This is where that define would go or where you would .org your startup code. – Michael Dorgan Feb 20 '19 at 18:54
  • Aside: in some processors the NMI is not really non-maskable: it is only enabled once you set the stack pointer. – Weather Vane Feb 20 '19 at 19:06
  • both. some architectures the stack pointer is defined by hardware, i would assume this to be rare. think 6502 for example. Other architectures it is all up to you like arm7 and cortex-m based. you read the documentation see how much ram the device has decide how you the software developer wants to divide that memory up for your project leaving some for stack. – old_timer Feb 21 '19 at 01:04
  • HOW you set the stack pointer if you can and choose to is also architecture specific. – old_timer Feb 21 '19 at 01:04
  • You are responsible as the software developer but most of the sandboxed environments will detect or ask you what chip you are using, their compilers will have a linker script, and bootstrap code setup for you, very likely including the initialization of the stack pointer. But there are many sandboxes to play in and they all are both similar and different. – old_timer Feb 21 '19 at 01:06

2 Answers2

3

Every architecture does it it's way. ARM Cortex uCs have the initial stack pointer location stored as the first 32 bits value in the vector table. This value is loaded by the hardware when the uC is booted. So this address is fully controllable by the programmer.

Then the start-up routine can change it or set the double stack (one for the thread another for the privileged mode).

0___________
  • 60,014
  • 4
  • 34
  • 74
1

The stack location in RAM is almost always configurable through software. In your tool chain-specific linker script, there will be an area called .stack or similar, which you can decide to place at a certain address, or give a certain size. This is only the memory mapping part though.

The first thing done in any start-up code is to set the stack pointer. It has to be done through assembler, since C has no means of directly writing to the stack pointer. On some cores like ARM, the stack pointer is set automatically by hardware out of reset, from a value that your linker script has placed in the vector table and programmed in flash.

So generally, the silicon vendor almost always leaves this completely to the programmer to decide. The core does specify if there's an up-counting or down-counting stack however. The most common by far is down-counting, but some oddball architectures have up-counting stacks (most famously PIC).

As for where the stack pointer is set, you'll find it very early on in the start-up code ("CRT"), if such code is delivered with your tool chain. Typically it takes a value obtained from the linker script, but you could as well hard-code it if you wish. It needs to be done before any C code is executed, since C loves to use the stack.

In case you write everything yourself, you should write to the stack pointer the first thing you do out of reset, from inside the reset vector.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Ok Thanks for that. Normally is the start-up-code always provided by the chip vendor? , or we have to write it from scratch? for example an ARM chip from Atmel – Engineer999 Feb 21 '19 at 11:36