Jump to content
IGNORED

Self-modifying ATARI Basic program?


ldelsarte

Recommended Posts

Dear Atari fans,

 

Yes, ATARI Basic has weaknesses and we all know them.
I wanted to demonstrate that one of its strengths is that a program can be self-modifying.
I wrote the following two programs:

 

PART1.BAS

10 OPEN #1,8,0,"D:ADDLINES.TXT"
20 PRINT #1;"1000 A=990:B=980"
30 CLOSE #1
40 RUN "D:PART2.BAS"

 

PART2.BAS

10 A=10:B=20
20 ?A,B
40 ENTER "D:ADDLINES.TXT"
50 GOSUB 1000
60 ?A,B
70 END
1000 REM PLACE HOLDER
1999 RETURN

 

Well, it doesn't work as expected at all.
In PART2.BAS, line 40 ENTER "D:ADDLINES.TXT" is executed, which updates line 1000 A=990:B=980
However, the program stops immediately at line 40, and does not execute the rest.

ENTER behaves as if it had been executed in direct mode, and not in a program.
So it returns control to the Atari BASIC editor, instead of continuing at line 50.

 

I have several questions:
[1] Does anyone have an explanation?
[2] Does anyone know of a similar - but working - way to get a program to modify itself by loading bits of code from diskette?
[3] I remember programs that would auto-modify by opening E: or S: in mode 13 (OPEN #1,13,0, "E:" or "S:"), then displaying new lines on the screen, and by positioning the cursor, the editor would simulate a "RETURN", so that the lines were actually added to the program, without the user pressing any keys. Does anyone remember this? And more importantly, does anyone remember how to do this exactly?

 

Thank you very much in advance,
Laurent

part1.bas part2.bas

Link to comment
Share on other sites

12 minutes ago, vitoco said:

What if you add a RUN or GOTO 50 line at the end of the txt file?

 

Ta-da!

Great suggestion @vitoco, it was exactly what was missing!

Thank you for much for this pertinent suggestion.

 

With PART1.BAS now reading

10 OPEN #1,8,0,"D:ADDLINES.TXT"
20 PRINT #1;"1000 A=990:B=980"
25 PRINT #1;"GOTO 50"
30 CLOSE #1
40 RUN "D:PART2.BAS"

It "resumes" PART2.BAS at line 50, as I wished.

 

(enclosed updated PART1.BAS)

 

part1.bas part2.bas

Edited by ldelsarte
Link to comment
Share on other sites

Just for fun, another way to load your file, although a bit long winded compared to @vitoco 's solution.

 

10 ? Chr$(125)
20 A=10:B=20
30 Position 2,10:? A,B

35 REM POSITON EVERTHING ON SCREEN
40 Position 2,2:? "ENTER ";Chr$(34);"D:ADDLINES.TXT";Chr$(34)
45 Position 2,5:? "CONT"
46 Position 2,0
50 Poke 842,13:REM TURN ON AUTO RETURN
60 Stop:REM STOP THE PROGRAM , WILL NOW ENTER FILE
70 Poke 842,12: REM TURN OFF AUTO RETURN

75 REM PROGRAM RESUMES
80 Gosub 1000
90 Position 2,12:? A,B
100 End
1000 Rem DUMMY LINE
1010 Return

  • Thanks 1
Link to comment
Share on other sites

10 minutes ago, TGB1718 said:

Just for fun, another way to load your file

Thanks very much, @TGB1718

My memory was flawed.
In my memory, to do exactly what you are doing (which works perfectly), you had to open a channel with S: or E: in mode 13.
Your program shows that this is unnecessary.
Thanks for the tip!

Link to comment
Share on other sites

Yep, alternate method is putting stuff onscreen then using forced input mode.

Once debugged you can turn screen off or set foreground to same as background colour to hide what's happening.

 

I used this a fair bit in the day, largely for generating Data statements of short asm routines.

 

And yep, other computers tend to not be able to do it, or at least not easily.

C64 you can sort of do it.  But entering a new line clears all variables so you need to Poke variables you want to keep then restore them later.

It has the keyboard buffer so you can just store a bunch of Returns in there.

  • Thanks 1
Link to comment
Share on other sites

6 hours ago, ldelsarte said:

[1] Does anyone have an explanation?
[2] Does anyone know of a similar - but working - way to get a program to modify itself by loading bits of code from diskette?
[3] I remember programs that would auto-modify by opening E: or S: in mode 13 (OPEN #1,13,0, "E:" or "S:"), then displaying new lines on the screen, and by positioning the cursor, the editor would simulate a "RETURN", so that the lines were actually added to the program, without the user pressing any keys. Does anyone remember this? And more importantly, does anyone remember how to do this exactly?

The explanation is that ENTER does enter the direct mode. The way how "enter" works is that it opens the file, then sets the "stdin" of the basic editor to that file, and then enters the direct mode. Thus, the lines the user would otherwise type in through E: now come through the opened file as the direct mode of Basic would continue to read from its stdin, now the file just opened. On an EOF, the Basic interpreter closes the file, and restores its "stdin" to channel #0, which is connected to E:. Thus, what you see is an artefact due to how "Enter" works. It is actually a quite simple command that performs just rediction.

 

[2] was already answered, you need to get code executed by the direct mode that puts basic back into the run mode.

 

[3] works by placing the E: handler into the "auto-enter" mode. That is, the E: handler then no longer waits for return to be pressed, but continues to read data from the screen. If I recall correctly, this was a "POKE 842,13" or so, which is the open-mode of channel #0, connected to E: That is actually not a feature of the basic, but a feature of the operating system.

 

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

2 hours ago, NorbertP said:

I wrote a program to plot graphs of algebraic functions, and used the self modifying code to compensate for the lack of an EVAL() statement - it was a little clunky, but a heck of a lot easier than writing some sort of parser!

I guess I remember that program.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...