6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Nov 14, 2024 6:33 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: -TRAILING
PostPosted: Tue Sep 24, 2024 6:27 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
-TRAILING is a Forth word which takes an address and character count of a string. It adjusts the character count to exclude trailing spaces.
I used to think -TRAILING should be implemented as a DO LOOP
Code:
: -TRAILING  ( ADR CNT1 -- ADR CNT2 )
   DUP 0
   ?DO
      2DUP + 1- C@ BL <> ?LEAVE
      1-
   LOOP ;

However, -TRAILING can be made smaller by implementing it as a BEGIN loop.
Code:
: -TRAILING
   BEGIN
      DUP
   WHILE
      1-  2DUP +  C@ BL <>
   UNTIL
      1+
   THEN ;

I did not originally set out to write this high level version of -TRAILING . I wanted a faster version of -TRAILING and wrote it as a primitive.
Code:
CODE -TRAILING
   BEGIN
      0 ,X LDA  1 ,X ORA
      NEXT.JMP 0= BRAN
      0 ,X LDA
      0= IF  1 ,X DEC  THEN
      0 ,X DEC
      CLC
      0 ,X LDA  2 ,X ADC  N STA
      1 ,X LDA  3 ,X ADC  N 1+ STA
      N )Y LDA  BL # CMP
   0= NOT UNTIL
   ' 1+ @ JMP
   END-CODE

This is from the source for my Forth's kernel. It is for a metacompiler and NEXT.JMP is a LABEL in the source for the previously defined word.
Code:
   LABEL NEXT.JMP
   NEXT JMP  END-CODE

Although I tested this new version of -TRAILING , it would have been nice to single step through it from within Forth (to see what happens to the values on the data stack).
My Forth has the word TRACE to trace the execution of high level Forth words. TRACE can not trace the execution of a primitive so I wrote a high level version of -TRAILING which works similarly to the primitive version. This version I was able to trace and it is the version I presented as the smaller version of -TRAILING . I will not be using this smaller high level version of -TRAILING . The primitive version is larger but much faster; however, I think the smaller high level version is a good reference implementation for -TRAILING .


Top
 Profile  
Reply with quote  
 Post subject: Re: -TRAILING
PostPosted: Tue Sep 24, 2024 7:26 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8540
Location: Southern California
Do you need strings more than 254 bytes long?  If not, you can shorten it further.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: -TRAILING
PostPosted: Tue Sep 24, 2024 7:38 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
The Forth-83 Standard as well as the ANSI Forth standard seem to indicate that -TRAILING can handle strings much longer than 254 bytes. Removing the ability to handle long strings is not, in my opinion, worth saving a few bytes.


Top
 Profile  
Reply with quote  
 Post subject: Re: -TRAILING
PostPosted: Tue Sep 24, 2024 8:47 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8540
Location: Southern California
I've gone for the shorter strings for my applications, as it shortens several other words too.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: -TRAILING
PostPosted: Sat Sep 28, 2024 10:23 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
Before I started this thread I added -TRAILING to WHERE , part of my Forth's error handling.
Before I added -TRAILING to WHERE , loading the following block
Code:
0:
1: CREATE
2:
3:
4:
5:
6:
7:
8:
9:
A:
B:
C:
D:
E:
F:

produced this error message.
Code:
SCR# 16385 LINE# 16
@@@@@@@@@@@@@@@@@[L.@@.@U..@@@@@................................
^
NAME?

Adding -TRAILING to WHERE changes the error message to this.
Code:
SCR# 16385 LINE# 1
CREATE
       ^
NAME?

For this to work -TRAILING must work with strings that could be 1024 bytes in size.


Top
 Profile  
Reply with quote  
 Post subject: Re: -TRAILING
PostPosted: Sun Sep 29, 2024 3:12 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8540
Location: Southern California
Ah-ha!  So you need a 1K block to be able to be a string.  I think Julian Noble said his book "Scientific Forth" was typeset in Forth too, and he needed a whole page to be handled as a single string.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: -TRAILING
PostPosted: Sun Sep 29, 2024 8:48 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
When loading a block, yes. When loading a block the text interpreter treats the contents of that block as a 1K string. When not loading a block the text interpreter treats the contents of the Text Input Buffer as a string with the length of #TIB . INTERPRET uses WORD to parse text and WORD uses 'STREAM to obtain the address and length of the remainder of the text stream for the TIB or a BLOCK
Code:
: 'STREAM  ( -- ADR N )
   BLK @ ?DUP
   IF
      BLOCK B/BUF
   ELSE
      TIB #TIB @
   THEN
   >IN @
   OVER UMIN /STRING ;

It is easier to let the interpreter treat the entire block as a single string than parse it sixty-four characters at a time. It's also faster as the extra code would make 'STREAM and therefore WORD slower.
Treating a screen in a block as sixteen lines of sixty-four characters is an editing convenience. This is also why the word to comment to the end of a line in my Forth ( backslash in most Forths, but a double forward slash in my C64 Forth) increments the value of >IN to the next multiple of sixty-four when loading a block or increments it to past the text stream when not loading a block.
Code:
: //  ( -- )
   'STREAM  BLK @
   IF  C/L MOD  THEN
   NIP >IN +! ; IMMEDIATE

A block does not always have to be a screen of source code. I use blocks with numbers so high they are in the Ram Expander as virtual memory in my metacompiler.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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: