Reset, the vector table and startup code
About this lesson
What happens between pressing RESET and the first line of main()? Follow an STM32F407VG (on the STM32F4DISCOVERY board) through it. The CPU reads two words from the vector table at the start of flash: 0x2002 0000 becomes the stack pointer and 0x0800 0189 sends it to Reset_Handler. That startup code copies initialized globals (.data) from flash into SRAM, zeroes the rest (.bss), calls SystemInit() and then main(). The same table later routes every interrupt. Pointers are covered in the C++ lesson “Pointers”, and flash versus SRAM in the Hardware lessons “ROM and flash” and “SRAM”; the lesson “Blink with registers, no library” comes just before this one.
Reset: start over
Press RESET, or switch the power on, and the STM32 stops whatever it was doing. Its registers go back to fixed reset values. When the button is released, the CPU knows only one thing: where to look first, address 0.
reset RESET pin low, or power on registers back to their reset values SP, PC not yet loaded SRAM nobody has written your values yet released -> the CPU reads address 0
Read the lesson step by step
Reset: start over
Press RESET, or switch the power on, and the STM32 stops whatever it was doing. Its registers go back to fixed reset values. When the button is released, the CPU knows only one thing: where to look first, address 0.
reset RESET pin low, or power on registers back to their reset values SP, PC not yet loaded SRAM nobody has written your values yet released -> the CPU reads address 0The vector table
With the BOOT0 pin low, the STM32 shows its flash, which really starts at 0x0800 0000, at address 0 as well. The first 98 words there are the vector table: a list of addresses the CPU reads, not code it runs.
BOOT0 = 0 flash is seen at 0x0000 0000 and at 0x0800 0000 vector table 98 words = 392 bytes (16 system + 82 interrupts) 0x0800 0000 2002 0000 Word 0: initial SP 0x0800 0004 0800 0189 Word 1: reset vector 0x0800 003C &SysTick Word 15: SysTick each entry is an address, not an instructionWord 0: the stack pointer
First the CPU copies word 0, 0x2002 0000, into SP, the stack pointer. That is the address just past the top of 128 KB of SRAM. The stack grows down from there, so function calls can now save things on it.
word 0 0x2002 0000 -> SP linker _estack = ORIGIN(RAM) + LENGTH(RAM) = 0x2000 0000 + 128 KB = 0x2002 0000 first push writes 0x2001 FFFC: the stack grows downWord 1: where to start
Next it copies word 1, 0x0800 0189, into PC, the program counter. The odd last bit only marks Thumb code, the instruction set Cortex-M runs, so PC becomes 0x0800 0188: the first instruction of Reset_Handler.
word 1 0x0800 0189 (bit 0 = 1: Thumb) PC 0x0800 0188 = Reset_Handler here right after the 392-byte table (our build) no C has run yet: globals are not setCopy .data into SRAM
A global with a start value, like int count = 5;, must live in SRAM so it can change. But SRAM loses everything without power. So the linker keeps the 5 in flash, and this loop copies it into SRAM, word by word.
int count = 5; lives in .data _sidata where the start values sit in flash _sdata .. _edata where .data lives in SRAM lines 2-4 copy one 32-bit word per pass count ?? -> 5Zero the .bss
A global with no start value, like int ticks;, must read 0: the C standard says so. All of them sit together in the .bss section, and this loop writes 0 into each word. Nothing is copied, so they cost no flash.
volatile int ticks; lives in .bss C: a global with no initializer starts at 0 _sbss .. _ebss the .bss words in SRAM lines 5-6 write 0 into each word ticks ?? -> 0SystemInit(), then main()
SystemInit() switches on the floating-point unit and points the CPU at the vector table. Then Reset_Handler calls main(). Only now does your program run, with a stack and every global set. On an Uno, avr-libc does this before main() calls setup().
SystemInit() FPU on (CPACR), VTOR = 0x0800 0000 the clock stays 16 MHz HSI until main sets the PLL main() your program, SP and globals ready Arduino Uno avr-libc .init4 copies .data, zeroes .bss, .init9 calls main(): init(), setup(), loop() ...Same table, for interrupts
The table is not only for reset. SysTick is exception 15, so its entry sits at 15 × 4 = 0x3C. When the timer fires, the CPU saves 8 registers on the stack, reads that entry and runs SysTick_Handler: ticks becomes 1.
SysTick exception 15 -> entry at 15 x 4 = 0x3C (0x0800 003C in flash) stacking 8 words pushed: R0-R3, R12, LR, PC, xPSR vector fetched from the table at the same time handler ticks++ -> ticks = 1 return the 8 words come back off the stack
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Right after reset, what does a Cortex-M CPU do with the word at address 0x0000 0000?
Word 0 is the initial stack pointer. On our STM32F407 it is 0x2002 0000, the address just past the top of 128 KB of SRAM. Word 1 is the reset vector, which goes into PC.
02Word 1 of the table holds 0x0800 0189. Where does the first instruction of Reset_Handler sit?
Bit 0 of every vector is 1 to mark Thumb code. It is not part of the address, so PC becomes 0x0800 0188.
03A program has the global int count = 5;. Where is the 5 kept while the power is off, and who puts it into SRAM?
SRAM loses its contents without power. The linker stores the start values of .data in flash (at _sidata), and Reset_Handler copies them to _sdata.._edata in SRAM.
04Why does a global like int ticks; (no start value) cost no flash?
C requires globals without an initializer to start at 0. They are grouped in .bss, and a loop writes 0 into each word, so there is nothing to store in flash.
05SysTick is exception 15. At what offset in the vector table is its handler's address?
Each entry is 4 bytes, so entry 15 sits at 15 × 4 = 60 = 0x3C. With flash at the table's start, that is 0x0800 003C.
Glossary
- Reset
- Putting the chip back to a known starting state, by the RESET pin, power-on or a watchdog. Registers return to fixed reset values.
- Vector table
- A list of addresses at the start of memory. On Cortex-M, entry 0 is the initial stack pointer, entry 1 the reset handler, then one entry per exception and interrupt.
- Stack pointer (SP)
- A CPU register holding the address of the top of the stack. On Cortex-M the stack grows toward lower addresses.
- Program counter (PC)
- A CPU register holding the address of the next instruction to run.
- Reset_Handler
- The startup code: the first code the CPU runs after reset. It prepares memory and then calls main().
- .data
- The section for globals with a start value. It lives in SRAM; its start values are stored in flash and copied at startup.
- .bss
- The section for globals without a start value. The startup code fills it with zeros.
- Thumb
- The instruction set that Cortex-M processors run. Every vector's lowest bit is 1 to mark Thumb code.
Notes and sources
- The card shows only Reset_Handler. The full file also declares the linker symbols (_sidata, _sdata, _edata, _sbss, _ebss, _estack, all defined in ST's STM32F407VGTX_FLASH.ld), builds the vector table in the .isr_vector section, and defines count, ticks and SysTick_Handler. A real table lists all 98 entries.
- ST's own startup file (startup_stm32f407xx.s) is assembly and does the same steps in a slightly different order: it sets SP from _estack, calls SystemInit() first, then copies .data, zeroes .bss, calls __libc_init_array (which runs C++ constructors) and then main().
- The address of Reset_Handler depends on the build. Ours sits right after the 392-byte table at 0x0800 0188, so word 1 holds 0x0800 0189. Every vector has bit 0 set because Cortex-M runs only Thumb code.
- BOOT0 low makes main flash the boot space, seen both at 0x0000 0000 and at 0x0800 0000 (ST RM0090, section 2.4, Boot configuration). With BOOT0 high the chip boots its built-in bootloader instead.
- The 8-word frame is the case here because this program never uses the floating-point unit. Once a task has used it, the Cortex-M4 frame also reserves room for S0-S15 and FPSCR (18 more words, with an aligner).
- The linker script gives the program 128 KB of RAM (SRAM1, 112 KB, plus SRAM2, 16 KB). The STM32F407 also has 64 KB of CCM RAM at 0x1000 0000, which this script keeps separate.
- Arm, Cortex-M4 Devices Generic User Guide (DUI 0553): registers, vector table, exception entry
- Arm CMSIS 6: startup file (vector table and Reset_Handler)
- ST: startup_stm32f407xx.s (GCC startup and vector table)
- ST: STM32F407VGTX_FLASH.ld linker script (STM32CubeF4, STM32F4-Discovery)
- ST: stm32f407xx.h device header (memory base addresses)
- ST: system_stm32f4xx.c (SystemInit)
- cppreference: initialization in C (static objects start at zero)
- avr-libc user manual: memory sections (.data, .bss, .initN)
- Arduino AVR core: main.cpp (main calls setup and loop)
Quiz Unlocks after the last step.