reverse string or MIRROR$

GFA BASIC-related articles in here please

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

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

reverse string or MIRROR$

Post by charles »

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?
The radioactive half-life : )
Atari is a lifestyle,not a hobby.
HOLD ON ! ! ! Im printing unreadable characters ...!
simonsunnyboy
Moderator
Moderator
Posts: 5235
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: reverse string or MIRROR$

Post by simonsunnyboy »

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/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee
User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2503
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Post by lp »

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.
Done. ;)

For maximum speed I'd use BYTE{} as PEEK() works in supervisor mode or just use GBE.
Last edited by lp on Tue Jan 01, 2019 4:56 pm, edited 3 times in total.
rockyone
Atari Super Hero
Atari Super Hero
Posts: 511
Joined: Thu Jan 20, 2011 8:47 pm
Location: France
Contact:

Re: reverse string or MIRROR$

Post by rockyone »

it is a very practical instruction to cut the paths in omikron
User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2704
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Post by charles »

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?
The radioactive half-life : )
Atari is a lifestyle,not a hobby.
HOLD ON ! ! ! Im printing unreadable characters ...!
User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 225
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Post by GokMasE »

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: 2503
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Post by lp »

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: 4199
Joined: Tue Jan 18, 2011 12:31 am
Location: Cheltenham, UK
Contact:

Re: reverse string or MIRROR$

Post by Dal »

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$
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: 2503
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Post by lp »

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.
simonsunnyboy
Moderator
Moderator
Posts: 5235
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: reverse string or MIRROR$

Post by simonsunnyboy »

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/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee
User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2503
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Post by lp »

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.
simonsunnyboy
Moderator
Moderator
Posts: 5235
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: reverse string or MIRROR$

Post by simonsunnyboy »

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/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee
User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2704
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Post by charles »

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 !!!
The radioactive half-life : )
Atari is a lifestyle,not a hobby.
HOLD ON ! ! ! Im printing unreadable characters ...!
User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 225
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Post by GokMasE »

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: 2704
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Post by charles »

is there a practical real life use for any single computer code?
The radioactive half-life : )
Atari is a lifestyle,not a hobby.
HOLD ON ! ! ! Im printing unreadable characters ...!
User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 225
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Post by GokMasE »

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: 12916
Joined: Sat May 03, 2008 2:09 pm
Location: Far East
Contact:

Re: reverse string or MIRROR$

Post by wongck »

there's also the xor method.
Not sure if it's any faster.
My Stuff: FB/Falcon CT63 CTPCI ATI RTL8139 USB 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: 2503
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: reverse string or MIRROR$

Post by lp »

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: 2704
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Post by charles »

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
The radioactive half-life : )
Atari is a lifestyle,not a hobby.
HOLD ON ! ! ! Im printing unreadable characters ...!
User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2704
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Post by charles »

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

strng$=fn mirror$(*a$)
The radioactive half-life : )
Atari is a lifestyle,not a hobby.
HOLD ON ! ! ! Im printing unreadable characters ...!
User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 225
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Post by GokMasE »

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: 2704
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Post by charles »

why not then illustrate for us the correct method gomaske?
The radioactive half-life : )
Atari is a lifestyle,not a hobby.
HOLD ON ! ! ! Im printing unreadable characters ...!
User avatar
GokMasE
Captain Atari
Captain Atari
Posts: 225
Joined: Sun Mar 02, 2003 11:16 pm
Location: Sweden
Contact:

Re: reverse string or MIRROR$

Post by GokMasE »

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
User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2704
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: reverse string or MIRROR$

Post by charles »

here is another

Code: Select all

 
PRINT @reverse$("asdf")
'
FUNCTION reverse$(string$)
  LOCAL result$,i%
  result$=""
  FOR i%=1 TO LEN(string$)
    result$=MID$(string$,i%,1)+result$
  NEXT i%
  RETURN result$
ENDFUNC
 

The radioactive half-life : )
Atari is a lifestyle,not a hobby.
HOLD ON ! ! ! Im printing unreadable characters ...!
Post Reply

Return to “GFA BASIC”