5.1.10 · D1 · Hardware › Instruction Set Architecture (ISA) › Calling conventions and ABI
CPU sirf registers (chip ke andar tiny named boxes) aur memory (bahar ek badi numbered strip) jaanta hai — usse bilkul pata nahi ki "function argument" ya "return value" kya hota hai. Calling convention ek bana-banaya rulebook hai jise alag-alag compile hue do code pieces dono maante hain, taaki "argument yahan rakho, answer wahan dhundo" mutual agreement se sach ho jaye, hardware se nahi.
Yeh page har word, register name, aur symbol ko build karta hai jo parent note tumhare saamne fek deta hai, ek aisi reader ke liye shuru karke jisne kabhi assembly dekhi hi nahi. Neeche kuch bhi uss par depend nahi karta jo usse upar hai.
Register ek tiny, extremely fast storage slot hai jo CPU ke andar hota hai. Yeh ek machine word hold karta hai (x86-64 par yeh 64 bits = 8 bytes hota hai). Inki ginti bahut kam hai, aur har ek ka ek fixed name hota hai jaise rax, rdi, rbx.
Figure dekho: CPU left wala box hai, jisme labelled cells ki ek chhoti row hai. Bas itne hi registers hote hain. Memory right wali lambi strip hai.
Intuition Calling conventions mein registers kyun matter karte hain
Kyunki inki ginti bahut kam hai aur yeh sab ke beech shared hain, jis second tum kisi aur function ko call karte ho woh uss box ko overwrite kar sakta hai jise tum use kar rahe the. Poora "kaun kya save kare" wala problem sirf isliye exist karta hai kyunki registers kam hain aur shared hain . Jo kuch bhi parent note "caller-saved / callee-saved" kehta hai woh sab in boxes ke liye ek ladai hai.
rax, rdi, rsp mein r ka matlab sirf hai "register, 64-bit". Baad ke letters historical hain (a = accumulator, di = destination index, sp = stack pointer). Inhe boxes ke naam samjho, bas itna hi. Poore set ke liye Registers and Register File dekho.
Definition Memory aur address
Memory byte-sized cells ki ek lambi line hai, jisme har ek ka ek number hota hai jise us ka address kehte hain. Address 0 ek taraf hai; addresses doosri taraf badhte jaate hain. Jab hum [ rsp ] likhte hain toh matlab hai "woh memory cell jiska address rsp box mein abhi jo number hai woh hai".
Square brackets [ ⋅ ] woh sabse important notation hai jo parent note bina define kiye use karta hai:
Bina brackets ke = box ki apni value. Brackets ke saath = box ko ek arrow ki tarah follow karke memory mein jao.
Reveal-test:
rsp mein jo value haiek number jo ek address hai (memory mein ek location)
[rsp]us location par actually stored data
Stack memory ka ek region hai jo temporary values ki pile ki tarah use hota hai. Ek special register, rsp (stack pointer) , hamesha pile ke top ka address hold karta hai. x86-64 par stack chhote addresses ki taraf badhta hai: push rsp se subtract karta hai, pop isme add karta hai.
"Neeche ki taraf badhta hai" wala fact explain karta hai ki parent kyun rsp ← rsp − ( frame size ) likhta hai: jagah banana matlab neeche jaana.
Intuition Stack kyun, aur sirf zyada registers kyun nahi?
Functions call karte hain functions jo call karte hain functions (dekho Recursion and Activation Records ). Har ek ko apna private scratch space chahiye, aur yeh perfectly nest hone chahiye — jo sabse baad start hua woh sabse pehle finish hota hai. Stack (last-in-first-out) "nested calls" ki exact shape hai, isliye yeh har function ki local variables aur saved values ka natural ghar hai. Joh chunk ek function ka hota hai woh uska stack frame hai.
Common mistake "Stack upar ki taraf badhta hai, plates ki stack ki tarah."
Kyun sahi lagta hai: plates ki physical stack unchi hoti jaati hai.
Fix: x86-64 par stack badhne ke saath addresses chhote hote jaate hain. Stack ka top sabse chhota address hai jo use mein hai. Push = subtract, pop = add. Memory ko address 0 upar rakh ke draw karo aur tum dekhoge ki yeh page pe neeche ki taraf badhta hai.
Definition rip aur char core instructions
rip (instruction pointer) : woh box jisme us next instruction ka address hai jo CPU chalayega. Code mein aage badhna = rip ko increment karna.
push X ::= rsp se 8 subtract karo, phir X ko [rsp] par store karo.
pop X ::= [rsp] se X load karo, phir rsp mein 8 add karo.
call f ::= call ke baad wala address (woh return address ) stack par push karo, phir rip = f set karo.
ret ::= stack se ek value rip mein pop karo (return address par wapas jump karo).
≡ (ya ::=) symbol jo parent use karta hai ka matlab sirf hai "bilkul same hi define kiya gaya hai ". Toh call f ≡ push rip_next; jmp f padha jaata hai: "call instruction sirf ek push aur ek jump hai." Kuch magical nahi — yeh upar wale pieces se bana hai.
Intuition Return address stack par kyun jaata hai
Jab f khatam hota hai toh use caller ko resume karna hota hai. Caller ki location kahin record honi chahiye jahan f use bina pehle arrangement ke dhundh sake. Stack kaam karta hai kyunki f ka apna frame bilkul uske upar baitha hota hai — return address caller aur callee ke beech ki boundary hai. Exactly isliye parent ke Example 2 mein 7th argument [rsp+8] par milta hai: [rsp] return address hai, aur argument bilkul uske upar hai.
Definition Argument / return value (ek convention ki tarah)
Argument woh value hai jo caller callee ko dena chahta hai. Return value woh answer hai jo wapas diya jaata hai. Yeh dono hardware mein exist nahi karte — ek convention inhe invent karta hai yeh declare karke: "pehla integer argument rdi mein rehta hai, answer rax mein wapas aata hai," wagera.
Definition Alignment symbol
rsp ≡ 8 ( mod 16 )
padha jaata hai "rsp ko 16 se divide karne par 8 remainder milta hai ". Notation a ≡ b ( mod m ) ka matlab hai ki a aur b ko m se divide karne par same remainder aata hai. Concretely: function entry par rsp koi aisa number hona chahiye jaise … , 8 , 24 , 40 — 16 ke multiple se 8 zyada. Yeh ek rule hai taaki 16-byte-wide SSE loads aligned addresses par padein.
Reveal-test:
40 ≡ 8 ( mod 16 ) ?haan — 40 = 2 × 16 + 8 , remainder 8
32 ≡ 8 ( mod 16 ) ?nahi — 32 , 16 ka multiple hai, remainder 0
Register kya hota hai? CPU ke andar ek tiny fast named storage box, jo ek 64-bit word hold karta hai; bas kuch hi hote hain.
rsp mein kya hota hai?Stack ke current top ka address.
[rsp+8] ka kya matlab hai?Us address par memory contents jo (rsp mein value) + 8 hai.
x86-64 par stack kis direction mein badhta hai? Neeche ki taraf — chhote addresses ki taraf; push rsp se subtract karta hai, pop add karta hai.
call f simple pieces mein kya karta hai?Return address stack par push karo, phir f par jump karo (rip = f set karo).
ret kya karta hai?Return address ko stack se rip mein pop karo, caller ko resume karo.
Arguments aur return values ko convention ki zarurat hi kyun hai? CPU ka unka koi concept nahi hai; sirf fixed registers/slots par agreement hi "argument" aur "return value" ko meaningful banati hai.
Function entry par rsp ≡ 8 (mod 16) ka kya matlab hai? rsp ko 16 se divide karne par remainder 8 aata hai, toh call se pehle stack 16-aligned tha aur call ne 8-byte return address push kiya.
"calling convention ⊂ ABI" mein ⊂ ka kya matlab hai? "Ka hissa hai / subset hai" — convention bade ABI rulebook ka ek chapter hai.