Bird
Raised Fist0
Embedded Cprogramming~10 mins

Startup sequence and reset vector in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Startup sequence and reset vector
Power ON / Reset
↓
CPU Fetches Reset Vector
↓
Jump to Startup Code
↓
Initialize Data & BSS Sections
↓
Call main() Function
↓
Program Runs Normally
When the device powers on or resets, the CPU fetches the reset vector address, jumps to startup code that sets up memory, then calls main() to start the program.
Execution Sample
Embedded C
void Reset_Handler() {
  // Initialize data and bss
  main();
}

int main() {
  // User code
  while(1) {}
}
This code shows the reset handler initializing memory then calling main, which runs the main program loop.
Execution Table
StepActionAddress/ValueDescription
1Power ON or ResetN/ADevice powers on or reset signal triggers
2Fetch Reset Vector0x00000000CPU reads reset vector address from fixed location
3Jump to Reset_Handler0x00000000CPU jumps to startup code address
4Initialize .data sectionCopy from Flash to RAMVariables with initial values are copied
5Zero .bss sectionSet to 0Uninitialized variables are cleared
6Call main()0x00000100Start main program function
7Execute main loopN/AProgram runs user code in infinite loop
8EndN/AProgram runs indefinitely until reset or power off
💡 Program runs indefinitely in main loop; no exit from startup sequence
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6Final
.data sectionIn FlashCopied to RAMUnchangedUnchangedIn RAM with initial values
.bss sectionUndefinedUndefinedZeroedUnchangedZero in RAM
Program Counter (PC)Reset Vector AddressReset_Handler AddressReset_Handler Addressmain() Addressmain() Address
Key Moments - 3 Insights
Why does the CPU jump to the reset vector address after power on?
Because the reset vector holds the address of the startup code, so the CPU knows where to begin executing after reset (see execution_table step 2 and 3).
What happens to variables in the .bss section during startup?
They are set to zero to ensure no garbage values (see execution_table step 5 and variable_tracker .bss section).
Why does main() never return in embedded programs?
Because embedded programs usually run continuously, main() contains an infinite loop to keep the program running (see execution_table step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the CPU doing at step 4?
AInitializing the .data section by copying from Flash to RAM
BJumping to main() function
CZeroing the .bss section
DFetching the reset vector
💡 Hint
Check the 'Action' and 'Description' columns at step 4 in execution_table
According to variable_tracker, what is the state of the .bss section after step 5?
ACopied to RAM with initial values
BUndefined
CZeroed in RAM
DContains garbage values
💡 Hint
Look at the .bss section row under 'After Step 5' in variable_tracker
If the reset vector address changed, which step in execution_table would be directly affected?
AStep 7: Execute main loop
BStep 2: Fetch Reset Vector
CStep 5: Zero .bss section
DStep 4: Initialize .data section
💡 Hint
The reset vector address is fetched at step 2 according to execution_table
Concept Snapshot
Startup sequence in embedded C:
- On reset, CPU fetches reset vector address
- CPU jumps to startup code (Reset_Handler)
- Startup code initializes .data and .bss sections
- Then calls main() function
- main() runs user code, usually infinite loop
- Reset vector is fixed address pointing to startup code
Full Transcript
When an embedded device powers on or resets, the CPU starts by fetching the reset vector address from a fixed memory location. This address points to the startup code, often called Reset_Handler. The CPU jumps to this startup code, which initializes the memory sections: it copies initialized variables (.data) from Flash to RAM and clears uninitialized variables (.bss) by setting them to zero. After memory setup, the startup code calls the main() function, where the user's program runs, usually inside an infinite loop to keep the device running. This sequence ensures the program starts correctly every time the device resets or powers on.

Practice

(1/5)
1. What is the primary role of the reset vector in an embedded C program?
easy
A. It manages power consumption modes.
B. It initializes all global variables to zero.
C. It handles interrupts during program execution.
D. It tells the microcontroller where to start executing code after a reset.

Solution

  1. Step 1: Understand the reset vector concept

    The reset vector is a special address in memory that points to the first instruction executed after a reset.
  2. Step 2: Identify its role in program start

    When the microcontroller resets, it jumps to the reset vector to begin running the startup code.
  3. Final Answer:

    It tells the microcontroller where to start executing code after a reset. -> Option D
  4. Quick Check:

    Reset vector = start address after reset [OK]
Hint: Reset vector points to start code after reset [OK]
Common Mistakes:
  • Confusing reset vector with interrupt vector
  • Thinking reset vector initializes variables
  • Assuming reset vector manages power modes
2. Which of the following is the correct way to declare the reset vector in embedded C?
easy
A. int ResetVector = 0x0000;
B. void ResetVector() { main(); }
C. void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler;
D. char *ResetVector = "Reset_Handler";

Solution

  1. Step 1: Understand reset vector declaration

    The reset vector is usually a pointer to a function placed in a special memory section.
  2. Step 2: Check syntax correctness

    void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler; correctly declares a constant function pointer to Reset_Handler placed in the ".reset_vector" section using GCC attribute syntax.
  3. Final Answer:

    void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler; -> Option C
  4. Quick Check:

    Function pointer in reset vector section = correct declaration [OK]
Hint: Reset vector is a function pointer in special section [OK]
Common Mistakes:
  • Declaring reset vector as int or char pointer
  • Defining reset vector as a function instead of pointer
  • Missing section attribute for placement
3. Given the following code snippet, what will be the output after reset?
void Reset_Handler(void) {
    int x = 5;
    x += 10;
    while(1) {}
}

void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler;
medium
A. The program will enter an infinite loop after setting x to 15.
B. The program will run main() and print 15.
C. The program will cause a runtime error due to missing main.
D. The program will reset repeatedly.

Solution

  1. Step 1: Analyze Reset_Handler behavior

    Reset_Handler sets x to 5, adds 10 making x = 15, then enters an infinite loop.
  2. Step 2: Understand program flow after reset

    After reset, the microcontroller jumps to Reset_Handler and stays in the infinite loop; no main() is called.
  3. Final Answer:

    The program will enter an infinite loop after setting x to 15. -> Option A
  4. Quick Check:

    Reset_Handler runs then loops forever = infinite loop [OK]
Hint: Reset_Handler runs then loops forever, no main called [OK]
Common Mistakes:
  • Assuming main() is called automatically
  • Expecting output or print statements
  • Confusing variable x scope and effect
4. Identify the error in this startup code snippet:
void Reset_Handler(void) {
    // Initialize data
    extern int _sidata, _sdata, _edata;
    int *src = &_sidata;
    int *dst = &_sdata;
    while(dst < &_edata) {
        *dst++ = *src++;
    }
    main();
}

void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler;
medium
A. Reset_Handler should not call main() directly.
B. The pointers _sidata, _sdata, _edata should be declared as extern char, not int.
C. The while loop condition should use <= instead of <.
D. ResetVector should not be a function pointer.

Solution

  1. Step 1: Check pointer types for memory copy

    Memory sections like _sidata, _sdata, _edata represent byte addresses and should be pointers to char or unsigned char, not int.
  2. Step 2: Understand pointer arithmetic impact

    Using int pointers causes incorrect copying because pointer arithmetic advances by sizeof(int), skipping bytes.
  3. Final Answer:

    The pointers _sidata, _sdata, _edata should be declared as extern char, not int. -> Option B
  4. Quick Check:

    Memory pointers for data init = char* not int* [OK]
Hint: Use char pointers for memory copy in startup [OK]
Common Mistakes:
  • Using int pointers for byte-wise memory copy
  • Changing loop condition incorrectly
  • Thinking main() call is wrong here
5. You want to create a startup sequence that initializes .data and .bss sections and then jumps to main(). Which of the following code snippets correctly implements this in embedded C?
hard
A. void Reset_Handler(void) { extern char _sidata, _sdata, _edata, _sbss, _ebss; char *src = &_sidata; char *dst = &_sdata; while(dst < &_edata) *dst++ = *src++; dst = &_sbss; while(dst < &_ebss) *dst++ = 0; main(); } void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler;
B. void Reset_Handler(void) { int *src = &_sidata; int *dst = &_sdata; while(dst <= &_edata) *dst++ = *src++; main(); } void ResetVector = Reset_Handler;
C. void Reset_Handler(void) { main(); } void (* ResetVector)(void) = Reset_Handler;
D. void Reset_Handler(void) { extern char _sidata, _sdata, _edata; char *src = &_sdata; char *dst = &_sidata; while(src < &_edata) *dst++ = *src++; main(); } void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler;

Solution

  1. Step 1: Verify .data section initialization

    void Reset_Handler(void) { extern char _sidata, _sdata, _edata, _sbss, _ebss; char *src = &_sidata; char *dst = &_sdata; while(dst < &_edata) *dst++ = *src++; dst = &_sbss; while(dst < &_ebss) *dst++ = 0; main(); } void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler; correctly copies initialized data from flash (_sidata) to RAM (_sdata to _edata) using char pointers and proper loop.
  2. Step 2: Verify .bss section zero initialization

    void Reset_Handler(void) { extern char _sidata, _sdata, _edata, _sbss, _ebss; char *src = &_sidata; char *dst = &_sdata; while(dst < &_edata) *dst++ = *src++; dst = &_sbss; while(dst < &_ebss) *dst++ = 0; main(); } void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler; zeroes the .bss section (_sbss to _ebss) by setting bytes to 0 before calling main().
  3. Step 3: Check reset vector declaration

    void Reset_Handler(void) { extern char _sidata, _sdata, _edata, _sbss, _ebss; char *src = &_sidata; char *dst = &_sdata; while(dst < &_edata) *dst++ = *src++; dst = &_sbss; while(dst < &_ebss) *dst++ = 0; main(); } void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler; declares ResetVector as a constant function pointer placed in the reset vector section, which is correct.
  4. Final Answer:

    Option A correctly implements startup sequence with .data and .bss initialization and reset vector. -> Option A
  5. Quick Check:

    Proper .data/.bss init + correct reset vector = void Reset_Handler(void) { extern char _sidata, _sdata, _edata, _sbss, _ebss; char *src = &_sidata; char *dst = &_sdata; while(dst < &_edata) *dst++ = *src++; dst = &_sbss; while(dst < &_ebss) *dst++ = 0; main(); } void (* const ResetVector)(void) __attribute__((section(".reset_vector"))) = Reset_Handler; [OK]
Hint: Initialize .data from flash, zero .bss, then call main() [OK]
Common Mistakes:
  • Using int pointers instead of char pointers
  • Swapping source and destination pointers
  • Missing .bss zero initialization
  • Incorrect reset vector declaration
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard