OpenAI writes a BASIC program!

Note: A slightly updated version of this article appeared earlier on Vintage is the New Old.

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 them as A$(1), A$(2), etc. That seemed silly, and I told it so.

It 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 switch out.

A few rounds of this, and is had created a program that almost worked. There were some errors, caused by the INSTR command returning 0. I told it “if the string is not found, INSTR returns 0 which cannot be used in that MID$”.

It corrected that.

It worked, but I didn’t like the output, so I told it:

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

– me to

And at least, 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.

BASIC written by the OpenAI!

We live in wondrous times, for sure!

3 thoughts on “OpenAI writes a BASIC program!

    1. Allen Huffman Post author

      I agree with you. The AI evolves the code as you keep telling it what to do. I didn’t catch that, else I could have said “I don’t think the CHR$ of an ASC of a MID$ is necessary” and I bet it would have changed it.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.