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.

The short versionThe small assembly/C routine that runs before main(), setting up the C runtime environment.

Theory in plain words

What startup code does before main()CPU reset: jump toreset handler (from…Set up the initialstack pointerCopy .data sectionfrom flash to RAMZero the .bss sectionCall main()
What startup code does before main()

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

Simulating what startup code does to a .data and a .bss variableC: edit and run

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

Loading the initial stack pointer from the vector table's first entryC: edit and run

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?