Hello. I'm trying to do some programming on the Apple II+, and I know that the stack can get weird between GOTO and GOSUB.
What I'm trying to do is GOSUB - GOTO - RETURN. The GOTO is in an IF. It looks something like this:
<code>
10 X = 1
20 GOSUB 50
30 END
50 IF X = 1 THEN 100
60 RETURN
100 X = X + 1
110 RETURN
</code>
I'm doing this sequence over and over again to display numbers, and I'm wondering if doing something like this would blow up the memory at some point.
- D
Unstructured (or "spaghetti") BASIC code is rather error-prone, but there's nothing here that will use up the stack. Each time that you execute GOSUB, a return address is pushed, and it is popped each time RETURN executes. As long as each executed GOSUB is paired with a RETURN, the stack will always return to its former height.
I'd change 50 to read:
50 IF X <> 1 THEN X = X + 1
And delete lines 100 and 110
And yes, you have a limited number of levels of GOSUBs you can do before you get a STACK OVERFLOW.
That reverses the sense of the test.
Oh, you're right. I had refactored that code a couple times and I switched it and forgot to put it back.
So 50 should be:
50 IF X = 1 THEN X = X + 1
Thanks for the help. I know it's easy to mess up code with GOTO's all over the place, it's not the best way to code, but work with what you got. Right?
The actual routine is a little more complicated. I'll paste the code since you were talking about my example. Maybe it's not the best way to do things, but it's what I came up with. This routine will format and value passed to PV. It's used to place commas in any number from 1,000 to 100,000,000. If it's less than 1k, it prints the number, clearing out the tail end. I've thought about changing some values to integers, but I wanted to make sure I was on the right course.
19000 REM FORMAT PV
19010 TP$ = STR$(PV)
19020 LN = LEN(TP$)
19030 IF LN < 4 THEN 19100
19040 IF LN > 3 AND LN < 7 THEN 19200
19050 IF LN > 6 THEN 19300
19060 RETURN
19100 REM FORMAT NUMBERS
19110 IF LN = 1 THEN PRINT TP$ + " "
19120 IF LN = 2 THEN PRINT TP$ + " "
19130 IF LN = 3 THEN PRINT TP$ + " "
19140 RETURN
19200 REM FORMAT K
19210 TV = LN - 3
19220 TF$ = LEFT$(TP$,TV)
19230 TP$ = TF$ + "," + RIGHT$(TP$,3)
19240 LN = LEN(TP$)
19250 IF LN = 4 THEN PRINT TP$ + " "
19260 IF LN = 5 THEN PRINT TP$ + " "
19270 IF LN = 6 THEN PRINT TP$ + " "
19280 IF LN = 7 THEN PRINT TP$ + " "
19290 RETURN
19300 REM FORMAT MIL
19310 TV = LN - 6
19320 TF$ = LEFT$(TP$,TV)
19330 MS$ = MID$(TP$,TV+1,3)
19340 TP$ = TF$ + "," + MS$ + "," + RIGHT$(TP$,3)
19350 PRINT TP$ + " "
19360 RETURN