6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Nov 21, 2024 9:03 am

All times are UTC




Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Extra stacks
PostPosted: Tue Oct 06, 2020 9:13 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895

My UNLOOP is a primitive. It's only nine bytes.
Code:
CODE UNLOOP  ( S: -- )
             ( R: ADR N1 N2 -- )
   PLA,  PLA,
   PLA,  PLA,
   PLA,  PLA,
   NEXT JMP,
END-CODE

Following a suggestion from Garth Wilson, my do loops have three loop parameters. ADR is the address that LEAVE or ?LEAVE ( if present) and LOOP or +LOOP branch to.
UNLOOP is from the Ansi Forth standard. Note that it doesn't actually exit the word the loop is in nor does it leave the loop. It just discards the loop parameters. This is so a word can be exited from within a do loop or within a nested do loop. There needs to be an UNLOOP for each level of loop nesting.
Here are two hypothetical examples:
Code:
: SOMEWORD  ( -- N1 )
   10 0 DO
      I DO_SOMETHING_WITH_I
      I SOMETEST
      IF  I UNLOOP EXIT  THEN
   LOOP
   TRUE ;

: ANOTHERWORD  ( -- N1 N2 )
   10 0 DO
      25 0 DO
         I J DO_SOMETHING
         I J SOME_OTHER_TEST
         IF  I UNLOOP I UNLOOP EXIT  THEN
      LOOP
   LOOP
   TRUE TRUE ;



Top
 Profile  
Reply with quote  
 Post subject: Re: Extra stacks
PostPosted: Thu Oct 08, 2020 11:59 pm 
Offline

Joined: Sun May 13, 2018 5:49 pm
Posts: 255
JimBoyd wrote:
Has anyone else implemented an extra stack ( or more) in Forth? How did it affect your programming?
Well... now I have. I ended up with:
Code:
( Auxiliary Stack - 2020-10-05        SamCoVT )
( Based on 6502.org discussion.  License: CC0 )

( Create the stack.  AUXTOS points to just *after* TOS )
CREATE AUXSTACK 16 CELLS ALLOT
VARIABLE AUXTOS   AUXSTACK AUXTOS ! ( Start with empty stack. )

: .AS   ( Print the aux stack in Tali2 format )
   ( Print <#items>  at the beginning. )
   ." <"   AUXTOS @ AUXSTACK -   1 CELLS /
   0 <# #S #> TYPE ( Number with no trailing space )
   ." > "
   ( Print the stack values with TOS on the right. )
   AUXSTACK               ( Start at the bottom of the stack. )
   BEGIN DUP AUXTOS @ < WHILE
      DUP @ .   1 CELLS + ( Print and move up the stack. )
   REPEAT  DROP ;

: >A   ( S: n  A: -- S:  A: n )
   AUXTOS @ !  1 CELLS AUXTOS +! ;

: A>   ( S:  A: n -- S: n  A: )
   AUXTOS @ AUXSTACK = IF
      ." AUXSTACK EMPTY"
   ELSE
      -1 CELLS AUXTOS +! AUXTOS @ @
   THEN ;

: A@   ( S:  A: n -- S: n  A: n )
   AUXTOS @ AUXSTACK = IF
      ." AUXSTACK EMPTY"
   ELSE
      AUXTOS @   1 CELLS -   @
   THEN ;

: DUP>A   ( S: n  A: -- S: n  A: n )
   DUP >A ;

: 2>A   ( S: d  A: -- S:  A: d )
   SWAP >A >A ;

: 2A>   ( S:  A: d -- S: d  A: )
  A> A> SWAP ;
I've tested this on Tali2 and Gforth and it looks like it works. I learned the following unrelated things about forth in the process:

>= isn't a standard word?!?! Once I realized I was printing my stack backards, I also discovered that I didn't need this word and < would do just fine. I'm not sure how I didn't run into this before.

. prints a space after the number. If you don't want that, you get to learn about <# and #S and #>

I think this will be handy for when I'm working on a new word that uses >R and R> and R@, like in JimBoyd's example, and I want to try it out a line at a time. I especially like how .AS lets you see what's going on in the pretend return stack. I don't know as I'll use it outside of this use case, but it certainly looks handy for debugging words that use the return stack.

Edit - uppercased all of the forth words in the code for readability


Top
 Profile  
Reply with quote  
 Post subject: Re: Extra stacks
PostPosted: Fri Oct 09, 2020 8:34 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895

How about replacing
Code:
0 <# #S #> TYPE

with
Code:
1 .R



Top
 Profile  
Reply with quote  
 Post subject: Re: Extra stacks
PostPosted: Sat Oct 10, 2020 2:50 pm 
Offline

Joined: Sun May 13, 2018 5:49 pm
Posts: 255
JimBoyd wrote:
Code:
1 .R
That's much better. I didn't realize that .R doesn't put a space after the number - that's useful to know and I think it makes the code more readable. I'm not too sad that I delved into #S and family - I understand them a little better each time I look them up and play with them.


Top
 Profile  
Reply with quote  
 Post subject: Re: Extra stacks
PostPosted: Sat Oct 31, 2020 1:16 am 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895

The return stack normally holds return addresses and DO LOOP parameters. Some Forth implementations may place the loop parameters elsewhere ( which in itself would be another stack, assuming the loops are nestable) but the latest standard indicates that the loop parameters will be on the return stack. The latest Forth standard states that a program may use the return stack for temporary storage subject to certain restrictions. The Forth-83 standard merely states that the return stack may cautiously be used for other values. An auxiliary stack may be used as freely as the data stack, without any of these restrictions. Nothing surprising there. I wonder, are we are so used to the restrictions with the return stack that we apply the same restrictions to an auxiliary stack?


Top
 Profile  
Reply with quote  
 Post subject: Re: Extra stacks
PostPosted: Sun Aug 06, 2023 9:55 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
GARTHWILSON wrote:
Jim, you have quite a few words above that are neither part of any standard I know of, nor defined above. One I'll ask about however is REDEFINE:.

I already mentioned the string "REDEFINE:" is a message from CREATE to let me know I redefined a word. I've changed that. CREATE will now print the name of the word followed by the string "EXISTS" when there is a redefinition.
Code:
: TEST ;  OK
: TEST ;
TEST EXISTS
 OK

One point to keep in mind with these session logs is that the printout does not show a distinction between what the programmer types and the computer's response. It is a faithful printout of the text sent to the computer screen.
Attachment:
Fleet Forth word redefinition.png
Fleet Forth word redefinition.png [ 14.15 KiB | Viewed 3516 times ]



Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: