Beginner’s Column – Error Codes and Debugging Programs

Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n4 (Volume 2, Issue 4)
Date: April 1981
Pages: 11-12

One of the ZX80/MicroAce‘s nicest features keeps you from entering lines with syntactic errors into a program. This saves you much time and irritation in trying to get your programs to run. But some errors in logic can get by the line checker and your program won’t run, usually because you’ve asked the impossible or you haven’t given complete enough instructions. Remember, the computer won’t know what you want unless you tell it specifically.

Fortunately, the machine tells you exactly where the program screwed up. It runs the program as far as possible, then gives you an error code and the line number where the problem occurred. They appear at the bottom left of the screen as error number/line number in a ZX80 and error number:line number on a MicroAce. Error codes are on page 99 of a ZX80 manual and page 54 of a MicroAce manual.

These error codes can be invaluable in helping you debug your program. Error code 0 means it ran with no problems. Error code 9 means it ran with no problems until it hit a STOP statement. But if you get an error code 1-8, you have trouble.

Error code 1 means you set up a FOR-NEXT loop like FOR X=1 TO 10 … PRINT X … NEXT T. The computer doesn’t know what NEXT T means (assuming you defined T) because X, not T, is the control variable for the FOR-NEXT loop. Make sure you use variable names consistently.

Error code 2 means that you threw in a variable without defining it. Work back from the offending line and make sure that every letter variable you use has a line to define it. In the FOR-NEXT loop example above, FOR X=1 TO 10 defines the variable X. Statements like LET A=0 or INPUT A$ define A and A$. Note the difference between the variable A and the string variable A$. Defining one does not define the other although they use the same letter. The most common source of this error is just mistyping a variable name.

Error code 3 occurs when you use a subscript greater than the maximum in an array you set up using a DIM statement (more on DIM statements later). For example, if your array A has 5 elements and you use A(10), you’ll get an error 3. A subscript that is an expression, like A(2*I), that evaluates to greater than the maximum subscript in the array also gives error 3.

Error code 4 occurs with LET, INPUT, DIM or PRINT statements and means that you’ve overshot the limit on the number or length of variables you can use because there isn’t enough memory for all of them. Variable names and strings can be any length, but can use up only as much memory as you have. Your only solution is to shorten something in the memory, like the variables, screen display or program length.

Error code 5 comes only from PRINT statements and means you ran out of display room on the screen. The best cure for this is just to cut the stuff you want to print.

Error code 6 means the answer to a calculation has gone over 32767 or under -32768, the largest and smallest numbers the computer’s arithmetic register can handle. Just ask it to calculate a smaller number. Sometimes all you need to do is use brackets in a long calculation to make sure the running total never exceeds the limits.

Error code 7 means the computer hit a RETURN statement with no corresponding GO SUB. Subroutines consist of a GO SUB statement that sends the computer to the subroutine and end with a RETURN statement to send it back to where it left. If it finds a RETURN with no GO SUB to go back to, it’s lost and stops. (See Dec., 1980 issue on subroutines.)

Error code 8 occurs when you try to use INPUT as a direct command instead of as a program command. To avoid this error, only use INPUT as part of a program line.

You can use most other commands directly, not as part of a program. Our trick for finding out how much memory is left is one example: typing PRINT 17408-PEEK(16400)-PEEK(16401)*256 gives you bytes remaining and an error code 0/-2. These kinds of commands give error code 0 in lines -1 or -2, but do not mean that you had an error in the line. Other direct commands, like GO TO (a line number), do not give error codes. GO TO functions like RUN if you GO TO the first line of the program; if you GO TO a later line the computer executes the program from that line on. This can also help you find errors in your program by showing you which parts run and which don’t. Try inserting marker PRINT lines, like PRINT “OK SO FAR”, in the middle of calculations to see how far the computer gets before the problem occurs. This narrows the possibilities for guilty lines. The other difference between GO TO and RUN in executing a program is that RUN clears all the variables you may have already entered in the program; GO TO preserves them.