Processor with no stack?

This is a discussion on Processor with no stack? within the Tech Board forums, part of the Community Boards category; In the C forum, we had this discussion: http://cboard.cprogramming.com/showthread.php?t=104435 In it, my code was slated because it assumes that there ...

  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677

    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.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    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
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    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.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I had seemed to recall an HC05 I worked with as a 'stackless' device. But the data sheet shows it too had one.
    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.*

  6. #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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  8. #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. :-)

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

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