Menu

Re: [mecrisp:discussion] Noob question: How to reverse a deep data stack without using an explicit buffer?

2018-12-18
2018-12-18
  • Reginald Beardsley

    What ever got either of you thinking that the stack was for anything but passing parameters to and from words? What books on forth did you read? I have two by Leo Brodie. Both seem to me rather confused and confusing. More harmful than beneficial. It makes my head hurt just to read a few pages. Rather than make something complex, simple, he makes something simple, complex.

    Every time a word pushes output such as the address of a buffer and the length on the stack you have created unnamed variables. The intent is these unnamed variables are the input to the next word.

    If that's not the case, you store them to named variables. In all other programming languages with the possible exception of Lisp with which I am not really all that familiar, the arguments passed to and from subroutines have to be named variables. Forth does not require this. But unless you use a "SAVE" statement in F77 or "static" in C all local variables on modern machines are allocated on the stack. Note that in C, "static" attached to a global variable in a file restricts the scope of the variable to the routines in that file. So the semantics are different for global and local variables.

    Forth is YAPL, Yet Another Programming Language. The use of the stack to pass parameters is not unique to forth. To the best of my knowledge, every programming language pushes the arguments on the stack when it calls a subroutine and pops the return value, if any, off the stack after the call. The caller also pushes the return address on the return stack. That is why it is called the return stack.


    On Tue, 12/18/18, Terry Porter techman001@users.sourceforge.net wrote:

    Amazing, I thought it was just me!

    The silly thing is that I don't have a
    problem using constants and arrays etc.

    Then there is the fact that the
    challenge to write a self contained Word that does cool
    stuff without needing any variables is just too hard for me
    to resist. I've spent hours/days trying to find ways to get
    rid of variables for that one.perfect.Word.

    Calling it an addiction is probably not
    far from the truth.

    I've been pigging out on Forth since
    2014 ...  at least Forth is lowcal!

     
    • Peter Rabbit

      Peter Rabbit - 2018-12-18

      What ever got either of you thinking that the stack was for anything but passing parameters to and from words? What books on forth did you read? I have two by Leo Brodie. Both seem to me rather confused and confusing.

      Yup, I think the brainwashing can be traced to that.

       
  • Terry Porter

    Terry Porter - 2018-12-18

    What ever got either of you thinking that the stack was for anything but passing parameters to and from words?

    In my case, using Forth for my projects. I still learn by breaking things, a method that has served me well since I was a baby, 64 years ago.

    My reasons for using the Return Stack as a Local Variable
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1) My example Split program saves 24 Bytes (a 36% saving) using the Return Stack , v/s a Global Variable. In embedded, memory is finite and costly to increase.
    2) I prefer Words to be self contained so I don't have to keep track of Global Variables
    3) The challenge of not using Global Variables is a bit addictive. What's life without a few addictions ?

    http://hightechdoc.net/mecrisp-stellaris/_build/html/devtips.html?highlight=return#balancing-the-return-stack

     

    Last edit: Terry Porter 2018-12-19

Log in to post a comment.