r/C_Programming • u/grimvian • Aug 18 '26
Not exactly C but the concept of the stack
I'm in my third year of happy C coding but I learned 6502 assembler in 1984 and now 70 years old. Therefore I often look at videos like: NES Mapper History to 1988 and Blaster Master Code Analysis - Talkin' Code Ep. 1
The mention of the stack made me think if the 6502 was had a bit of inspiration from C, but it was a lot older concept.
NES Mapper History to 1988 and Blaster Master Code Analysis - Talkin' Code Ep. 1
https://www.youtube.com/watch?v=EhO_iHkUkE0
31
Upvotes
2
u/timrprobocom Aug 19 '26
"inspiration from C but it was a lot older concept" -- definitely not. C was first developed in 1972. The 6502 came out in 1975.
C is a lot older than people think, but the concept of a stack dates back to the late 1950s.
25
u/Zirias_FreeBSD Aug 18 '26
Didn't look at the links here, but regarding
Another reasoning to sort that out: The 6502 does have a hardware stack (yep, same concept, yep, older than C), but it is utterly unusable for C purposes:
$0100)$100/256bytes)Aregister)That's the reason C implementations targeting MOS6502 typically introduce a "C stack" implemented entirely in software.
5
u/Beginning-Junket8979 Aug 19 '26
Yeah hardware stacks are pretty cool for making super lean code in tiny handwritten asm programs (I remember doing this on microchip pics forever ago), but they don't scale well in general for modern C since call stacks tend to be pretty deep even without recursion and most C code ends up running on multicore CPUs.
Like POSIX requires a minimum of 16KB for a thread stack, but 8MB is standard in glibc on modern Linux machines.
To actually make a HW stack fast while saving cost across several ghz multicore CPU running a multithreaded OS you'd probably need some kind of secondary cache dedicated to call stacks such that a context switch stalls on loading your fastest cache first to load immediate scope locals and then prefetch loading the area "behind" the current SP and for normal operation tie in with a next function call aware branch predictor, and then we probably get another critical CVE rendering the whole feature useless.
Also for a batched compute load for data heavy operations and a shallow stack then leave you with a huge cache sitting there cold and unused while you fill up "normal" cache.
So general purpose cache and a sw stack wins on a modern general purpose CPU and already basically fulfills the same role, just slightly less effeciently than massive dedicated HW stack could.
3
u/Eeriecurrence Aug 18 '26
I keep forgetting programmers can be old
2
u/knouqs Aug 18 '26
Thanks for sharing!
2
u/catladywitch Aug 21 '26
Very tangential to your post, but something that I'm still surprised about is that when browsing through decompiled motorola 68k games (Sonic the Hedgehog etc) I've never come across link/unlk instructions. I don't know if that's an artifact of decompilation, but typically there are no local variables to speak of outside of the registers. What is prevalent though is struct-like chunks of memory where each offset is a property of, say, an enemy. I know some old games were written in C (famously Sonic Spinball) and that compilers are so good now that present-day homebrew is done in C with no performance penalty, but I've never seen any disasms of that.
3
u/pOmelchenko Aug 18 '26
Ohhh if i start learning 6502 asm now i‘ll be 70 years old too?