top of page

Microcontroller Memory Layout

  • Writer: Ashok Kumar Kumawat
    Ashok Kumar Kumawat
  • Feb 3
  • 2 min read

In my corporate days, when we had to fit a complex automotive airbag driver into a tiny microcontroller with only 32KB of memory, we had to know exactly where every single bit lived.

​To understand how a C program is stored, you have to look at the Memory Layout. A microcontroller doesn't have a hard drive; it has Flash (ROM) and SRAM (RAM).


1. The "Storage" (Flash vs. RAM)

​When you turn the power off, your code stays on the chip. That’s because the actual instructions are burned into the Flash Memory. But your variables—the data that changes—need to live in SRAM.

​2. The Five Main Segments

​When you compile your code, the "Linker" (the architect of your program) carves the memory into five distinct sections:

​A. The Code Segment (Text)

  • What it is: Your actual compiled instructions (the logic).

  • Where it lives: Flash.

  • Technical Note: This is Read-Only. The CPU fetches instructions from here to execute them.

​B. The Data Segment (.data)

  • What it is: Global and static variables that have a starting value.

  • Example: int engine_temp = 90;

  • Where it lives: It’s stored in Flash (to keep the 90) but copied to RAM at startup so it can be changed.

​C. The BSS Segment (.bss)

  • What it is: Global and static variables that are not initialized (or set to zero).

  • Example: int wheel_speed;

  • Where it lives: RAM. At startup, the microcontroller runs a tiny bit of code to "zero out" this entire section.

​D. The Stack

  • What it is: Temporary storage for local variables and function return addresses.

  • Where it lives: RAM.

  • How it works: It grows and shrinks automatically as you call and exit functions (LIFO: Last-In, First-Out).

​E. The Heap

  • What it is: Memory used for malloc().

  • Where it lives: RAM.

  • Corporate Tip: In safety-critical automotive systems, we never use the Heap. It’s too risky because it can cause "Memory Fragmentation" which might crash the car after 100 hours of driving.

​3. The "Startup" Process (From Power-On to main)

​Between hitting the "Power" button and your main() function running, the chip does a secret dance called the Reset Handler:

  1. Vector Table: The CPU looks at a specific address in Flash to find the "Reset Vector" (the starting point).

  2. Stack Pointer Initialization: It sets up the "Stack" area in RAM so functions can start working.

  3. Data Initialization: It copies the .data segment from Flash to RAM.

  4. BSS Clearing: It fills the .bss segment in RAM with zeros.

  5. Jump to Main: Finally, it calls your main() function.

​4. Summary Table for your Interview

Segment

Content

Memory Type

Persistence (Power Off)

.text

Compiled Code

Flash

Remains

.data

Initialized Globals

Flash & RAM

Initial value remains in Flash

.bss

Uninitialized Globals

RAM

Lost (Reset to 0)

Stack

Local Variables

RAM

Lost

Heap

Dynamic Memory

RAM

Lost

Pro-Tip: If you ever run out of RAM on your Jacinto SoC, check your Stack size in the Linker Script (.ld file). If the Stack grows too big and hits the Data segment, your program will crash—this is the famous Stack Overflow.

Recent Posts

See All
Q&A: C Code

Q1: How do you find the size of an array in C? Q2: What is an array of pointers in C, and why is it useful? Q3: How can function pointers be used to trigger callbacks in C? Q4: How can we determine th

 
 
 
Bubble Sort Concept

Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. After each pass, the largest element "bubbles up" to its correct position at the end. C Code Example #i

 
 
 
Storage Classes in Embedded C

Excellent, let’s now systematically cover Storage Classes in Embedded C with the structured breakdown you asked for. 1. Concept Overview Storage classes define scope, lifetime, and visibility of varia

 
 
 

Comments


© 2035 by Robert Caro. Powered and secured by Wix

bottom of page