C Board  

Go Back   C Board > Community Boards > Tech Board

Reply
 
LinkBack Thread Tools Display Modes
Old 06-25-2008, 09:13 AM   #1
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Processor with no stack?

In the C forum, we had this discussion:
How to calculate the Stack Size?

In it, my code was slated because it assumes that there is a stack. Of course, C being a function-based language, there needs to be SOME FORM OF stack, even if the stack is managed by software (such as the Deep Blue C for 6502 - since 6502 doesn't have a processor stack that is worth anything, it's 256 bytes long - hardly enough for passing arguments and calling functions in any reasonable C program). .

But can someone tell me if there is any machines in current production use (not counting machines kept alive in a museum somewhere) that doesn't have a conventional stack - besides pointing out that some machines have TWO stacks [e.g. 29K that has a "register" stack and a "memory" stack].

I'm just curious as to what machines (today) would not have a stack in a meaningful way.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 06-25-2008, 09:43 AM   #2
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,614
If you allow for a single call-return stack as being generally considered "stack absent", then you have a multitude of processors that can be considered not having a stack. Albeit their use is probably limited to only specialized tasks (coffee machine processors )

But looking at the original thread I cannot conceive in any way as meaningful for the solution presented the fact some processors may not offer a stack or offer only a single call-return stack. Micro-controllers, my $1 calculator, all have a stack and stack pointer.

...

BTW, for fun... they even have or plan to have OSes for these processors http://www.patentstorm.us/patents/6823517.html
__________________
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.


Mario F. is offline   Reply With Quote
Old 06-25-2008, 10:01 AM   #3
Malum in se
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 3,188
No, the common microcontrollers all use stack based processors. There really arent any completely stackless μP or μC that I can think of. Stacks are such a basic advance it would be itentionally crippling the processor to not use one. There would be no performance gain, unless you count having one fewer registers, or being able to use the SP for something else.

__________________
Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.
abachler is offline   Reply With Quote
Old 06-25-2008, 06:59 PM   #4
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,811
I cannot imagine a CPU not having a stack. I'm fairly sure I cannot come up with any that I even know of off-hand.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 06-25-2008, 09:19 PM   #5
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 5,005
I had seemed to recall an HC05 I worked with as a 'stackless' device. But the data sheet shows it too had one.
Quote:
The 64 addresses from $00C0 to $00FF serve as both the user RAM and the stack RAM. The CPU uses five stack RAM bytes to save all CPU register contents before processing an interrupt. During a subroutine call, the CPU uses two bytes to store the return address. The stack pointer decrements during pushes and increments during pulls.
The toolchain did a neat job of simulating a stack for parameterized function calls -- the map generated looked into all possibilities of 'stack depth' and chose to share memory appropriately.

Trivia: this device also had a meaningful address 0.
__________________
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Dave_Sinkula is offline   Reply With Quote
Old 06-26-2008, 09:16 AM   #6
Registered User
 
Join Date: Apr 2008
Posts: 19
Most (if not all) of the Microchip PIC uControllers lack a stack that the programmer can use. There is a stack but it just stores return vectors for subroutines and interrupts. There is nothing there to use for automatic variables and the like.
birkelbach is offline   Reply With Quote
Old 06-26-2008, 09:31 AM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by birkelbach View Post
Most (if not all) of the Microchip PIC uControllers lack a stack that the programmer can use. There is a stack but it just stores return vectors for subroutines and interrupts. There is nothing there to use for automatic variables and the like.
Is there a C compiler for those devices? If so, how do they solve the stack problem?

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 06-26-2008, 04:56 PM   #8
Registered User
 
Join Date: Apr 2008
Posts: 19
There are some C compilers, but I've done all my PIC programming in MPASM (Assembly Language) so I don't really know. I suspect that the compiler allocates a spot in memory to use as a stack pointer and then uses indirection for all the arguments and automatic memory. Unless there is only one argument, then it'd just use the single working register. Most of my subroutines just use the working register. I guess you could use the W (working register) as a pointer to arguments, and then use some predefined memory for automatic variables. Then you can only call subroutines one deep, or allocate some memory for each subroutine. You'll run out of memory real quick like this because RAM is measured in bytes on these machines. A big one has 2k bytes.

The hardware stack is typically only 8 words deep, so you can't nest subroutines very far anyway. Don't forget to save one for the interrupt routine too. (Don't ask me how I know that) Recursions will get you in big trouble real quick. :-)
birkelbach is offline   Reply With Quote
Old 06-26-2008, 05:48 PM   #9
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,018
it seems that it would be easy enough to set up a stack. just reserve a register as the stack pointer, set it to the desired memory location and replace push with:

dec SP
mov DATA -> (SP)

and pop with:

mov (SP) -> DATA
inc SP
Sebastiani is offline   Reply With Quote
Old 06-27-2008, 10:39 AM   #10
Registered User
 
Join Date: Apr 2008
Posts: 19
Yeah that's the basic idea. It takes several more instructions on the PIC. i.e. there are no indirect moves, rather a pair of registers to use for indirection, also there are bank select bits that have to be handled carefully to access the right registers. But I'm pretty sure that is the way the compilers handle it unless it's a really simple function.
birkelbach is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
stack and pointer problem ramaadhitia C Programming 2 09-11-2006 11:41 PM
infix evaluation using stack lewissi C++ Programming 0 11-03-2005 02:56 AM
Question about a stack using array of pointers Ricochet C++ Programming 6 11-17-2003 10:12 PM
error trying to compile stack program KristTlove C++ Programming 2 11-03-2003 06:27 PM
Stack Program Here Troll_King C Programming 7 10-15-2001 05:36 PM


All times are GMT -6. The time now is 04:53 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22