Self-modifying Code - Application in Low and High Level Languages - Assembly Language

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 (/binary file). 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 x 1)-1 where N is the number of records on the file (-1 being the overhead to perform the overlay).

SUBRTN CLI SUBRTN,X'95' BNE OPENED MVC SUBRTN(4),JUMP OPEN INPUT JUMP B OPENED OPENED GET INPUT ...

(Since the replacement unconditional branch is also slightly faster than a compare instruction, as well as reducing the overall path length, the saved difference in timing between the two instructions is magnified by a factor of N. The 'jump' instruction retains locality of reference and much higher 'visibility' by its close proximity to the overwritten instruction, despite adding an unnecessary extra instruction after the OPEN) 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 & 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).

Read more about this topic:  Self-modifying Code, Application in Low and High Level Languages

Famous quotes containing the words assembly and/or language:

    There is a sacred horror about everything grand. It is easy to admire mediocrity and hills; but whatever is too lofty, a genius as well as a mountain, an assembly as well as a masterpiece, seen too near, is appalling.
    Victor Hugo (1802–1885)

    All official institutions of language are repeating machines: school, sports, advertising, popular songs, news, all continually repeat the same structure, the same meaning, often the same words: the stereotype is a political fact, the major figure of ideology.
    Roland Barthes (1915–1980)