Self-modifying code: Difference between revisions

Content deleted Content added
computer programming -> computer science
m spelling, punctuation
Line 3:
 
==Assembly style self-modifying code:==
The kinds of self-modifying code that are used in assembly, can be for various purpuses:
 
# Optimisation of a state dependant loop.
Line 23:
}
 
Self -modifying code in this case would simply be a matter of rewriting the loop like this:
 
repeat N times {
Line 35:
}
 
Note that 2-state replacement of the opcode, can be easly written as
'xor var at address with the value "opcodeOf(Inc) xor opcodeOf(dec)"'
 
Line 46:
In some cases self-modifying code executes slower on modern processors. This is because a modern processor will usually try to keep blocks of code in its [[cache]] memory. Each time the program rewrites a part of itself, the rewritten part must be loaded into the cache again, which results in a slight delay.
 
The cache invalidation issue on modern processors, usualyusually means, that self modifing-modifying code would still be faster, only when the modification will occureoccur seldomlyrarely. Such as in the case of a state switching in an inner loop.
This concideration is not unique to processors with code cache, since on any processor, rewriting the code never does come for free.
 
Self-modifying code was used in the early days of computers in order to save memory space, which was limited. It was also used to implement [[subroutine]] calls and returns when the instruction set only provided simple branching or skipping instructions to vary the flow of control (this is still relevant in certain ultra-[[RISC]] architectures, at least theoretically, e.g. one such system has a sole branching instruction with three operands: subtract-and-branch-if-negative).
Line 81:
==TODO==
#an example and discussion of 'high-level' self-modifying code such as in LISP.
# examples and discussion of traditional uses of self-modifying code, such as in graphic Blitting units, specialisation of algorithms (like sort with embed'ingembedding a cmp), and in interpeterinterpreter kernalskernels.