Self-modifying Code

In computer science, self-modifying code is code that alters its own instructions while it is executing - usually to reduce the instruction path length and improve performance or simply to reduce otherwise repetitively similar code, thus simplifying maintenance. Self modification is an alternative to the method of 'flag setting' and conditional program branching, used primarily to reduce the number of times a condition needs to be tested for. The term is usually only applied to code where the self-modification is intentional, not in situations where code accidentally modifies itself due to an error such as a buffer overflow.

The method is frequently used for conditionally invoking test/debugging code without requiring additional computational overhead for every input/output cycle.

The modifications may be performed:-

  • only during initialization - based on input parameters (when the process is more commonly described as software 'configuration' and is somewhat analogous, in hardware terms, to setting jumpers for printed circuit boards). Alteration of program entry pointers is an equivalent indirect method of self-modification, but requiring the co-existence of one or more alternative instruction paths, increasing the program size.
  • throughout execution ('on-the-fly') - based on particular program states that have been reached during the execution

In either case, the modifications may be performed directly to the machine code instructions themselves, by overlaying new instructions over the existing ones (for example: altering a compare and branch to an unconditional branch or alternatively a 'NOP'). In the IBM/360 and Z/Architecture instruction set, an EXECUTE (EX) instruction overlays its target instruction (in its 2nd byte) with the lowest order 8 bits of register 1, as a standard, legitimate method of (temporary) instruction modification.

Read more about Self-modifying Code:  Application in Low and High Level Languages, History, Usage, Advantages, Disadvantages

Other articles related to "code":

Subroutine - History - Self-modifying Code
... The first use of subprograms was on early computers that were programmed in machine code or assembly language, and did not have a specific call instruction ... lower level machine instructions that relied on self-modifying code ...
Self-modifying Code - Disadvantages
... Self-modifying code is harder to read and maintain because the instructions in the source program listing are not necessarily the instructions that will be executed ... Self-modifying code can be rewritten as code that tests a flag and branches to alternative sequences based on the outcome of the test ... Self-modifying code avoids making such a test on every occasion, and typically runs faster ...

Famous quotes containing the word code:

    ... the self respect of individuals ought to make them demand of their leaders conformity with an agreed-upon code of ethics and moral conduct.
    Mary Barnett Gilson (1877–?)