Startup Code for Embedded Systems


Startup code consists of code to setup and to terminate an application program. Startup code is typically written in assembly language and the source code is sometimes provided with the C compiler. Replacement libraries often include replacement startup code that can be used when the normal C compiler library functions are not used.

Startup code for EPROM-based systems must provide additional functionality which depends on the specifics of the system.

Embedded system startup code usually performs the following steps:

1. Establish order of segment classes.

2. Set up the program stack.

3. Transfer initialized values to RAM from EPROM.

4. Zero uninitialized RAM values.

5. Set up error interrupt vectors.

6. Call necessary library initialization routines.

7. Call the application.

The startup code must also provide the following components:

8. Error shut-down code to terminate or restart.

9. Exit shut-down code to terminate or restart.

10. Any error and miscellaneous routines.

11. Initialization of application before calling main function.

12. A reset vector for standalone applications.

There are two approaches to creating a custom startup module. You can start with the original library or compiler code and modify it to handle the added embedded requirements, or you can start with a minimal embedded system startup module and extend it as required.

Custom startup code may not provide all features described in a compiler's documentation. Environment, argv and argc, and even common option variables may not be implemented. For example, Borland C allows you to set stack size by initializing a far variable, _stklen. This variable is created by the Borland startup code and may be replaced in custom startup code by a simple stack-size definition.

Startup code defines segments and segment classes and their order. While compilers often allow these names to be changed for modules, the compiled libraries are still expecting certain module names to exist. Warnings abound over changing the basic segment order so extreme care should be used if that appears to be necessary. The addition of segments and classes is much less critical. As shown in Listing 4, you can even add them virtually to make the MAP more descriptive.

Listing 4 is a simple example of startup code. I provide it as a template rather than a complete example since some portions are compiler dependent. You may still need to add several code sections for an application. Refer to your compiler's startup code module for specific details.