3

GW-BASIC : Methods of making Self-modifying programs just using Basic ?

I wonder if GW-BASIC may have had a method for allowing the creation of Self-modifying programs just using Basic, without needing to use Peek's or Poke's, and not accessing disk-drives or similar memory .

At the moment, I'm wondering if you could open the actual file of the 'current program' .

I'm looking at the OPEN instruction detailed at www.antonis.de, and wondering if it may contain a method of opening the actual file of the 'current program' .

TWO QUESTIONS I HAVE AT THE MOMENT ARE -
OPEN "0" [ Could "0" ( THE NUMBER "0"), specify the location of the 'current program' file ? ]
OR,
OPEN "0: " FOR OUTPUT AS #0 [ Could #0 ( The number "0" after the # ) in this line, somehow specify the file of the 'current program' ? ]


The document I'm looking at at the moment is www.antonis.de -

OPEN "O",#1,"DATA" [ "Open the file in output (O) mode" from www.antonis.de , SO THAT SPECIFIES THE LETTER "O" FOR "O" MODE, NOT A LOCATION NAMED "0" ( SPECIFYING THE NUMBER "0" ), WHICH IS ONE OF MY TWO QUESTIONS ]

Normally, I have only seen the following type of format -
OPEN "C: PROGRAM" FOR OUTPUT AS #1


3
  • 2
    Possible duplicate of this: retrocomputing.stackexchange.com/questions/11286/… – another-dave Oct 22 '20 at 2:10
  • #N is simply a channel number that can be used in I/O statements. I see no reason why #0 is even valid (your link says numbers start from 1), much less that it has a mysterious association to the source file. – another-dave Oct 22 '20 at 2:20
  • Doesn't any BASIC that can POKE in the program's memory have self-modifying capabilities? – the busybee Oct 22 '20 at 6:22
2

Pretty much any program that can do file I/O can open the source file it came from, and overwrite it. The trick is merely knowing what that file is.

That is not "self-modifying code" under any common definition of the term, since the program is almost certainly not "running" from the source file.

The sole exception might be a wholly interpreted language for which the interpreter re-reads the source for every statement executed, and the source file either permits writes in situ while being read, or else the interpreter closes and reopens the file (thus seeing the updated instance) between statements.

Simple-minded scripting languages like MS-DOS so-called 'batch' processing might do that, but I think it's not common, because it's so inefficient.

4
  • I confirm that .bat files can be modified while running and the modification affects the current execution (with mostly syntax errors...) – Jean-François Fabre Oct 22 '20 at 13:12
  • I can also confirm that Bourne shell script works the same way from personal experience... and that can't be changed because vendors like GOG.com distribute multi-gigabyte MojoSetup installers, MojoSetup is based on makeself, and tools like makeself and shar work by concatenating compressed data onto a shell script with the only boundary being eval $finish; exit $res\n (as MojoSetup's fork of makeself phrases it.) – ssokolow Oct 22 '20 at 15:01
  • MojoSetup is particularly tricky because it's a shell script bootstrap which unpacks a tarball containing MojoSetup (because tar is required by POSIX), and then MojoSetup unpacks a Zip file concatenated onto the end of it and, given that the install resourcers and game data are both in the Zip portion, if they ever bring GOG Galaxy to Linux, GOG will probably replicate what they did with their patched Inno Setup on Windows so an installer would be sh+tar+zip with GOG Galaxy chunk files shoved into that stack of concatenation somewhere to cut re-zipping the game data out of build times. – ssokolow Oct 22 '20 at 15:08
  • @Jean-FrançoisFabre: Interestingly, CP/M executes batch files by having the command processor look for a file with a magic name (I forget that name), reading the last block of such a file, shrinking the file by one block, and executing the command it just read. To submit a batch file, one runs a utility that writes the commands therein in to the magic file, one per block, in reverse order. – supercat Oct 23 '20 at 19:23
1

I wonder if GW-BASIC may have had a method for allowing the creation of Self-modifying programs just using Basic, without needing to use Peek's or Poke's, and not accessing disk-drives or similar memory .

Nope. You can't do this in GW-BASIC without implementing some sort of VM that takes data and interprets it as commands. GW-BASIC never had an eval-type command that would take a string and execute from it.

That being said GW-BASIC does have the CHAIN MERGE command to bring in BASIC text from a file and overlay it on the currently running program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.