Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: Hot_Dog on May 31, 2011, 02:08:56 pm

Title: Memory Leak with Axe sub( ?
Post by: Hot_Dog on May 31, 2011, 02:08:56 pm
Suppose I had the following code (basically an endless loop):

Lbl A
sub(B

Lbl B
Goto A

Is this bad?
Title: Re: Memory Leak with Axe sub( ?
Post by: JosJuice on May 31, 2011, 02:13:23 pm
Yes, it's bad. It'll cause a stack overflow, and then random things start happening because data is being executed.
Title: Re: Memory Leak with Axe sub( ?
Post by: Deep Toaster on May 31, 2011, 05:12:50 pm
Code: (Axe) [Select]
Lbl A
sub(A)

Same thing :D

sub( is basically just CALL. That's all it is.
Title: Re: Memory Leak with Axe sub( ?
Post by: Munchor on May 31, 2011, 06:09:06 pm
Code: (Axe) [Select]
Lbl A
sub(A)

Same thing :D

sub( is basically just CALL. That's all it is.

So that's not like Pause 0, it's really infinite or does it break or something?
Title: Re: Memory Leak with Axe sub( ?
Post by: Quigibo on May 31, 2011, 07:39:46 pm
Its okay if you have a base case and you've set it up to be a recursive call.  For instance, you could write a factorial function like this:

Code: [Select]
Lbl A
If r1=0
  Return 1
End
Return r1*sub(A,r1-1)

Anyway you just have to be careful about those.  The less goto's you have in your code, the less likely you will leak.  Most uses of Goto can be replaced by either loops or subroutines without sacrificing speed or size.
Title: Re: Memory Leak with Axe sub( ?
Post by: Deep Toaster on May 31, 2011, 08:02:04 pm
Code: (Axe) [Select]
Lbl A
sub(A)

Same thing :D

sub( is basically just CALL. That's all it is.

So that's not like Pause 0, it's really infinite or does it break or something?

Pause is really a bunch of jumps (DJNZ and JR, to be exact). sub( is different because it's CALL, which uses the stack. You know what happens when you overload the stack ;)
Title: Re: Memory Leak with Axe sub( ?
Post by: Hot_Dog on May 31, 2011, 08:18:45 pm
I also found Asm(3333 to work :P
Title: Re: Memory Leak with Axe sub( ?
Post by: jnesselr on May 31, 2011, 11:00:37 pm
I also found Asm(3333 to work :P
Yeah, randomly increasing the stack pointer does seem to have that affect.

And compiling the "Lbl A:Sub(A)" thing shows that it is in fact, just a straight call back to 9D95.
Title: Re: Memory Leak with Axe sub( ?
Post by: Hot_Dog on May 31, 2011, 11:30:20 pm
I also found Asm(3333 to work :P
Yeah, randomly increasing the stack pointer does seem to have that affect.

And compiling the "Lbl A:Sub(A)" thing shows that it is in fact, just a straight call back to 9D95.

Lol, I meant increasing the stack pointer fixes the problem
Title: Re: Memory Leak with Axe sub( ?
Post by: jnesselr on June 01, 2011, 03:20:45 pm
I also found Asm(3333 to work :P
Yeah, randomly increasing the stack pointer does seem to have that affect.

And compiling the "Lbl A:Sub(A)" thing shows that it is in fact, just a straight call back to 9D95.

Lol, I meant increasing the stack pointer fixes the problem
Oh, right, stack grows downward. This is why I can never really be a great z80 coder.  I can't remember stupid details like that!
Title: Re: Memory Leak with Axe sub( ?
Post by: BrownyTCat on June 02, 2011, 12:26:36 pm
I hear "Memory leak" a lot and generally know what it is on computers, but is it permanent memory loss on Z80 calculators?
Title: Re: Memory Leak with Axe sub( ?
Post by: Hot_Dog on June 02, 2011, 12:52:32 pm
I hear "Memory leak" a lot and generally know what it is on computers, but is it permanent memory loss on Z80 calculators?

When you are programming for calculators (actually for anything, but calculators in particular), you require memory for loops and subprograms.  The more unexited loops and subgrams you are running at once the more memory you need.  Eventually you reach a point where you run out of safe-to-use memory.  You don't lose the memory for good, but your calculator does crash or can't run anymore.
Title: Re: Memory Leak with Axe sub( ?
Post by: Deep Toaster on June 02, 2011, 01:03:21 pm
Depends on the context. In BASIC a "memory leak" (caused by Goto'ing out of a loop) causes a minor leak that is returned when the program exits. In Axe and ASM, a memory leak is a bit more serious, but it still doesn't usually crash or anything. It just means a small portion of your RAM is unavailable because your calculator thinks it's still being used, and it gets fixed when you RAM clear. But in this case, if you keep using sub( (or CALL) over and over, you cause a type of memory leak that could lead to a stack overflow, which finishes off in a nice little crash. None of it's permanent, though.