Lably
Skip to content

Self-modifying code

Source code that alters its instructions to the hardware while executing

  • 16 min read
  • 7 chapters

Related on Lably

Through Computer science, Execution (computing) and Input/output

Self-modifying code can involve overwriting existing instructions or generating new code at run time and transferring control to that code.

Self-modification can be used as 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.

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 System/360 architecture and its successors up to z/Architecture, an EXECUTE (EX) instruction logically overlays the second byte of its target instruction with the low-order 8 bits of register 1. This provides the effect of self-modification, although the actual instruction in storage is not altered.

Application in low and high level languages

Self-modification can be accomplished in a variety of ways depending upon the programming language and its support for pointers and/or access to dynamic compiler or interpreter "engines":

  • overlay of existing instructions (or parts of instructions such as opcode, register, flags or addresses)
  • direct creation of whole instructions or sequences of instructions in memory
  • creation or modification of source code statements followed by a "mini compile" or a dynamic interpretation (see eval statement)
  • creating an entire program dynamically and then executing it

Assembly language

Self-modifying code is quite straightforward to implement when using assembly language. Instructions can be dynamically created in memory (or else overlaid over existing code in non-protected program storage), in a sequence equivalent to the ones that a standard compiler may generate as the object code. With modern processors, there can be unintended side effects on the CPU cache that must be considered. The method was frequently used for testing "first time" conditions, as in this suitably commented IBM/360 assembler example. It uses instruction overlay to reduce the instruction path length by (N × 1) − 1, where N is the number of records on the file (−1 being the overhead to perform the overlay).

SUBRTN NOP OPENED FIRST TIME HERE? * The NOP is x'4700'<span typeof="mw:Entity" id="mwVg"><</span>Address_of_opened> OI SUBRTN+1,X'F0' YES, CHANGE NOP TO UNCONDITIONAL BRANCH (47F0...) OPEN INPUT AND OPEN THE INPUT FILE SINCE IT'S THE FIRST TIME THRU OPENED GET INPUT NORMAL PROCESSING RESUMES HERE ...

Alternative code might involve testing a "flag" each time through. The unconditional branch is slightly faster than a compare instruction, as well as reducing the overall path length. In later operating systems for programs residing in protected storage, this technique could not be used, and so changing the pointer to the subroutine would be used instead. The pointer would reside in dynamic storage and could be altered at will after the first pass to bypass the OPEN (having to load a pointer first instead of a direct branch and link to the subroutine would add N instructions to the path length – but there would be a corresponding reduction of N for the unconditional branch that would no longer be required).

Below is an example in Zilog Z80 assembly language. The code increments register B in range [0, 5]. The CP compare instruction is modified on each loop.

<span id="mwZg"></span><span class="c1" id="mwZw">;==========</span> <span class="k" id="mwaA">ORG</span><span class="w" id="mwaQ"> </span><span class="mh" id="mwag">0H</span> <span class="nf" id="mwaw">CALL</span><span class="w" id="mwbA"> </span><span class="nv" id="mwbQ">FUNC00</span> <span class="nf" id="mwbg">HALT</span> <span class="c1" id="mwbw">;==========</span> <span class="nl" id="mwcA">FUNC00:</span> <span class="nf" id="mwcQ">LD</span><span class="w" id="mwcg"> </span><span class="nv" id="mwcw">A</span><span class="p" id="mwdA">,</span><span class="mi" id="mwdQ">6</span> <span class="nf" id="mwdg">LD</span><span class="w" id="mwdw"> </span><span class="nv" id="mweA">HL</span><span class="p" id="mweQ">,</span><span class="nv" id="mweg">label01</span><span class="o" id="mwew">+</span><span class="mi" id="mwfA">1</span> <span class="nf" id="mwfQ">LD</span><span class="w" id="mwfg"> </span><span class="nv" id="mwfw">B</span><span class="p" id="mwgA">,(</span><span class="nv" id="mwgQ">HL</span><span class="p" id="mwgg">)</span> <span class="nl" id="mwgw">label00:</span> <span class="nf" id="mwhA">INC</span><span class="w" id="mwhQ"> </span><span class="nv" id="mwhg">B</span> <span class="nf" id="mwhw">LD</span><span class="w" id="mwiA"> </span><span class="p" id="mwiQ">(</span><span class="nv" id="mwig">HL</span><span class="p" id="mwiw">),</span><span class="nv" id="mwjA">B</span> <span class="nl" id="mwjQ">label01:</span> <span class="nf" id="mwjg">CP</span><span class="w" id="mwjw"> </span><span class="kc" id="mwkA">$</span><span class="mi" id="mwkQ">0</span> <span class="nf" id="mwkg">JP</span><span class="w" id="mwkw"> </span><span class="nv" id="mwlA">NZ</span><span class="p" id="mwlQ">,</span><span class="nv" id="mwlg">label00</span> <span class="nf" id="mwlw">RET</span> <span class="c1" id="mwmA">;==========</span>

