Linkers, Loaders & Executables
On a microcontroller without an OS, what does the start-up code do right after power-on before calling main?
- AWrite all .text machine code back from RAM to flash
- BRead RAM and restore variable values from before the last power-off
- CCopy the initial values of .data from flash to RAM and fill the .bss area with zeros
Show answer and explanation
Answer: C. Copy the initial values of .data from flash to RAM and fill the .bss area with zeros
RAM loses its contents at power-off, so the initial values of variables are kept in non-volatile flash, and the start-up code copies them to the variables' locations in RAM. The area for uninitialized variables (.bss) is filled with zeros. The linker script gives .data separate addresses: where it is stored in flash and where it is used in RAM at run time. Only after these steps can main begin in the state that C promises.
- A. Nothing is written back.
- B. Ordinary RAM loses its contents at power-off.
- C. Correct. This honors C's promises about initial values.