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 PEEKs or POKEs, 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" above, specify the location of the current program file?



Could #0 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 -

  • 2
    Possible duplicate of this: retrocomputing.stackexchange.com/questions/11286/…
    – dave
    Oct 22, 2020 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.
    – dave
    Oct 22, 2020 at 2:20
  • Doesn't any BASIC that can POKE in the program's memory have self-modifying capabilities? Oct 22, 2020 at 6:22

2 Answers 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.

  • I confirm that .bat files can be modified while running and the modification affects the current execution (with mostly syntax errors...) Oct 22, 2020 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, 2020 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, 2020 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, 2020 at 19:23

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.

  • 1
    If one was using a BIOS that handled keyboard input in the same manner as the original IBM PC, one could use the POKE command to stuff the keyboard buffer. If one printed commands to the screen starting at the third line, positioned the cursor at the top of the screen, stuffed some carriage returns into the keyboard buffer, and exited the program, the effect would be to make the BASIC interpreter process the commands on the screen.
    – supercat
    Apr 26, 2021 at 20:09

Not the answer you're looking for? Browse other questions tagged .