Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: MGOS on December 09, 2011, 07:27:53 am

Title: Exiting For-loop in a search-subroutine
Post by: MGOS on December 09, 2011, 07:27:53 am
I have some question for a routine I was about to write:
The function should search the array L1 with the dimension G for the value r1. It should return the index of the first found value, or 0 if not found.

I had these two ideas how to do this, but Is the second legal axe code?
If so, which one is faster?
Do I have to put a space between Return and I?

Code: [Select]
Lbl S
If G
For (I,1,G)
If {L1+I-1} = r1
Goto F
End
End
End
0->I
Lbl F
Return I

Code: [Select]
Lbl S
If G
For (I,1,G)
If {L1+I-1} = r1
Return I
End
End
End
Return 0
Title: Re: Exiting For-loop in a search-subroutine
Post by: aeTIos on December 09, 2011, 08:39:07 am
So what you want to do is stopping the for() loop?
In that case, do this:
Code: [Select]
for(A,0,G)
blah blah
if condition to stop loop = 1
G->A
end
end
Title: Re: Exiting For-loop in a search-subroutine
Post by: MGOS on December 09, 2011, 08:51:55 am
Not only. I want to stop the for loop if the condition is true. But the function should return the value the index variable of the loop had when it was stopped.
So lets say the Array has the dimension of 5 and is {1,2,0,9,7} and r1 is 9. Then my function goes through the loop, stops at I = 4 and returns 4.
If I looked for let's say 5, the function would return 0, becouse none of the elements in the array had the value 5.

Both of the functions I posted SHOULD do this, but I don't know which one is better / legal axe code.

I know that array indizes usally start with 0, but starting with 1 has some advantages in my case, so I use that.
Title: Re: Exiting For-loop in a search-subroutine
Post by: Builderboy on December 09, 2011, 01:50:31 pm
You could use InData() :D It does exactly what you describe, and is a built in Axe command ^^
Title: Re: Exiting For-loop in a search-subroutine
Post by: jacobly on December 09, 2011, 02:07:28 pm
Well, InData() works with zero terminated data, and his routine works with G bytes of data and embedded zeros.

First of all, {L1+I-1} can be optimized to {I-1+L1}. Other than that, the second one should work fine. Also, the space should be optional.

Edit:
Spoiler For Crazy Optimizations:
Lbl S
Return!If G .returns G if G=0
For(I,1,G)
!If {I-1+L1}-r1
Return I
End
End
.as long as I is not modified inside the for loop, and G >= 1 (true), ans = -1
Return+1
Title: Re: Exiting For-loop in a search-subroutine
Post by: AngelFish on December 09, 2011, 02:26:08 pm
Isn't this exactly what the CPIR instruction in z80 does?
Title: Re: Exiting For-loop in a search-subroutine
Post by: jacobly on December 09, 2011, 02:42:29 pm
Almost. CPIR returns an address, whereas this routine returns an index. Also, this routine is much easier to extend to arrays of words or objects (but I'm not saying that access to CPIR through an axe command wouldn't be cool ;)).
Title: Re: Exiting For-loop in a search-subroutine
Post by: AngelFish on December 09, 2011, 02:47:15 pm
An address is pretty easily converted into an index by simply subtracting the base address of the array. As for the flexibility of the routine, yes. However, a single CPU instruction with a single subtraction afterwards is going to be far faster and smaller than anything Axe compiles to.
Title: Re: Exiting For-loop in a search-subroutine
Post by: squidgetx on December 09, 2011, 03:33:53 pm
Side note that there is almost no such thing as illegal Axe code. If it works, then it's legal. Some of Axe's side effects created through 'hackish' code can be used to your advantage.

Goto-ing out of a For() loop is totally acceptable, but I like the method you used with Returns better
Title: Re: Exiting For-loop in a search-subroutine
Post by: Quigibo on December 09, 2011, 03:54:06 pm
The only problem with using hackish effects is that anything not specified explicitly by the commands list or documentation (like the inside/outside "ans" value in a for loop) might change from version to version, especially as more optimizations are made.  So use at your own risk, you just have a small possibility of losing the portability. :)
Title: Re: Exiting For-loop in a search-subroutine
Post by: MGOS on December 09, 2011, 04:15:37 pm
Huge thanks to all of you, guys. I think I'm gonna use the version with the returns. That with searching the array for values is just a simplyfied version of what I want to do, actually the condition is a lot more than just that.