Startup sequence and reset vector in Embedded C - Time & Space Complexity
We want to understand how the time it takes for a microcontroller to start up changes as the startup code runs.
Specifically, how does the startup sequence and reset vector affect execution time as the program begins?
Analyze the time complexity of the following embedded C startup code snippet.
void Reset_Handler(void) {
// Copy initialized data from flash to RAM
for (int i = 0; i < DATA_SIZE; i++) {
data_ram[i] = data_flash[i];
}
// Zero initialize the BSS section
for (int i = 0; i < BSS_SIZE; i++) {
bss_ram[i] = 0;
}
main();
}
This code runs after reset. It copies data and clears memory before calling main.
Look at the loops that repeat work.
- Primary operation: Two for-loops copying and clearing memory.
- How many times: Each loop runs once, repeating DATA_SIZE and BSS_SIZE times respectively.
As the size of data to copy and clear grows, the time grows too.
| Input Size (DATA_SIZE + BSS_SIZE) | Approx. Operations |
|---|---|
| 10 | About 20 operations (copy + clear) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The time grows roughly in direct proportion to the total size of data to initialize.
Time Complexity: O(n)
This means the startup time grows linearly with the amount of data to copy and clear.
[X] Wrong: "The startup time is always constant no matter how much data is initialized."
[OK] Correct: The loops copy and clear memory based on data size, so more data means more work and longer time.
Understanding how startup code scales helps you write efficient embedded programs and shows you know how hardware initialization affects performance.
"What if the startup code used DMA (Direct Memory Access) to copy data instead of a loop? How would the time complexity change?"