extra stack operators

Topics relating to various Forth models on the 6502, 65816, and related microprocessors and microcontrollers.
chitselb
Posts: 232
Joined: 21 Aug 2010
Location: Ontonagon MI
Contact:

extra stack operators

Post by chitselb »

on the OLPC wiki, there's NIP ( a b -- b ) and TUCK ( a b -- b a b ) and -ROT ( a b c -- c a b ) which I call LROT, but what is the word which has the stack diagram ( a b c -- b a c ) ? Is it UNDER?
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: extra stack operators

Post by GARTHWILSON »

I haven't heard of that or of LROT, but NIP, TUCK, and -ROT are pretty standard.
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?
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: extra stack operators

Post by barrym95838 »

Ooh, may I try? (utter noob)

: UNDER >R SWAP R> ;

... or ...

: UNDER ROT SWAP ;

I don't know what to call it, but UNDER sounds as good as any, as long as that name isn't habitually used for something different.

Mike
chitselb
Posts: 232
Joined: 21 Aug 2010
Location: Ontonagon MI
Contact:

Re: extra stack operators

Post by chitselb »

Mike,

Both of your definitions have the equivalent and proper stack effect ( a b c -- b a c ). I would choose the latter because it's two bytes shorter. In Brodie's "Thinking Forth" one of the things he says, to paraphrase badly from a book I read 30 years ago, "Naming the word properly is half the work of writing the word." In this case, it would be greatly preferred to write code that another Forth programmer would understand so I'm seeking the recognizable name for what I'm now calling UNDER.

I've really taken myself to school this past couple days, on the short bus. Stupid mistake after stupid mistake, violating principles that were once well known to me. I wrote this pretty short word (Copy) and then a monolithic word (Delete) and then a huge monolithic word (Paste) where everything fell apart with the line wrap table. Yes, that thing you suggested I dispense with entirely on the other thread. I'm keeping it in mostly because of the "nativeness" of it, and because of the long string literals. Today I did a fair amount of (literal) gardening and redesigned everything relating to copy/delete/paste into tiny reusable chunks of code. It's all headerless anyway, I don't know why I resisted this path. I see light at the end of the tunnel.

Update: I just learned on comp.lang.forth that UNDER is taken, and it does something else. It's probably not RSWAP either, which I'm pretty sure swaps two words on the return stack ( R: a b -- R: b a ).
: UNDER ( a b -- a a b ) >R DUP R> ;
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: extra stack operators

Post by barrym95838 »

chitselb wrote:
... : UNDER ( a b -- a a b ) >R DUP R> ;
Ah, it figures.

Using your criteria for compactness, we could successfully change this to

: UNDER ( a b -- a a b ) OVER SWAP ;

right? I'm sure that actual execution speed is a toss-up, since OVER and SWAP are probably more complex primitives than >R DUP and R>, but it's definitely a cell shorter, all else being equal. ROTSWAP would be most efficient as a primitive on my 'm32, with no ITC size penalty ... assuming that it would be used often enough to be preferred over ROT SWAP in a real program, which I have yet to write.

Code: Select all

UNDER: ; ( a b -- a a b )
        .dw  .+1
        ldb  0,x
        stb  0,-x
        bra  _next
ROTSWAP: ; ( a b c -- b a c )
        .dw  .+1
        exa  0,x        ; a c b
        exa  1,x        ; b c a
        bra  SWAP+1     ; b a c
SWAP: ; ( a b -- b a )
        .dw  .+1
        exa  0,x
_next:
        ldu  0,y+
        jmp  (0,u+)
Mike
chitselb
Posts: 232
Joined: 21 Aug 2010
Location: Ontonagon MI
Contact:

Re: extra stack operators

Post by chitselb »

Seems to me that it would be useful to have a stack manipulator word for every permutation of stack diagram, and these should have universally accepted names.

