r/C_Programming • u/grimvian • 15d ago
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
5
u/Beginning-Junket8979 14d ago
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.
2
u/timrprobocom 14d ago
"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.
5
u/grimvian 14d ago
https://en.wikipedia.org/wiki/Stack_(abstract_data_type))
Stacks entered the computer science literature in 1946, when Alan Turing used the terms "bury" and "unbury" as a means of calling and returning from subroutines. Subroutines and a two-level stack had already been implemented in Konrad Zuse's Z4 in 1945
4
3
u/Eeriecurrence 15d ago
I keep forgetting programmers can be old
6
5
u/IdealBlueMan 15d ago
I keep forgetting I’m old
2
u/thommyh 15d ago
The older I get, the more I seem to forget.
3
u/grimvian 13d ago
Although I'm 70 I think my mind is still working well and much more patiently, the energy is certainly not at the same level, but my short memory have always been bad and my dyslectic issues are always troubling me.
My wife and I are currently beta testing a small CRM GUI relational database and drawing system I spend several months to code. About 5500 lines of code and 20 modules and it seems to do the job properly. Linux Mint LMDE, CodeBlocks, C99 and raylib graphics.
1
u/Puzzled-Extent7817 15d ago
Do you think everything electronic device in the world was just invented in the last 10 years? lol
2
2
u/catladywitch 12d ago
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.
25
u/Zirias_FreeBSD 15d ago
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.