First Day of Coding…but Not Assembly Yet

I was able to code 2 programs today from the book “Machine Language for Beginners” by Richard Mansfield – both in BASIC for Atari on my Atari 800XL. The first program was smooth. It is a cool binary quiz for converting binary into decimal.

(Program RUN)

(Program Code)

However, when trying to run the second program…the one that would “print out the entire table of binary numbers from 0 to 255″…I get an ERROR 9 indicating a DIM error.

(Program RUN)

(Program Code)

Looking at LINE 150, it is K(C)=48. This indicates to me that K needs to be DIMmed. After DIMming, I still get various errors, and Roger (ChatGPT) and I try some different solutions to make this code work. By the way, this is the original code from the book (you can verify this in the featured image):

100 REM COMPLETE BINARY TABLE
110 L=8:B=2:C=1
120 FORX=0TO255:PRINTX;
140 IFXANDTHENK(C)=49:GOTO160
150 K(C)=48
160 C=C+1:IFBANDXTHENK(C)=49:GOTO180
170 K(C)=48
180 B=B*2:IFC>8THEN200
190 GOTO160
200 FORI=0TO7:PRINTSTR$(K(L)-48);:L=L-1
210 NEXT
220 C=0:PRINT
260 L=8:B=2:C=1:NEXTX

I’ve heard of saving space, but this is basically unreadable, and I don’t think it would be functional as is. When I typed it, I corrected LINE 160, for example, to

C=C+1: IF B AND X THEN K(C)=49: GOTO 180

but I believe LINE 140 had a problem with something missing:

IF X AND THEN K(C)=49: GOTO 160

Once I was finally able to manipulate the original code enough to get it to run, this was the output. Getting to this point involved PRINTing the output of C and B, and realizing there was overflow happening. This original code kept needing various variable bounds added to make it work correctly. It was a disaster. So…

…I decided to start from scratch, and I asked Roger (ChatGPT) to “Write me an Atari basic program that will print out the entire table of binary numbers from 0 to 255.” The following is what he provided:

10 DIM B$(8) ' Reserve space for binary string 
20 FOR N = 0 TO 255 
30 B$ = "" ' Reset binary string for each number 
40 X = N ' Temporary variable for calculation 
50 FOR I = 7 TO 0 STEP -1 
60 IF X >= 2^I THEN B$ = B$ + "1" : X = X - 2^I ELSE B$ = B$ + "0" 
70 NEXT I 
80 PRINT N; " = "; B$ ' Print decimal number and its binary equivalent 
90 NEXT N 
100 END

Anyone who has programmed Atari Basic for awhile (not me) sees the error right way. Although the debugger was telling me there was an error with B$ + “1”, I didn’t know why. As it turns out, string concatenation is a little clumsier to write in Atari BASIC. You can’t simply add two strings together with the plus sign. Instead, it needed to be modified like B$(LEN(B$)+1)=Z$ (by this point, I had put the string value of “1” into the Z$ variable).

And finally, after tinkering with the original code from the book, and then tinkering with the code from Roger (ChatGPT), I got a working program. Based on my coding experience, I believe this code makes a lot more sense then what the book was trying to accomplish.

Final Code:

10 DIM B$(8)
15 DIM P$(1),Z$(1)
16 P$="1"
17 Z$="0"
20 FOR N = 0 TO 255 
30 B$ = "" 
40 X = N 
50 FOR I = 7 TO 0 STEP -1 
60 IF X>=2^I THEN GOTO 70
61 IF X<2^I THEN GOTO 65
65 B$(LEN(B$+1)=Z$
66 GOTO 80
70 B$(LEN(B$)+1)=P$:X=X-2^I 
80 NEXT I 
90 PRINT N; " = "; B$ 
100 NEXT N 
110 END

All this, and I’m now moving on to page 3 of “Machine Language for Beginners” by Richard Mansfield to learn My 6502 bit by bit.

Leave a Reply