( a --
dup ( a -- a a )
?dup ( a -- a a | 0 )
drop ( a -- )

( a b --
over ( a b -- a b a )
swap ( a b -- b a )
under ( a b -- a a b )
nip ( a b -- b )
tuck ( a b -- b a b )
2dup ( a b -- a b a b )

( a b c --
rot ( a b c -- b c a )
FLIP ( a b c -- b a c )
-rot ( a b c -- c a b )
FLOP ( a b c -- c b a )

I'm going to call them FLIP and FLOP for now, until I find out what the names are supposed to be.
chitselb
Posts: 232
Joined: 21 Aug 2010
Location: Ontonagon MI
Contact:

Re: extra stack operators

Post by chitselb »

barrym95838 wrote:
Ooh, may I try? (utter noob)

: UNDER >R SWAP R> ;

... or ...

: UNDER ROT SWAP ;

I don't know what to call it, but UNDER sounds as good as any, as long as that name isn't habitually used for something different.

Mike
I went with : UNDER ( a b -- a a b ) >R DUP R> ;
User avatar
programandala.net
Posts: 8
Joined: 13 Dec 2015
Location: Spain
Contact:

Re: extra stack operators

Post by programandala.net »

barrym95838 wrote:
: UNDER ( a b -- a a b ) OVER SWAP ;
It's called also nup. It seems there's no accepted name yet. Some Forthers dislike those too specific stack manipulations, except the standard nip and tuck; others find them useful.
Marcos Cruz (programandala.net)
User avatar
programandala.net
Posts: 8
Joined: 13 Dec 2015
Location: Spain
Contact:

Re: extra stack operators

Post by programandala.net »

chitselb wrote:
Seems to me that it would be useful to have a stack manipulator word for every permutation of stack diagram, and these should have universally accepted names.
I don't think it would be practical, even feasible. There are many different stack operations you can do, and most of them can be defined using others. The standard operations have names related to their functions, because the functions are simple.
chitselb wrote:
FLIP ( a b c -- b a c )
FLOP ( a b c -- c b a )
I'm going to call them FLIP and FLOP for now, until I find out what the names are supposed to be.
That's the problem I just mentioned. It would be hard to remember not only which one is `flop` and which one is `flip`, but also what they actually do!
Marcos Cruz (programandala.net)
nyef
Posts: 235
Joined: 28 Jul 2013

Re: extra stack operators

Post by nyef »

programandala.net wrote:
I don't think it would be practical, even feasible. There are many different stack operations you can do, and most of them can be defined using others. The standard operations have names related to their functions, because the functions are simple.
chitselb wrote:
FLIP ( a b c -- b a c )
FLOP ( a b c -- c b a )
I'm going to call them FLIP and FLOP for now, until I find out what the names are supposed to be.
That's the problem I just mentioned. It would be hard to remember not only which one is `flop` and which one is `flip`, but also what they actually do!
Even within the standard, I have a hard time remembering which way round ROT goes. It seems like every time I need to use it, I either look it up or I do "1 2 3 ROT .S DROP 2DROP" to figure it out.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: extra stack operators

Post by barrym95838 »

I would be tempted to name them bca, bac, cab, and cba, but I have a perverse affinity for three-letter mnemonics. :wink:

Mike B.
User avatar
Dr Jefyll
Posts: 3526
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: extra stack operators

Post by Dr Jefyll »

Mike, the TLM's you propose are valid hex numbers. Could get (a little too) interesting... :)
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
User avatar
programandala.net
Posts: 8
Joined: 13 Dec 2015
Location: Spain
Contact:

Re: extra stack operators

Post by programandala.net »

nyef wrote:
Even within the standard, I have a hard time remembering which way round ROT goes. It seems like every time I need to use it, I either look it up or I do "1 2 3 ROT .S DROP 2DROP" to figure it out.
I suggest this mnemonic: `rot` is the "normal", so it's "clockwise"; `-rot` is the opposite.
Marcos Cruz (programandala.net)
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: extra stack operators

Post by barrym95838 »

Dr Jefyll wrote:
Mike, the TLM's you propose are valid hex numbers. Could get (a little too) interesting... :)
Ugh ... foiled again! How about:

: wxy drop ;
: yzz dup ;
: yzx rot ;
: yzy over ;
: zwx 2over ;

... meh, could cause brain damage in large quantities. :shock: Plus the fact that those are valid base-36 numbers. :wink:

Mike B.
theGSman
Posts: 85
Joined: 26 Jan 2015

Re: extra stack operators

Post by theGSman »

programandala.net wrote:
I don't think it would be practical, even feasible. There are many different stack operations you can do, and most of them can be defined using others. The standard operations have names related to their functions, because the functions are simple.
I think that what chitselb means is that every stack manipulation should have a name, not that every conceivable stack manipulation (no matter how rarely used) should be implemented in a Forth system. It is a simple matter to write a CODE word for any manipulation that isn't in your Forth system but is likely to be used a lot in a program. I have often found 2PICK to be such a word.
programandala.net wrote:
That's the problem I just mentioned. It would be hard to remember not only which one is `flop` and which one is `flip`, but also what they actually do!
for "flop" I would probably use ROTSWAP if I needed the word or SWAPUNDER if the former name was not descriptive enough.
"flip" would probably sound better as 3REVERSE. (I hope I didn't get "flip" and "flop" mixed up. :mrgreen: )
Post Reply