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

All times are UTC




Post new topic Reply to topic  [ 38 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: extra stack operators
PostPosted: Sun May 04, 2014 3:28 pm 
Offline

Joined: Sat Aug 21, 2010 7:52 am
Posts: 231
Location: Arlington VA
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?


Top
 Profile  
Reply with quote  
PostPosted: Sun May 04, 2014 7:10 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
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?


Top
 Profile  
Reply with quote  
PostPosted: Sun May 04, 2014 9:19 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1927
Location: Sacramento, CA, USA
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


Top
 Profile  
Reply with quote  
PostPosted: Mon May 05, 2014 4:02 am 
Offline

Joined: Sat Aug 21, 2010 7:52 am
Posts: 231
Location: Arlington VA
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> ;


Top
 Profile  
Reply with quote  
PostPosted: Mon May 05, 2014 5:49 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1927
Location: Sacramento, CA, USA
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:
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


Top
 Profile  
Reply with quote  
PostPosted: Mon May 05, 2014 5:15 pm 
Offline

Joined: Sat Aug 21, 2010 7:52 am
Posts: 231
Location: Arlington VA
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 11, 2014 4:27 am 
Offline

Joined: Sat Aug 21, 2010 7:52 am
Posts: 231
Location: Arlington VA
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> ;


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 16, 2015 8:12 pm 
Offline
User avatar

Joined: Sun Dec 13, 2015 3:25 pm
Posts: 8
Location: Spain
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)


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 16, 2015 8:26 pm 
Offline
User avatar

Joined: Sun Dec 13, 2015 3:25 pm
Posts: 8
Location: Spain
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)


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 16, 2015 10:59 pm 
Offline

Joined: Sun Jul 28, 2013 12:59 am
Posts: 235
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 16, 2015 11:44 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1927
Location: Sacramento, CA, USA
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 16, 2015 11:54 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3346
Location: Ontario, Canada
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 16, 2015 11:59 pm 
Offline
User avatar

Joined: Sun Dec 13, 2015 3:25 pm
Posts: 8
Location: Spain
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)


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 17, 2015 12:30 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1927
Location: Sacramento, CA, USA
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 17, 2015 7:08 am 
Offline

Joined: Mon Jan 26, 2015 6:19 am
Posts: 85
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: )


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

All times are UTC


Who is online

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