1
\$\begingroup\$

I know that .data will be in the RAM, but I came across article that says during boot of an micro-controller the initial value of variables is stored in the ROM and then the boot-loader copy the initial values of variables from ROM to RAM, Is this true!?

\$\endgroup\$
1

2 Answers 2

5
\$\begingroup\$

Ask yourself how could the system work if it isn't true?

In most implementations, the .data segment is for initialized data, where .bss is for data that is set to zero. You can't count on RAM being set to anything at all on power-up, so the processor has to zero out the .bss segment and initialize .data.

Usually this is done by storing an image of the start-up value of the .data segment in ROM, and copying it to the correct location in RAM on startup.

\$\endgroup\$
6
  • \$\begingroup\$ the bootloader of laptops and computer also does the same thing!? \$\endgroup\$ Commented Aug 19, 2019 at 20:19
  • 1
    \$\begingroup\$ It is not the bootloader but the startup code of every program stored in ROM regardless of platform. \$\endgroup\$
    – Eugene Sh.
    Commented Aug 19, 2019 at 20:32
  • 1
    \$\begingroup\$ PC bootloaders are a considerably different animal. The BIOS probably does it (most modern-day BIOS's are probably written in C), but the bootload process is multi-step, and the actual operating system comes from disk. I suggest searching on some variation of "how does the PC bootload process work" if you want to study up on that one. \$\endgroup\$
    – TimWescott
    Commented Aug 19, 2019 at 20:33
  • \$\begingroup\$ A modern BIOS is a bunch of tens of firmwares running on different components and loaded from some kind of ROMs at different stages. And yes most (if not all) of these are working this way. \$\endgroup\$
    – Eugene Sh.
    Commented Aug 19, 2019 at 20:37
  • 5
    \$\begingroup\$ No. A bootloader is a separate program which is running before the others and is intended to allow other programs to run. A startup code is a part of the same program, which is running before main (in C) and responsible for setting up the memory, stack and the data ad bss sections as explained for this specific program. Then it is calling main. \$\endgroup\$
    – Eugene Sh.
    Commented Aug 19, 2019 at 20:48
3
\$\begingroup\$

Yes. That's how values of variables get initialized, and the rest of the variables are in .bss section which is normally cleared to zero on startup.

\$\endgroup\$
4
  • \$\begingroup\$ this also happens in big laptops an computer right !? \$\endgroup\$ Commented Aug 19, 2019 at 20:18
  • \$\begingroup\$ Yes if it is a program written in C language for example, C startup code initializes variables for you before main. If it is a program written in assembly, there is no startup code to do anything. \$\endgroup\$
    – Justme
    Commented Aug 19, 2019 at 21:14
  • \$\begingroup\$ why in assembly there is no startup code?! How this operation works in assembly \$\endgroup\$ Commented Aug 19, 2019 at 21:56
  • 2
    \$\begingroup\$ Nobody forces you to do anything in assembly, you can write your startup if you like, or link the assembly with C startup. Feel free to look at how STM32 startup works for example. But this is now off topic for Electrical Engineering \$\endgroup\$
    – Justme
    Commented Aug 20, 2019 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.