reverse string or MIRROR$

GFA BASIC-related articles in here please

Moderators: exxos, simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2027
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

reverse string or MIRROR$

Postby charles » Mon Nov 13, 2017 11:28 pm

ive adapted a omicron command into my gfa language,,,MIRROR$
many ways to reverse a string ,
whats the preferred optimum method ?

at the moment I use
pointer to string ,
find length ,
allot a new string same length
use for/ next length downto 0
peeking and poking into new string

any experiences with such?
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4849
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: reverse string or MIRROR$

Postby simonsunnyboy » Tue Nov 14, 2017 5:24 pm

Certainly not PEEK or POKE for a loop iterating backwards, taking subcharacters with MID$ and then outputting them to a result.

But it might a hint for lp to add to his GBE.
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2252
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Postby lp » Tue Nov 14, 2017 6:12 pm

simonsunnyboy wrote:Certainly not PEEK or POKE for a loop iterating backwards, taking subcharacters with MID$ and then outputting them to a result.

But it might a hint for lp to add to his GBE.


I can add it to the to-do list. ;)

For maximum speed I'd use BYTE{} as PEEK() works in supervisor mode.
Last edited by lp on Tue Nov 14, 2017 6:55 pm, edited 1 time in total.

rockyone
Captain Atari
Captain Atari
Posts: 330
Joined: Thu Jan 20, 2011 8:47 pm
Location: France
Contact:

Re: reverse string or MIRROR$

Postby rockyone » Tue Nov 14, 2017 6:48 pm

it is a very practical instruction to cut the paths in omikron

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2027
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Postby charles » Tue Nov 14, 2017 10:56 pm

heres the one I wrote

Code: Select all

FUNCTION mirror$(as%)
  LOCAL a$,a%,l.a&,x&
  l.a&=WORD{ADD(as%,4)}
  a$=SPACE$(l.a&)
  a%=*a$
  DEC l.a&
  FOR x&=l.a& DOWNTO 0
    BYTE{ADD({a%},SUB(l.a&,x&))}=BYTE{ADD({as%},x&)}
  NEXT x&
  RETURN a$
ENDFUNC



and as a secondary I googled ...

Code: Select all

FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I



any assembler fans?
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!

User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 181
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Postby GokMasE » Wed Nov 15, 2017 1:38 pm

charles wrote:heres the one I wrote

Code: Select all

FUNCTION mirror$(as%)
  LOCAL a$,a%,l.a&,x&
  l.a&=WORD{ADD(as%,4)}
  a$=SPACE$(l.a&)
  a%=*a$
  DEC l.a&
  FOR x&=l.a& DOWNTO 0
    BYTE{ADD({a%},SUB(l.a&,x&))}=BYTE{ADD({as%},x&)}
  NEXT x&
  RETURN a$
ENDFUNC



I suspect that refering to a string by using its address/descriptor (as%) can be unreliable in this example,
since you then rely on the fact that the actual string is kept in the same location in ram throughout the loop.
This can be a false assumtion - for example, when you declare a new string, A$=SPACE$(l.a&) in this case, GFA might well decide to reorder/shuffle the strings in ram.

Even if this might only cause problems on rare occasions, the above FUNC will probably not be a 100% stable solution.

It would probably be a safer way to pass the string itself to the FUNC, and then fill in the as% variable with string address only after you have already declared A$.


Regards,

/Joakim

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2252
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Postby lp » Wed Nov 15, 2017 2:33 pm

An odd size string wouldn't need its center character moved. You can cut the loop iterations in half and swap the characters in the original string and eliminate a$ entirely.

Dal
Administrator
Administrator
Posts: 4070
Joined: Tue Jan 18, 2011 12:31 am
Location: Cheltenham, UK
Contact:

Re: reverse string or MIRROR$

Postby Dal » Wed Nov 15, 2017 3:15 pm

I wondered the same thing but then stopped when it became apparent you would still need a placeholder to marshall the character being replaced. So, not sure anything is gained... but what do I know...

I would do:

Code: Select all

FOR I = LEN(Input$) TO 1 STEP -1
Output$ = Output$ + MID$(Input$, I, 1)
NEXT I

RETURN Output$
TT030: 4MB/16MB + Crazy Dots, Mega"SST" 12, MegaSTE, STE: Desktopper case, IDE interface, UltraSatan (8GB + 512Mb) + HXC floppy emulator. Plus some STE's/STFM's

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2252
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Postby lp » Wed Nov 15, 2017 3:41 pm

String concatenation and MID$() generate library calls (BSRs) which execute much more code. BYTE{} swapping even with a temporary placeholder generates simple move.b instructions. I don't think this routine requires asm or such optimizations, but I always have fun with such things no matter how small. ;)
Last edited by lp on Wed Nov 15, 2017 6:07 pm, edited 1 time in total.

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4849
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: reverse string or MIRROR$

Postby simonsunnyboy » Wed Nov 15, 2017 4:53 pm

lp wrote:String concatenation and MID$() generate library calls (BSRs) which execute much more code. BYTE{} swapping even with a temporary placeholder generates simple move.b instructions. I don't know that this sort routine requires asm or such optimizations, but I always have fun with such things no matter how small. ;)


I guess your optimization is only faster when compiled. The classic BASIC approach with BASIC string functions is more readable in this case.

For realtime things, strings should generally be avoided.
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2252
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Postby lp » Wed Nov 15, 2017 5:27 pm

I stand by comments about BYTE{}, just as reliable if you know how GFA manages strings internally. If you want your code to look clean and correct the string functions are sufficient. I never code for the interpreter only the compiled end result.

Charles asked about asm, so my assumption was he was seeking a faster solution. However I don't know in what context he is using the routine, or if he's just goofing around mimicking omikron functions for the heck of it.

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4849
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: reverse string or MIRROR$