Self-modifying code is sometimes used to overcome limitations in a machine's instruction set. For example, in the Intel 8080 instruction set, one cannot input a byte from an input port that is specified in a register. The input port is statically encoded in the instruction itself, as the second byte of a two-byte instruction. Using self-modifying code, it is possible to store a register's contents into the second byte of the instruction, then execute the modified instruction in order to achieve the desired effect.

High-level languages

Some compiled languages explicitly permit self-modifying code. For example, the ALTER verb in COBOL may be implemented as a branch instruction that is modified during execution. Some batch programming techniques involve the use of self-modifying code. Clipper and SPITBOL also provide facilities for explicit self-modification. The Algol compiler on B6700 systems offered an interface to the operating system whereby executing code could pass a text string or a named disc file to the Algol compiler and was then able to invoke the new version of a procedure.

With interpreted languages, the "machine code" is the source text and may be susceptible to editing on-the-fly: in SNOBOL the source statements being executed are elements of a text array. Other languages, such as Perl and Python, allow programs to create new code at run-time and execute it using an eval function, but do not allow existing code to be mutated. The illusion of modification (even though no machine code is really being overwritten) is achieved by modifying function pointers, as in this JavaScript example:

<span id="mwrg"></span><span class="w" id="mwrw"> </span><span class="kd" id="mwsA">var</span><span class="w" id="mwsQ"> </span><span class="nx" id="mwsg">f</span><span class="w" id="mwsw"> </span><span class="o" id="mwtA">=</span><span class="w" id="mwtQ"> </span><span class="kd" id="mwtg">function</span><span class="w" id="mwtw"> </span><span class="p" id="mwuA">(</span><span class="nx" id="mwuQ">x</span><span class="p" id="mwug">)</span><span class="w" id="mwuw"> </span><span class="p" id="mwvA">{</span><span class="k" id="mwvQ">return</span><span class="w" id="mwvg"> </span><span class="nx" id="mwvw">x</span><span class="w" id="mwwA"> </span><span class="o" id="mwwQ">+</span><span class="w" id="mwwg"> </span><span class="mf" id="mwww">1</span><span class="p" id="mwxA">};</span> <span class="w" id="mwxQ"> </span><span class="c1" id="mwxg">// assign a new definition to f:</span> <span class="w" id="mwxw"> </span><span class="nx" id="mwyA">f</span><span class="w" id="mwyQ"> </span><span class="o" id="mwyg">=</span><span class="w" id="mwyw"> </span><span class="ow" id="mwzA">new</span><span class="w" id="mwzQ"> </span><span class="nb" id="mwzg">Function</span><span class="p" id="mwzw">(</span><span class="s1" id="mw0A">'x'</span><span class="p" id="mw0Q">,</span><span class="w" id="mw0g"> </span><span class="s1" id="mw0w">'return x + 2'</span><span class="p" id="mw1A">);</span>

Lisp macros also allow runtime code generation without parsing a string containing program code.

The Push programming language is a genetic programming system that is explicitly designed for creating self-modifying programs. While not a high-level language, it is not as low-level as assembly language.

Compound modification

Prior to the advent of multiple windows, command-line systems might offer a menu system involving the modification of a running command script. Suppose an MS-DOS batch file MENU.BAT contains the following:

:start SHOWMENU.EXE

Upon initiation of MENU.BAT from the command line, SHOWMENU presents an on-screen menu, with possible help information, example usages and so forth. Eventually the user makes a selection that requires a command SOMENAME to be performed: SHOWMENU exits after rewriting the file MENU.BAT to contain

:start SHOWMENU.EXE CALL <i id="mw7A">SOMENAME</i>.BAT GOTO start

Because the command interpreter does not compile a script file and then execute it, nor does it read the entire file into memory before starting execution, nor yet rely on the content of a record buffer, when SHOWMENU exits, the command interpreter finds a new command to execute (it is to invoke the script file SOMENAME, in a directory location and via a protocol known to SHOWMENU), and after that command completes, it goes back to the start of the script file and reactivates SHOWMENU ready for the next selection. Should the menu choice be to quit, the file would be rewritten back to its original state. Although this starting state has no use for the label, it, or an equivalent amount of text is required, because the command interpreter recalls the byte position of the next command when it is to start the next command, thus the re-written file must maintain alignment for the next command start point to indeed be the start of the next command.

Aside from the convenience of a menu system (and possible auxiliary features), this scheme means that the SHOWMENU.EXE system is not in memory when the selected command is activated, a significant advantage when memory is limited.

Control tables

Control table interpreters can be considered to be, in one sense, "self-modified" by data values extracted from the table entries (rather than specifically hand coded in conditional statements of the form IF inputx = 'yyy').

Channel programs

Some IBM access methods traditionally used self-modifying channel programs, where a value, such as a disk address, is read into an area referenced by a channel program, where it is used by a later channel command to access the disk.

Sign in to keep reading

6 more chapters are free with a Lably account.

More in Computing and communication