Post by atolle on Feb 22, 2006 0:17:17 GMT -5
I found out a few nifty things about the C64 BASIC interpreter in my quest improve Color 64. Some of you may already know these things, but I found them interesting...
1) Unlike other programming languages, jumping outside of a FOR/NEXT loop may not necessarily be bad. For example, you might expect the following program to crash from running out of stack space:
10 for i = 1 to 5
20 if i = 3 then 40
30 print i : next
40 goto 10
However, this program will just keep running merrily along for as long as you want. The reason? If the variable name of an existing FOR/NEXT loop is used, the interpreter will simply overwrite the loop's existing stack entry, rather than creating a new one. Of course, this only works within the same subroutine scope (explained in the next section).
2) Calling a subroutine with GOSUB creates a new "scope" for FOR/NEXT loops. Here's an example:
10 for i = 1 to 10
20 print "outer"i
30 x = i : gosub 50 : i = x
40 next i : end
50 for i = 1 to 5
60 print "inner"i
70 next i : return
In this case, the subroutine at line 50 will create a new stack entry for the inner loop instead of overwriting the existing entry for the outer loop. However, the value of i in the outer loop still needs to be preserved before calling the subroutine.
3) Using the RETURN statement will purge all FOR/NEXT loops in the current scope. Here's an example that generates an error message:
10 gosub 50 : next : end
50 for j = 1 to 10 : print j : return
Here's what happens: When the FOR statement in line 50 is executed, it creates a new entry on the stack for the loop. Next, when the RETURN statement is executed, the the stack pointer is returned to the way it was before the GOSUB. Thus, when the NEXT statement is reached, there is no FOR/NEXT entry on the stack, so it fails.
This allows you to create some optimized but structurally "sloppy" routines and dump them in a subroutine. When the subroutine exits, the stack will be cleaned up for you
4) The end value of a FOR/NEXT loop is calculated only once, at the beginning of the loop. For example:
10 x = 10 : for i = 1 to x : print i : x = x - 1 : next
The above loop will still execute 10 times, despite the fact that x is being modified within the loop. The same goes for the step value. You don't have to worry about slowing the loop down by using a complicated expression for the end value, since it is calculated only once.
1) Unlike other programming languages, jumping outside of a FOR/NEXT loop may not necessarily be bad. For example, you might expect the following program to crash from running out of stack space:
10 for i = 1 to 5
20 if i = 3 then 40
30 print i : next
40 goto 10
However, this program will just keep running merrily along for as long as you want. The reason? If the variable name of an existing FOR/NEXT loop is used, the interpreter will simply overwrite the loop's existing stack entry, rather than creating a new one. Of course, this only works within the same subroutine scope (explained in the next section).
2) Calling a subroutine with GOSUB creates a new "scope" for FOR/NEXT loops. Here's an example:
10 for i = 1 to 10
20 print "outer"i
30 x = i : gosub 50 : i = x
40 next i : end
50 for i = 1 to 5
60 print "inner"i
70 next i : return
In this case, the subroutine at line 50 will create a new stack entry for the inner loop instead of overwriting the existing entry for the outer loop. However, the value of i in the outer loop still needs to be preserved before calling the subroutine.
3) Using the RETURN statement will purge all FOR/NEXT loops in the current scope. Here's an example that generates an error message:
10 gosub 50 : next : end
50 for j = 1 to 10 : print j : return
Here's what happens: When the FOR statement in line 50 is executed, it creates a new entry on the stack for the loop. Next, when the RETURN statement is executed, the the stack pointer is returned to the way it was before the GOSUB. Thus, when the NEXT statement is reached, there is no FOR/NEXT entry on the stack, so it fails.
This allows you to create some optimized but structurally "sloppy" routines and dump them in a subroutine. When the subroutine exits, the stack will be cleaned up for you
4) The end value of a FOR/NEXT loop is calculated only once, at the beginning of the loop. For example:
10 x = 10 : for i = 1 to x : print i : x = x - 1 : next
The above loop will still execute 10 times, despite the fact that x is being modified within the loop. The same goes for the step value. You don't have to worry about slowing the loop down by using a complicated expression for the end value, since it is calculated only once.