5.5.16 · D1 · HinglishEmbedded Systems & Real-Time Software

FoundationsStartup code — vector table, reset handler, stack initialization

3,415 words16 min read↑ Read in English

5.5.16 · D1 · Coding › Embedded Systems & Real-Time Software › Startup code — vector table, reset handler, stack initializa

Parent note padhne se pehle, aapko uski vocabulary chahiye. Yeh page har term aur notation ko bilkul zero se build karta hai — CPUs, memory, compilers ya linkers ki koi prior knowledge assume nahi ki gayi. Upar se neeche padho; har block uske upar wale par lean karta hai. Do words jo aapko jaldi milenge — reset handler aur linker — yahan ek line ka preview milega aur baad mein full definition:


1. Memory: ek badi numbered wall of boxes

0x prefix ka matlab hai number hexadecimal (base-16) mein likha hai — bade addresses likhne ka ek compact tarika. 0x10 sixteen hai, 0xFF 255 hai. Engineers hex use karte hain kyunki memory addresses isme tidy lagte hain.

Figure s01 neeche yeh wall draw karta hai: paanch boxes ek row mein, har ek apne address 0x00, 0x04, 0x08, … ke saath label hua. Red note yaad dilata hai ki addresses 4 se jump karte hain kyunki ek word 4 boxes span karta hai. Yeh picture dimag mein rakho: baad ka har step "us box par jao, word padho ya likho" hai.

Figure — Startup code — vector table, reset handler, stack initialization

Related layout ideas Stack vs Heap memory layout mein hain.


2. Flash vs RAM — do bilkul alag shelves

Figure s02 unhe do boxes ke roop mein contrast karta hai: Flash (black) mein aapka code aur init value 42 hai; RAM (black) woh jagah hai jahan variable g runtime par actually rehta hai lekin garbage se start karta hai. Unke beech ka red arrow is poore page ka sabse important action hai — Flash se RAM mein 42 ki copy. Reset handler jo kuch karta hai woh uss ek red arrow aur ek wipe mein capture hota hai.

Figure — Startup code — vector table, reset handler, stack initialization

3. CPU, Program Counter, aur "executing"

Cortex-M machine model par zyada: ARM Cortex-M exception and interrupt model.


4. Stack aur Stack Pointer (SP / MSP)

Figure s03 memory ko vertically khada karta hai. Top marker _estack hai; uske neeche do pushed items hain (ek return address aur ek local variable). Left par red arrow SP hai, hamesha last written item ki taraf point karta hua; right par red arrow growth direction dikhata hai — neeche ki taraf, kyunki har push pehle 4 subtract karta hai, phir likhta hai. Figure ko top-to-bottom padhein — "high addresses upar, stack neeche low addresses ki taraf eat karta hua."

Figure — Startup code — vector table, reset handler, stack initialization

Heap ke saath compare karo Stack vs Heap memory layout mein.


5. Pointers aur function pointers


6. Sections: .text, .rodata, .data, .bss

Figure s04 sections ko line up karta hai aur har ek ko woh action label karta hai jo boot code karta hai: .text aur .rodata (kuch nahi — pehle se Flash mein hain, directly padhe jaate hain), .data red mein (COPY Flash→RAM), aur .bss (WIPE to 0). .data par red highlight woh ek section mark karta hai jise Figure s02 ki copy chahiye. Figure padhna mental sorting rule sikhata hai: "startup par mujhpar kya hona chahiye?"

Figure — Startup code — vector table, reset handler, stack initialization

Full detail: Linker scripts and memory sections (.text .data .bss).


7. Linker symbols: _sdata, _edata, _sbss, _ebss, _sidata, _estack

Details Linker scripts and memory sections (.text .data .bss) aur The C runtime and crt0 mein.


8. Vector table (sab kuch ek saath)

Ab aapke paas use padhne ke liye har piece hai:

  • Yeh ek array hai (Section 1: numbered boxes ek row mein, 4-byte aligned).
  • Iske entries addresses of code hain = function pointers (Section 5).
  • Except entry 0, jo stack-top address _estack hai (Section 4 & 7).

Is table ko relocate karna Bootloaders and VTOR relocation mein hai. VTOR jaise registers Volatile, memory-mapped registers and hardware init ke through touch kiye jaate hain.


9. volatile aur memory-mapped registers (one-line primer)


Prerequisite map

Yeh diagram kaise padhein: har box upar ke sections se ek foundation hai; ek arrow "A → B" ka matlab hai "B samajhne ke liye A chahiye." Upar se koi bhi path follow karo aur aap exactly woh order walk kar rahe ho jo yeh page sikhata hai. Har path eventually "Startup code topic" box mein feed hota hai neeche — woh parent note hai jiske liye aap prepare kar rahe ho. Do beech ke merge-points (reset handler ka copy/zero kaam, aur vector table) woh jagah hain jahan kai foundations aek saath aate hain, toh yeh woh ideas hain jo zyada over-learning ke laayak hain.

Memory as numbered boxes

Flash vs RAM

Pointers and function pointers

CPU and Program Counter

Alignment and byte order

Sections text rodata data bss

Linker boundary symbols

Stack and Stack Pointer

Full-descending stack

Vector table as address array

Reset handler copies and zeros

Startup code topic


Equipment checklist

Khud test karo — parent note kholne se pehle har ek obvious feel hona chahiye.

An address is
memory mein ek box (byte) par stamped permanent number.
0x in front of a number means
number hexadecimal (base-16) mein likha gaya hai.
The underscore inside a number like 0x2000_5000 means
value ke baare mein kuch nahi — yeh bas readability ke liye ek digit separator hai, jaise comma.
A word on Cortex-M is
4 bytes (32 bits), toh words 4 apart addresses par baithte hain.
"Aligned" means
address value ke size ka multiple hai; ek word 4 ke multiple par start hona chahiye, nahi toh access fault kar sakta hai.
Little-endian means
ek multi-byte value ka least-significant byte lowest address par stored hota hai.
Flash keeps its contents when
power off ho; RAM ke contents power-up par undefined hote hain.
The Program Counter (PC) holds
next instruction ka address; jumping = isme naya address likhna.
The Stack Pointer (SP/MSP) holds
stack ke current top ka address.
"Full-descending" means
SP last used item par point karta hai, aur push pehle 4 subtract karta hai, phir likhta hai — stack downward grow karta hai.
A function pointer is
ek variable jo function ki pehli instruction ka address rakhta hai; type void (*)(void).
The reset handler is
aapke code ka pehla piece jo power-on ke baad run hota hai; .data copy karta hai, .bss zero karta hai, phir main call karta hai.
The linker is
woh tool jo har function aur variable ko address assign karta hai aur program glue karta hai, boundary symbols drop karte hue.
.rodata holds
read-only constants jaise string literals; yeh Flash mein rehte hain aur directly padhe jaate hain, koi copy ya zero nahi chahiye.
.data holds
non-zero initial values wale globals, startup par Flash se RAM mein copy kiye jaate hain.
.bss holds
zero-initialized globals, startup par 0 se wipe kiye jaate hain (Flash mein stored nahi).
_estack is
stack ka top — RAM ke end ke baad first invalid byte (alignment ke hisaab se rounded), reset par MSP mein load hota hai.
_sidata is
Flash address jahan .data ki initial values stored hain, RAM mein copy karne ke liye.
We use &_sdata (its address) not _sdata because
linker symbols ka meaning sirf unke address mein hai, ek region boundary mark karte hue.
*dst++ = *src++ does
src jo point karta hai uski value dst jo point karta hai wahan copy karo, phir dono pointers advance karo.