Startup Code Explained: Reset to main()

The CPU resets.
Execution begins at a fixed address.
Startup code prepares the system before main() ever runs.

1. The Reset Vector

When an embedded system powers on or resets, the processor begins execution from a predefined memory address.

This address contains the reset vector, which points to the reset handler — the first piece of firmware that executes.

The reset handler is responsible for starting the initialization sequence that prepares the system to run application code.

2. The Role of Startup Code

Startup code runs before the program enters main().

Its purpose is to prepare the runtime environment required for programs written in C or C++.

Typical responsibilities include:
  • setting the stack pointer
  • preparing memory sections
  • initializing global variables
  • configuring essential runtime settings

Without these steps, the program cannot safely execute high-level code.

3. Memory Initialization

Two important memory regions must be prepared during startup.

Initialized data section
Variables with predefined values are copied from non-volatile memory into RAM.

BSS section
Variables without explicit initialization are cleared and set to zero.

This ensures global and static variables behave according to language guarantees.

4. Interrupt Vector Setup

Most embedded systems maintain a vector table that stores addresses of interrupt handlers.

During startup, the processor must know where this table is located so that hardware interrupts can call the correct functions.

Incorrect vector configuration can cause immediate system failures when interrupts occur.

5. Entering main()

After memory is initialized and the runtime environment is prepared, the startup code finally transfers control to main().

At this point:
  • memory is valid
  • global variables are initialized
  • the stack is ready
  • interrupts can be configured

Only then does the application logic begin.
Startup code prepares the system.
main() begins the application.
Firmware truly starts before both.