ChatGPT A.I. writes a BASIC program

Previously, I shared my discovery that the new ChatGPT artificial intelligence program had some understanding of programming languages such as C, BASIC and even COBOL. In the days since that discovery, I have continued to experiment with this technology and have found it to be quite useful. It has already helped me with my day job. It was able to optimize a C conversion function that compiled in to 112 assembly instructions down to about 90. (Smaller and faster? Thank you very much, robot.)

But, more interesting to readers here might be my experiments with making the A.I. write BASIC.

I asked:

can you write a program in Microsoft BASIC that encodes a message and can decode it later?

– me, to

It initially showed me this:

REM This is a simple program that uses a substitution cipher to encode and decode a message in Microsoft BASIC

REM Define the alphabet and the substitution key
DIM alphabet(26)
DIM key(26)

REM Define the message to encode
DIM message(40)
message = "HELLO, WORLD!"

REM Define the encoded message
DIM encoded(40)

REM Define the decoded message
DIM decoded(40)

REM Encode the message
FOR i = 1 TO LEN(message)
  encoded(i) = key(INSTR(alphabet, UCASE$(message(i))))

REM Print the encoded message
FOR i = 1 TO LEN(encoded)
  PRINT encoded(i);

REM Decode the message
FOR i = 1 TO LEN(encoded)
  decoded(i) = alphabet(INSTR(key, UCASE$(encoded(i))))

REM Print the decoded message
FOR i = 1 TO LEN(decoded)
  PRINT decoded(i);

Well, that might be some type of BASIC, but it wasn’t what I meant. So I added:

convert that to older interpreted BASIC, which requires line numbers. Make sure no variables are longer than two characters. All variables should be in uppercase.

– me, to

It took a few back-and-forths as it generated code that wasn’t quite compatible with my CoCo. It initially was using strings similar to how C works, declaring a string array for the number of letters in the string. It would then try to access the letters inside the string as A$(1), A$(2), etc. While it would be possible to do this, it seemed silly, and I told it so.

It then removed the DIMs and just started using strings normally.

When it was trying to build a string that way, I’d tell it “my BASIC can’t do that. use MID$ instead” and it would change the code to the traditional “A$=A$+” approach.

A few rounds of this, and is had created a program that almost worked. There were some function call errors, caused by the INSTR command not finding a match and returning 0. It had created code where an INSTR appeared as a parameter inside a MID$ operation. I told it “if the string is not found, INSTR returns 0 which cannot be used in that MID$”.

It corrected that.

Once it finally was working, I didn’t like the output, so I told it:

print the original message at the start. you can remove the ; on the PRINT statements so they print a carriage return

– me to

And at last, this AI-generated masterpiece was complete. And I didn’t write a line of it — I just told it when something wouldn’t work on my BASIC…

10 REM Simple substitution cipher
50 PRINT "Original message: "; M$
60 E$=""
70 FOR I=1 TO LEN(M$)
80 C=INSTR(A$,CHR$(ASC(MID$(M$,I,1))))
90 IF C>0 THEN E$=E$+MID$(K$,C,1) ELSE E$=E$+MID$(M$,I,1)
100 NEXT
110 PRINT "Encoded message: "; E$
120 D$=""
130 FOR I=1 TO LEN(E$)
140 C=INSTR(K$,CHR$(ASC(MID$(E$,I,1))))
150 IF C>0 THEN D$=D$+MID$(A$,C,1) ELSE D$=D$+MID$(E$,I,1)
160 NEXT
170 PRINT "Decoded message: "; D$

And, I could have kept giving it more suggestions on what to do. Make them in to subroutines? Sure! Change the encoding method? No problem.

OpenAI’s ChatGPT writes a BASIC program that actually runs (shown on a CoCo).

We live in wondrous times, for sure!

In 1982, I received my first computer: a $299.99 Commodore VIC-20. A year later, I moved on to a 64K Radio Shack Color Computer ("CoCo"). In 1990, I co-founded Sub-Etha Software "in Support of the CoCo and OS-9". This later led me to a job at Microware, creator of OS-9. I am author of the CoCoFest Chronicles, a compilation of my fest reports covering the 1990s era. I also host the wiki. These days, I am enjoying excavating my original VIC-20 tapes and thousands of CoCo floppy disks...

3 thoughts on “ChatGPT A.I. writes a BASIC program

    1. I’ve had it write BATCH files, PowerShell scripts, C routines, and more. I’m surprised it knew what LabWindows is (a GUI IDE) and it was able to generate a simple GUI program that was 99% ready to compile. I was able to tell it to “add a second button labeled COUNT and a text box containing a number, and every time I click on COUNT the number goes up.” And it did. Mind blown. It even was aware of a PIC24 compiler from a company called CCS (I use it at work) and only had a few words I had to edit, and it made something that built and ran. Ridiculous!

Leave a Reply

Your email address will not be published. Required fields are marked *