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.
can you write a program in Microsoft BASIC that encodes a message and can decode it later?– me, to chat.openai.com
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) alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" key = "QWERTYUIOPASDFGHJKLZXCVBNM" 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)))) NEXT REM Print the encoded message FOR i = 1 TO LEN(encoded) PRINT encoded(i); NEXT REM Decode the message FOR i = 1 TO LEN(encoded) decoded(i) = alphabet(INSTR(key, UCASE$(encoded(i)))) NEXT REM Print the decoded message FOR i = 1 TO LEN(decoded) PRINT decoded(i); NEXT
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 chat.openai.com
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 chat.open.ai
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 20 A$="ABCDEFGHIJKLMNOPQRSTUVWXYZ" 30 K$="QWERTYUIOPASDFGHJKLZXCVBNM" 40 M$="HELLO, WORLD!" 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.
We live in wondrous times, for sure!