Home / Learn Embedded C / Startup Code and the Reset Handler
Startup Code and the Reset Handler
The small assembly/C routine that runs before main(), setting up the C runtime environment.
Theory in plain words
What it is
Startup code (often partly assembly, partly C) runs immediately after reset, before main() is called — it initializes the stack pointer, copies initialized data from flash to RAM, zeroes the uninitialized (.bss) section, and then calls main().
Why it matters
This is specifically what makes C's assumptions (like "every global variable is already zero-initialized or has its declared value before main() runs") actually true in practice — someone has to do that initialization work, and it is the startup code's job.
Mechanically speaking
The reset handler (the very first code to run) is the entry referenced by the vector table's reset slot — understanding this ties directly back to the earlier Interrupts group lesson on the vector table's structure.
Shows up in
- Toolchain/startup file understanding
- Debugging early-boot issues
- Custom startup code modification
Try it
Real startup code does this copy-and-zero step for every .data and .bss variable in the whole program, before main() ever runs.
Try it: setting up the initial stack pointer
On real Cortex-M hardware the CPU itself reads the very first vector-table word into the stack pointer before any code runs at all.
Check yourself
1. Why does C's assumption that global variables are correctly initialized before main() runs actually hold true in practice?
Startup code performs the real work (copying .data, zeroing .bss) that makes C's global-initialization guarantees actually true by the time main() begins executing.