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:
“That man is to be pitied who cannot enjoy social intercourse without eating and drinking. The lowest orders, it is true, cannot imagine a cheerful assembly without the attractions of the table, and this reflection alone should induce all who aim at intellectual culture to endeavor to avoid placing the choicest phases of social life on such a basis.”
—Mrs. H. O. Ward (18241899)
“There is no such thing as a language, not if a language is anything like what many philosophers and linguists have supposed. There is therefore no such thing to be learned, mastered, or born with. We must give up the idea of a clearly defined shared structure which language-users acquire and then apply to cases.”
—Donald Davidson (b. 1917)