Postby simonsunnyboy » Wed Nov 15, 2017 5:29 pm

I suspect goofing around as he does for 10 years. Sorry, never saw clean solutions from his side. Only overcomplicated ones like the one posted.
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2027
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Postby charles » Wed Nov 15, 2017 11:37 pm

yes wise ones , goofing around ,
but eventually things should fall into place .

we are all different , our usage of a language is diferent

I find enjoyment in learnning how each computer language forces a different program flow pattern due to constraints in commands .

I like figuring out what it does before assigning a usage for such
be nice random number generator or jumble type command

all of a sudden 341 becomes 143
of stun becomes nuts

but I know within omicron its typically used to separate the drive label from the path from the filename by searching for "\" once reversed,,, I don't believe omicron has a rinstr !!!
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!

User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 181
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Postby GokMasE » Thu Nov 16, 2017 9:15 am

charles wrote:
but I know within omicron its typically used to separate the drive label from the path from the filename by searching for "\" once reversed,,, I don't believe omicron has a rinstr !!!


Is there any other practical situation where MIRROR$ might be useful?

In case the only reason for using MIRROR$ is that omikron is lacking RINSTR, my first thought it would make sense to write a wrapper for RINSTR for omikron (if possible).
Flipping the string just for the possibility to be able to search from left to right just sounds.. ..well.. ..backwards :wink:

Anyway, I don't think Lonny should put any effort into adding the call to GFA in case there is no real Life practical use for it.
It would just seem an unnecessary waste of resources and dev time.


Regards,

/Joakim

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2027
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Postby charles » Thu Nov 16, 2017 11:14 am

is there a practical real life use for any single computer code?
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!

User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 181
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Postby GokMasE » Thu Nov 16, 2017 11:36 am

charles wrote:is there a practical real life use for any single computer code?


Yes. In case you disagree with that, I'd say you are doing something wrong :-D


All jokes aside, since it seems you did not get my point:

If there are no cases where mirror$ has a practical (other than just for the pure amusement of the coder...) use from GFA side of things, then it makes no sense for Lonny to put time and efforts into adding it. As for writing GFA code to deal with mirroring a string (mimicing the omikron command), I don't mind that anyone does that purely for training purposes. Or simply for fun. I however doubt that such code would truly *need* heavy optimising since it is unlikely to be used in a CPU critical part of a program (my own personal assumption). But I guess that point is invalid if this is done as a coding exercise.

When it comes to omikrons lack of certain commands I guess further discussions are better left to the forum for "Other BASIC".


Regards,

/Joakim

User avatar
wongck
Ultimate Atarian
Ultimate Atarian
Posts: 11976
Joined: Sat May 03, 2008 2:09 pm
Location: Far East
Contact:

Re: reverse string or MIRROR$

Postby wongck » Thu Nov 16, 2017 2:43 pm

there's also the xor method.
Not sure if it's any faster.
My Stuff: FB/Falcon CT63+CTPCI_ATI_RTL8139 14+512MB 30GB HDD CF HxC_SD/ TT030 68882 4+32MB 520MB Nova/ 520STFM 4MB Tos206 SCSI
Shared SCSI Bus:ScsiLink ethernet, 9GB HDD,SD-reader @ http://phsw.atari.org
My Atari stuff for sale - click here for list

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2252
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Postby lp » Thu Nov 16, 2017 3:25 pm

XOR won't work in the case. You may of misinterpreted the function. ;)

Don't know why charles feels the need to derail the topic. As for mirror$() it's very low on my priority list. It's true omikron lacks rinstr() I see why it might be useful there. Maybe he's attempting to port omikron sources to gfa unmodified.

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2027
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Postby charles » Thu Nov 16, 2017 11:09 pm

universal basic decoder ....
(nice picture lonny ..its from page 6 archives?)

any basic code
run under Atari gfa ,,

huge!! need I say more?

,,so dynamic
,, so bold
new ...

ok maybe not in my lifetime ,
but another practical application for mirror$ , might be to store a encrypted password
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2027
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Postby charles » Thu Nov 16, 2017 11:17 pm

gomask I use a call in gfa like
and it wont loose its address like you assume

strng$=fn mirror$(*a$)
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!

User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 181
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Postby GokMasE » Fri Nov 17, 2017 6:59 am

charles wrote:gomask I use a call in gfa like
and it wont loose its address like you assume

strng$=fn mirror$(*a$)


You are missing the point again. I am not saying it necessarily WILL lose its address every time, but I am saying that on rare occasions it can indeed do that with your approach.

You do as you wish, but I would say writing code that has potential to blow up when there are very simple ways to avoid all the danger just sounds like a game of pointless russian roulette. If you strive to produce stable code/binary (I don't know if that is a priority of yours) you should never include stuff that involves even a theoretical risc of malfunction.

Why write riscy code when you don't gain anything at all? And I might as well ask, why ask other coders for advice and then just ignore the answers?

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2027
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Postby charles » Fri Nov 17, 2017 11:23 am

why not then illustrate for us the correct method gomaske?
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!

User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 181
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Postby GokMasE » Fri Nov 17, 2017 2:56 pm

I must revise previous suggestion, since it seems you are in fact passing the descriptor address rather than the address of the string contents.
The descriptor will not be altered, hence you should be safe to do what you do here - the address of the string can absolutely change after A$=SPACE$(l.a&), but the descriptor can not.

However, I would generally advice against using the short form of LONG{variable%}, where you just do {variable%}.
Using the short form makes the code very hard for others to read and this was the reason for me drawing the wrong conclusion here.

But indeed, the example should work as-is.

Regards,

Joakim


Social Media

     

Return to “GFA BASIC”

Who is online

Users browsing this forum: No registered users and 1 guest