Named GOSUB's
M.R. Smith
In a previous article, (COMPUTE! #12), I showed how to use the Ampersand instruction in Applesoft to permit instructions of the form:
10 FIRST = 1000 20 DEUX = 2000 30 THIRD = 3000 40 & GOSUB FIRST 50 & GOTO THIRD
In Applesoft BASIC, named GOSUB's and GOTO's are not normally allowed.
The machine language program given in that article did not allow the use of names in ON... GOSUB or ON...GOTO statements. The following machine language program rectifies that problem.
The following statements are now permitted:
60 FOURTH = 4000 70 NUM = INT(1 + RND(2)) 80 & ON NUM GOSUB FIRST, DEUX 90 & ON NUM GOTO THIRD, FOURTH
Using statements of this form makes it much easier to follow programs containing a large number of subroutines. In addition, it is much easier to remember the name of a subroutine rather than its number.
I am presently working on an extension to these statements to allow the passing of variables to a subroutine. That means statements of the form:
& GOSUB CALCULATE(A, B, C)
allowing the power of FORTRAN subroutine calls.
Description Of The BASIC Program
Line 20. Load the machine language program.
Line 20–100. Demonstrates the & GOSUB and & GOTO statements.
Line 110–180. Demonstrates the & ON... GOSUB and & ON...GOTO statements.
Lines 1000–3500. Demonstration subroutines and statements.
Lines 5000–6160. This subroutine loads and checks the machine language program. Every 17th number is the simple sum of the previous 16 numbers. This allows the entry of the numbers to be checked. The machine language program can be saved using the instruction:
BSAVE NAMED.GO,A$300,L$88
and called into your programs using the instruction:
10 PRINT CHR$(4);"BRUN NAMED.GO"
before any call to the ampersand (&) statements are made.
Program 1.
300. 387 0300- A9 4C 8D F5 03 A9 10 8D 0308- F6 03 A9 03 8D F7 03 60 0310- C9 B0 F0 0D C9 AB F0 29 0318- C9 B4 F0 31 A2 10 4C 12 0320- D4 20 B1 00 A9 03 20 D6 0328- D3 A5 B9 48 A5 B8 48 A5 0330- 76 48 A5 75 48 A9 B0 48 0338- 20 B7 00 20 44 03 4C D2 0340- D7 20 B1 00 20 7B DD 20 0348- 52 E7 4C 41 D9 20 B1 00 0350- 20 F8 E6 48 C9 B0 F0 0D 0358- C9 AB F0 09 C9 AF D0 BC 0360- 68 20 B1 00 48 C6 A1 D0 0368- 04 68 4C 10 03 A5 A1 48 0370- 20 B1 00 20 7B DD 20 52 0378- E7 20 B7 00 C9 2C DO 05 0380- 68 85 A1 DO E0 68 68 60 *
Program 2.
1 REM M. R. SMITH 2 REM MAY 1981 3 REM 10 REM LOAD THE ROUTINE - NORMA GOSUB 20 GOSUB 5000 30 REM ESTABLISH NAMES OF THE SUBROUTINES 40 FIRST = 1000 : DEUX = 1500 : THIRD = 2000 50 FOURTH = 2500 : FVTH = 3000 : SIXTH = 3500 60 REM DEMONSTRATE NAMED GOSUB'S 70 & GOSUB FIRST 80 & GOSUB DEUX 90 REM DEMONSTRATE NAMED GOTO 100 & GOTO FOURTH 110 REM DEMONSTRATE NAMED ON...GOSUB 120 NUM = INT (1 + 3 * RND (1)) 130 & ON NUM GOSUB FIRST, DEUX, THIRD 140 REM DEMONSTRATE NAMED ON...GOTO 150 FOR J = 1 TO 1000 : NEXT : REM DELAY 160 NUM = INT (1 + 3 * RND (1)) 170 & ON NUM GOTO FOURTH, FVTH, SIXTH 180 STOP 190 REM 970 REM DUMMY SUBROUTINES AND LINES 980 REM 990 REM FIRST SUBROUTINE 1000 PRINT : PRINT "IN SUBROUTINE FIRST" : RETURN 1490 REM SECOND SUBROUTINE 1500 PRINT : PRINT "IN A DIFFERENT NAMED SUBROUTINE" 1510 PRINT "IN SUBROUTINE DEUX" : RETURN 1990 REM THIRD SUBROUTINE 2000 PRINT : PRINT "IN SUBROUTINE THIRD" : RETURN 2490 REM LINE CALLED FOURTH 2500 PRINT : PRINT "LINE CALLED FOURTH": GOTO 120 2990 REM LINE CALLED FVTH 3000 PRINT : PRINT "LINE CALLED FVTH": GOTO 150 3490 REM LINE CALLED SIXTH 3500 PRINT : PRINT "LINE CALLED SIXTH": GOTO 150 4980 REM 4990 REM LOAD IN MACHINE LANGUAGE PROGRAM 5000 LOW = 768:HIGH = 903 5010 OK = 1 5020 REM LOAD IN GROUP OF SIXTEEN 5030 FOR J = LOW TO HIGH STEP 16 5040 CHECK = 0 5050 FOR K = J TO J + 15 5060 READ IT 5070 CHECK = CHECK + IT 5080 NEXT K 5090 REM CHECK IF CHECKSUM OKAY 5100 READ SUM 5110 L$ = "OKAY": IF CHECK < > SUM THEN L$ = "BAD":OK = 0 5120 PRINT L$ 5130 NEXT J 5140 IF OK = 0 THEN STOP 5150 REM THINGS ARE OKAY - LOAD INTO MEMORY 5160 RESTORE : FOR J = LOW TO HIGH STEP 16 5170 FOR K = J TO J + 15: READ IT: POKE K,IT: NEXT K 5180 READ IT: NEXT J 5190 PRINT "BLOAD OKAY": PRINT : PRINT 5200 REM SET THE AMPERSAND VECTOR 5210 REM NOT NEEDED IF CALLED BY BRUN STATEMENT 5220 CALL 768: RETURN 6000 DATA 169,76,141,245,3,169,16,141,246 6010 DATA 3,169,3,141,247,3,96,1868 6020 DATA 201,176,240,13,201,171,240,41,201 6030 DATA 180,240,49,162,16,76,18,2225 6040 DATA 212,32,177,0,169,3,32,214,211 6050 DATA 165,185,72,165,184,72,165,2058 6060 DATA 118,72,165,117,72,169,176,72,32 6070 DATA 183,0,32,68,3,76,210,1565 6080 DATA 215,32,177,0,32,123,221,32,82 6090 DATA 231,76,65,217,32,177,0,1712 6100 DATA 32,248,230,72,201,176,240,13,201 6110 DATA 171,240,9,201,175,208,188,2605 6120 DATA 104,32,177,0,72,198,161,208,4 6130 DATA 104,76,16,3,165,161,72,1553 6140 DATA 32,177,0,32,123,221,32,82,231 6150 DATA 32,183,0,201,44,208,5,1603 6160 DATA 104,133,161,208,224,104,104,96,0,0,0,0,0,0,0,0,1134