Self-modifying code

This is an old revision of this page, as edited by Tarquin (talk | contribs) at 23:10, 27 April 2003. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, self-modifying code is code that modifies itself. This is straightforward to write when using assembly language and is also supported by some high level language interpreters such as SNOBOL4. It is more difficult to implement on compilers but compilers such as Clipper and Spitbol make a fair attempt at it. Batch programming scripts often involve self modifying code as well. Use of self-modifying code is not recommended where alternatives exist. This is because such code can be difficult to understand and maintain.

example algorithm (theoretic!)

Start:
GOTO Decryption_Code
Encrypted:
    ...
    lots of encrypted code!!!
    ...
Decryption_Code:
    *A = Encrypted
Loop:
    B = *A
    B = B XOR CryptoKey
    *A = B
    A = A + 1
    GOTO Loop IF NOT A = (Decryption_Code - Encrypted)
    GOTO Encrypted
 CryptoKey:
    some_random_number

This "program" will decrypt a part of itself and then jump to it.

(*A means "the ___location to which A points")