![](/squares_tiny/publisher/0/0/0/0/0/5/5/0/pcn.jpg)
Personal Computer News
![](/themeForest/images/clock.png)
Published in Personal Computer News #077
Spectrum Debugging With Variable Results
The subroutine below is a debugging aid that traces the path taken through a Spectrum Basic program and prints the content of any variable.
Add the instruction GOSUB 9995 to the program lines to be debugged and RUN your program. Whenever the subroutine is called, the line and statement number from which the call is made will be printed at the bottom of the screen and the program will PAUSE. Press any key (except Break) to single step through each line containing GOSUB 9995, or press 'P' if you wish to print the content of the variable.
When printing the content of variables, you may use many of the Spectrum variable handling functions. A few examples are:
A$
A$(5 TO 9)
PEEK 23635 + 256 * PEEK 23636
INT((PEEK 23609)/6
BIN 11111100 (this will return the decimal equivalent)
Note the use of that mysterious VAL$ function in line 9998 of the subroutine. If you make a mistake when entering the variable(s) and get the error message 'Nonsense in Basic', re-enter the subroutine with the direct command GOTO 9997. If you Break out of the subroutine, you will have to enter CLOSE#2.
In the subroutine, PEEK 23613/4 gives the ERR SP address from which point the GOSUB stack can be referenced to obtain the RETURN line and statement numbers. POKE 23692,1 prevents the line and statement numbers being scrolled up the screen and OPEN#2,'K' opens the channel for printing to the lower screen.
If your program already uses the string variable p$, you will have to use another variable name in the subroutine. After printing variables, return to the main program by just pressing Enter in response to the 'Variable?' prompt.
9995 LET trace=PEEK 23613+256*PEEK 23614: POKE 23692,1: LET tracex=PEEK (trace+2)+256*PEEK (trace+3): LET tracez=PEEK (trace+4)=1: OPEN #2,"K": PRINT AT 0,0;tracex;".";tracez;" ": PAUSE 0: IF INKEY$="p" OR INKEY$="P" THEN GO SUB 9997
9996 CLOSE #2: RETURN
9997 INPUT AT 0,0;(tracex);".";(tracez);" Variable? "; LINE p$: IF p$="" THEN PRINT AT 0,0;" ": RETURN
9998 PRINT AT 0,0;tracex;".";tracez;" ";p$;"=";: IF LEN p$>=2 THEN IF p$(2)="$" THEN PRINT """";VAL$ p$;"""": PAUSE 0: GO TO 9997
9999 PRINT VAL p$: PAUSE 0: GO TO 9997
DPJ Brown
Innsworth, Gloucester