Self modifying code

 BBS: Inland Empire Archive
Date: 06-28-92 (14:34)             Number: 1656
From: RICK PEDLEY                  Refer#: NONE
  To: AL LAWRENCE                   Recvd: NO  
Subj: Self modifying code            Conf: (2) Quik_Bas
 On 06-26-92 Al Lawrence wrote to All...

 AL> Does anyone have any code that would allow a program to
 AL> modify a numeric or string variable each time the program
 AL> is run, and maintain the modified variable from run to
 AL> run.
 AL>
 AL> Thanks in advance...


In your program, define a string something like this:

Count$ = ">>>#$#$#$"

The alternating #$ characters ensure that when you compile, the compiler
won't pack the string -- you want it to remain exactly as it is in the
source. Also the >>> is easy to spot/find with a hex editor.

DEFINT A-Z
Marker$ = ">>>"
Num& = -1                         ' in this case means 'outta luck'.
B$ = SPACE$(3)                    ' 3 chars, we're looking for Marker$.
OPEN PROG.EXE FOR BINARY AS #1    ' open file in binary mode.
FileLen& = LOF(1)                 ' get file length.
FOR x& = 1 TO FileLen& - 9        ' file length minus (LEN(Marker$) + 6).
   SEEK #1, x&                    ' move file pointer to position x&.
   GET #1,,B$                     ' get 3 bytes (binary mode, so no
   IF B$ = Marker$ THEN           '  record number).
      Num$ = SPACE$(6)            ' 6 spaces to hold the number string.
      NumStart& = x& + 3          ' move file pointer to start of Num$
      SEEK #1, NumStart&          '  and keep this value for later.
      GET #1,,Num$                ' get 6 bytes.
      Num& = VAL(Num$)            ' convert to a long integer.
      EXIT FOR
   END IF
NEXT x&

IF Num& = -1 THEN
   CLOSE
   PRINT "Couldn't find ";Marker$
   END
ELSE
   Num& = Num& + 1                ' increment usage counter.
   IF Num& > 25 THEN
      PRINT "NEENER NEENER DEADBEAT, TRIAL PERIOD HAS EXPIRED!"
      END                         ' or SHELL "ECHO y | DEL
*.*" if you prefer.
   ELSE
      Num$ = LEFT$("000000", 6 - LEN(STR$(Num&))) + STR$(Num&)
      SEEK #1, NumStart&          ' write incremented counter to file.
      PUT #1,,Num$
      CLOSE
   END IF
END IF


There are faster ways to find Marker$, like GET a 32K string and use
INSTR to find the ">>>", but the fastest way is once you have the
program finished, hardcode NumStart& as NumStart$ in your program
(i.e. use a hex editor to find exactly where Num$ begins and add 1
to it, as all hex editors count from zero), then SEEK that position
directly; SEEKing through the file byte by byte is slow. Also, the
user could easily install a fresh copy of the program or set the
read only attribute to defeat this method.

Caveat: I typed this in an editor without testing so be careful and
please excuse minor errors.


... OFFLINE 1.37

--- Maximus 2.01wb
 * Origin: The BULLpen BBS * Intel 14.4EX (613)549-5168 (1:249/140)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET