6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Apr 27, 2024 9:17 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Jan 18, 2024 10:30 pm 
Offline

Joined: Thu Apr 26, 2012 4:54 pm
Posts: 7
Looking at the fig-FORTH listing, I see two different methods used for taking 2 values off the stack and replacing them with a single value. For example, PLUS uses this method:
Code:
PLUS   CLC
          LDA 0,X
          ADC 2,X
          STA 2,X
          LDA 1,X
          ADC 3,X
          STA 3,X
          INX
          INX
          JMP NEXT

while other operations like AND, etc. use this:
Code:
XOR    LDA 0,X
          EOR 2,X
          PHA
          LDA 1,X
          EOR 3,X
          INX
          INX
          JMP PUT

As near as I can tell, they both have the same stack effect (n1 n2 -- n3), the only difference is one is a few bytes longer but a few cycles faster, while the other is a few bytes shorter but a few cycles slower. Is that correct?


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 18, 2024 11:42 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1927
Location: Sacramento, CA, USA
glockr wrote:
As near as I can tell, they both have the same stack effect (n1 n2 -- n3), the only difference is one is a few bytes longer but a few cycles faster, while the other is a few bytes shorter but a few cycles slower. Is that correct?

Yes, I think you summed it up. If you want to study a FORTH that makes sincere efforts to maximize efficiency with the NMOS instruction set, check out Charlie's PETTIL ... he utilizes a split data stack, a dedicated zp TOS, lots of fall-throughs and direct threading, with many many trade-offs carefully considered and meticulously balanced.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 19, 2024 12:29 am 
Offline

Joined: Sun Apr 26, 2020 3:08 am
Posts: 357
I think AND is used far less than PLUS, so probably doesn't hurt to save a couple bytes at the expense of speed.


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 19, 2024 4:10 pm 
Offline

Joined: Sat Dec 12, 2015 7:48 pm
Posts: 122
Location: Lake Tahoe
Some of the decisions used in figForth were a little surprising to save space. One that I rectified for myself a few years ago was the 6809 Forth for the TRS-80 CoCo called ColorForth (not to be confused with Chuck's later version called ColorForth). It was meant to fit in a cartridge ROM, so there were many places were NEXT was branched to, even though the 6809 was practically designed for Forth and could pull it off in two machine instructions. I went through the code (someone else had done the work to extract it and create a RAM based version of the cartridge) and in-lined NEXT in all the words that had branched to it. The funny thing is that I was using a modern cross assembler that must have done a better job with instruction encoding than the one figForth used, so the actual result was *smaller* than the original. I added a few handy words that had been missing and *still* had a smaller binary.


Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 04, 2024 7:59 am 
Offline

Joined: Wed Aug 21, 2019 6:10 pm
Posts: 217
IamRob wrote:
I think AND is used far less than PLUS, so probably doesn't hurt to save a couple bytes at the expense of speed.


That sounds right ... in Phillip Koopman's 1989 "Stack Computers" he does a frequency of word use for four different benchmarks, and the average for + is 4.18% while the average for AND is 1.61%, so in userland + may be roughly two and a half times more frequent ... and if + is used more heavily than AND in the fig-Forth outer interpreter, the (system+userland) frequency might be higher still.


Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 04, 2024 8:05 pm 
Offline

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

In my Forth, I used POP in both + (plus) and AND . It is an ITC Forth so reducing how often NEXT executes was a design consideration. If I can squeeze a few bytes out of enough words then I can make more of the words in the kernel primitives while keeping the kernel about the same size, or even smaller.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 10 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: