Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: indubitably on September 30, 2009, 11:54:15 pm

Title: Programming help
Post by: indubitably on September 30, 2009, 11:54:15 pm
I was bored in class so I decided to try and make a ddr type game.
I did so and i like it, but it slows down over time and I cannot figure out how to prevent this.
Is this a problem with my code or is it the calculator?
I didn't think it was the calc because there are much bigger games that keep a constant speed.
Also I tried the program on an emulator and it still slowed down. I cannot find any reason why this would happen in the code.

Thanks for any help in advance!

Link:
http://www.ticalc.org/archives/files/fileinfo/421/42167.html


Code: [Select]
:ClrHome
:0→S
:9→O
:9→P
:9→Q
:9→R
:Goto Z
:Lbl A
:0→X
:0→A
:While X=0
:(A+1)→A
:If A=10
:Goto B
:getKey→X
:End
:Goto B
:Lbl B
:If X=24
:Goto 1
:If X=25
:Goto 3
:If X=26
:Goto 4
:If X=34
:Goto 2
:If O=2
:(S-1)→S
:If P=2
:(S-1)→S
:If Q=2
:(S-1)→S
:If R=2
:(S-1)→S
:Goto X
:Lbl 1
:If O=2
:Then
:(S+1)→S
:9→O
:Goto X
:Else
:(S-1)→S
:Goto X
:End
:Lbl 2
:If P=2
:Then
:(S+1)→S
:9→P
:Goto X
:Else
:(S-1)→S
:Goto X
:End
:Lbl 3
:If Q=2
:Then
:(S+1)→S
:9→Q
:Goto X
:Else
:(S-1)→S
:Goto X
:End
:Lbl 4
:If R=2
:Then
:(S+1)→S
:9→R
:Goto X
:Else
:(S-1)→S
:Goto X
:End
:Lbl Z
:randInt(1,4)→M
:1→N
:Goto X
:Lbl X
:If N=0
:Goto Z
:(N-1)→N
:If M=1
:Then
:If O=9
:(O-1)→O
:End
:If M=2
:Then
:If P=9
:(P-1)→P
:End
:If M=3
:Then
:If Q=9
:(Q-1)→Q
:End
:If M=4
:Then
:If R=9
:(R-1)→R
:End
:0→M
:Goto Y
:Lbl Y
:ClrHome
:If O≤8
:Then
:Output(O,7,"<")
:(O-1)→O
:End
:If P≤8
:Then
:Output(P,8,"V")
:(P-1)→P
:End
:If Q≤8
:Then
:Output(Q,9,"^")
:(Q-1)→Q
:End
:If R≤8
:Then
:Output(R,10,">")
:(R-1)→R
:End
:Output(1,1,S)
:Output(3,6,"-"
:Output(3,11,"-"
:If O=0
:9→O
:If P=0
:9→P
:If Q=0
:9→Q
:If R=0
:9→R
:Goto A
Title: Re: Programming help
Post by: cooliojazz on October 01, 2009, 12:12:20 am
It would help anyone wnting to help if you posted the code too, maybe with comments. :P
Title: Re: Programming help
Post by: indubitably on October 01, 2009, 12:18:03 am
Is there a way I can copy it right from the program?
Title: Re: Programming help
Post by: Builderboy on October 01, 2009, 12:20:14 am
Hi Indubitably (coolest word ever ^^) and welcome to the site!

The problem you are describing is called a memory leak.  when you enter a while loop, the calculator needs to remember that it has entered the while loop, so it will know what to do when it gets to the End.  So what it does is set aside a small pice of memory coresponding to that while loop.  When the program exits the while loop, the calculator is able to get rid of that memory, and everything works great.

The problem arises when you have code like this

Code: [Select]
While x=0
If A
Goto B
End

Lbl B

When you Goto out of the loop, the calculator is never able to reach the End it needs to in order to get rid of that memory.  As a result, you have this chunk of memory that is never going to be free.  If you continue doing this over and over again, eventually the program is going to get very slow, and eventually crash when the calculator runs out of memory.

I see that you use a lot of Goto's in your program, and while that is not a bad thing, it is very important that you don't Goto out of a Loop, as this causes memory errors, slowdowns, and crashes :(


By the way, if you want to convert your program to text, you can use this web program called sourcoder :)
http://www.cemetech.net/projects/basicelite/sourcecoder2.php
It will take in and display your program, and even try to optimize it in some cases.
Title: Re: Programming help
Post by: indubitably on October 01, 2009, 12:29:05 am
Thanks for the reply, the reason I did this is because I really do not know another
way to only allow a certain amount of time for a getkey command.

I am thinking that I should change the code to
Code: [Select]
While x=0
If A
9>x
end

This would end the while loop. I would just have to make sure that doesn't mess with
scoring or even button recognition.
Title: Re: Programming help
Post by: jsj795 on October 01, 2009, 12:43:07 am
yeah playing around with goto/lbl is not a good idea because it is highly likely to give you stack mems...
Title: Re: Programming help
Post by: TsukasaZX on October 01, 2009, 01:02:20 am
Why not do:
Code: [Select]
:While X=0 and A<10
:A+1→A
:getKey→X
:End
:Goto B

Also, there's some optimizations you could make if you want to make your game smaller and faster.
Title: Re: Programming help
Post by: indubitably on October 01, 2009, 01:11:55 am
I was not aware that there could be 2 conditions for a while loop.
I tried that and it worked really nice.
Title: Re: Programming help
Post by: TsukasaZX on October 01, 2009, 01:13:48 am
Yup, you can have pretty much as many conditions as you want in an If or While. :)
Glad I helped some ;D
Title: Re: Programming help
Post by: megasasquatch on October 01, 2009, 02:28:30 am
Ahh... Good ole programming during class. :)
Title: Re: Programming help
Post by: DJ Omnimaga on October 01, 2009, 02:28:03 pm
Hi and welcome here :)

Another thing about Goto/Lbls:

The farther the Lbl in your program, the longer it will take to load until it reaches it. Because of this, it is recommended to immediately start your program with Goto 0 and have Lbl 0 with the intro screen, initializing and menu/options (if any) located at the end of your game program and have the game main loop (where arrows rises) at the top. However, if you use While/Repeat for the main game loop, this might not do such a difference. However, in case you use a lot of Goto, it might be best. Your program would look like this:

PRGM:PRGMNAME
Goto 0
Lbl 1
<game code>
Lbl 0
<initializing/menu code>
Goto 1

The issue is that when you use Goto, the calculator will search from the beginning of the program code and go down until it reaches the first Lbl of the same name. Because of this, if someone uses a lot of Lbl and Gotos, and his game main code is very very far in the program (especially for very large games), it will run considerably slower
Title: Re: Programming help
Post by: indubitably on October 01, 2009, 08:13:39 pm
wow that explains a lot considering my most commonly referenced
label is label x, which is located all the way at the bottom.
There are also some unnecessarily placed gotos and lbls that can
be removed as well.
Title: Re: Programming help
Post by: Ti-newb on November 15, 2009, 04:42:36 pm
stay away from Lbls and Goto's =DDD.

Another reason to stay away from Lbls and Goto's.. When you try to edit ur prgm, you will be confused, you'll have to 'track/folow' your prgm to see what it does again. and when u finally think u remember whatu were thinking, you'll forget. That and its hard for people to help you... im not sure i wanna read through that prgm =P no offence lol.

*EDIT:

Just wondering.. are u gonna make a new DDR game? or have it Edited? i wanna try it out.
Title: Re: Programming help
Post by: Galandros on November 15, 2009, 04:49:18 pm
Goto and Lbl in TI-BASIC are slow (DJ shows why). Also you can do everything in program flow without them.

That said there is no need to use them. The exception is the Menu( command... Lbl's with Menu( isn't a that bad idea depending on what you do.
Title: Re: Programming help
Post by: Ti-newb on November 16, 2009, 09:13:56 pm
Hah, exactly why i made a Menu Subprgm, =D it doesnt use Label =DDD
Title: Re: Programming help
Post by: titane666 on May 05, 2010, 06:00:17 am
I believed ti-basic was bound to memory-errors.
Is exiting a loop with a goto the only way to block memory ?
Even without blocks like that some scripts ends with a memory error :(
Anyway thanks for this information.
Title: Re: Programming help
Post by: DJ Omnimaga on May 05, 2010, 01:07:21 pm
The Goto instruction must never be put inside a While, Repeat, For() or If:Then/Else block. Otherwise, it never reaches the End instruction and the RAM that block took will not be freed up, thus, causing a memory leak and eventual ERR:MEMORY messages.
Title: Re: Programming help
Post by: calcdude84se on May 05, 2010, 06:46:21 pm
You can use it w/some unintended features of the parser (like how I implement subroutines), but yeah, you shouldn't ever unless you really know what you're doing. If you're not careful, it will be in a piece of infrequently executed code, and, just about as the player is about to get a high score after playing a few times for a few hours (must be a good game :)), BAM! ERR:MEMORY. And that would just suck. :)
Title: Re: Programming help
Post by: meishe91 on May 05, 2010, 07:15:34 pm
Just curious, but what if you had a loop (While, Repeat, or For() and you put the Goto AND Lbl in the same loop, would that change anything? Just a random thought I just had.
Title: Re: Programming help
Post by: Builderboy on May 05, 2010, 07:21:04 pm
It would work, as long as the goto and the Lbl are in the same level of the code then everything will work great :)  The problem arises when you are goto-ing a place that is at a different level than where you start.  (and by level, i mean how many loops/if's you are inside)
Title: Re: Programming help
Post by: apcalc on May 05, 2010, 07:21:33 pm
Hi.  I have a program that uses Goto [whatever] in an If...EndIf block.    The code is below.  It is a [incomplete] Gas Laws program for the TI-Nspire.  Because this is a program and will end, and not keep playing like a game, will I use RAM memory.  Also, if this does use memory, could I fix it by making it go back to the original If...EndIf block with a lbl at the end of it and have it display the answer from there?  Thanks!

Code: [Select]
Define LibPub gaslaws()=
Prgm
:Local equ
:Text "Choose Equation-
(1) Boyle's Law
(2) Charles' Law
(3) Gay-Lussac's Law
(4) Avogadro's Law
(5) Ideal Gas Law
(6) Combined Gas Law
(7) Van der Waals' Equation
(8) Graham's Law"
:Request "Choose Equation:",equ
:If equ=1 Then
:Goto a
:ElseIf equ=2 Then
:Goto b
:ElseIf equ=3 Then
:Goto c
:ElseIf equ=4 Then
:Goto d
:ElseIf equ=5 Then
:Goto e
:ElseIf equ=6 Then
:Goto f
:ElseIf equ=7 Then
:Goto g
:ElseIf equ=8 Then
:Goto h
:EndIf
:Lbl a
:©Boyle's Law
:RequestStr "V1",v1
:RequestStr "V2",v2
:RequestStr "P1",p1
:RequestStr "P2",p2
:Disp "Answer: "&nSolve(expr(v1)*expr(p1)=expr(v2)*expr(p2),x)
:Lbl b
:©Charles' Law
:RequestStr "V1",v1
:RequestStr "V2",v2
:RequestStr "T1",t1
:RequestStr "T2",t2
:Disp "Answer: "&nSolve(((expr(v1))/(expr(t1)))=((expr(v2))/(expr(t2))),x)
:Lbl c
:©Gay-Lussac's Law
:Lbl d
:©Avogarro's Law
:RequestStr "V1",v1
:RequestStr "V2",v2
:RequestStr "n1",n1
:RequestStr "n2",n2
:Disp "Answer: "&nSolve(((expr(v1))/(expr(n1)))=((expr(v2))/(expr(n2))),x)
:Lbl e
:©Ideal Gas Law
:RequestStr "V1",v1
:RequestStr "V2",v2
:RequestStr "P1",p1
:RequestStr "P2",p2
:RequestStr "T1",t1
:RequestStr "T2",t2
:Disp "Answer: "&nSolve(((expr(v1)*expr(p1))/(expr(t1)))=((expr(v2)*expr(p2))/(expr(t2))),x)
:Lbl f
:©Combined Gas Law
:RequestStr "V1",v1
:RequestStr "V2",v2
:RequestStr "P1",p1
:RequestStr "P2",p2
:RequestStr "T1",t1
:RequestStr "T2",t2
:RequestStr "n1",n1
:RequestStr "n2",n2
:Disp "Answer: "&nSolve(((expr(v1)*expr(p1))/(expr(t1)*expr(n1)))=((expr(v2)*expr(p2))/(expr(t2)*expr(n2))),x)
:Lbl g
:©Van der Waals Equation
:Lbl h
:EndPrgm

Title: Re: Programming help
Post by: bwang on May 05, 2010, 07:51:47 pm
Don't use goto in that program. Make the ElseIf blocks ElseIf...Then blocks and put your code between the Then and the next ElseIf.
Title: Re: Programming help
Post by: apcalc on May 05, 2010, 07:59:38 pm
That should make the program better.  Thanks for your help!  I have to finish this program fast before my Chemistry final.  I still have a long list of programs to go.
Title: Re: Programming help
Post by: dislexicengineer on December 22, 2013, 07:37:18 pm
That should make the program better.  Thanks for your help!  I have to finish this program fast before my Chemistry final.  I still have a long list of programs to go.

did you end up finishing that program?
Title: Re: Programming help
Post by: DJ Omnimaga on December 22, 2013, 07:42:37 pm
Sadly I think he is no longer into calculator development. His last release was back in the Ndless 2.0 days, a few years ago, and he didn't post here for months. D:
Title: Re: Programming help
Post by: dislexicengineer on December 22, 2013, 09:22:36 pm
damn, well thanks anyway! if you know if any complete chemistry apps. which include pH for the ti nspire. let me know! happy holidays!