In [[computer programming]], '''self-modifying code''' is code that modifies itself. This is straightforward to write when using [[assembly language]] and is also supported by some high level language interpreters such as [[SNOBOL4]] or the [[Lisp programming language]]. It is more difficult to implement on compilers but compilers such as [[Clipper programming language|Clipper]] and [[Spitbol]] make a fair attempt at it. Batch programming scripts[[scripting programming language|script]]s often involve self modifying code as well.
Self-modifying code is often executingexecutes slower on mordernmodern processors. This is because mosta modern processorsprocessor will usually try to keep codeblocks fromof datacode in its [[cache]] memoriesmemory. Each time the processorprogram noticerewrites a switchpart fromof codeitself, tothe datarewritten (whenpart themust programbe rewritesloaded ainto partthe ofcache itself)again, orwhich fromresults datain toa codeslight (whendelay. theUse programof jumpsself-modifying tocode theis modifiednot code)recomended itwhen willa takeviable aalternative litleexists, whilebecause forsuch itcode can be difficult to updateunderstand itsand entiresmaintain.
Self modifying code is not recomended to use when alternatives exist. This is because such code can be difficult to understand and maintain.
Self-modifying code was used in the early days of computers in order to save memory space, inwhich computers with very small main memorywas sizeslimited. 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 singlesole branching instruction with three operands: subtract-and-branch-if-negative).
Self-modifying code was used to hide copy protection instructions in [[1980s]] [[MS-DOS]] based games. The [[floppy disk]] drive access instruction '[[interrupt|int]] 0x13' would not appear in the executable program's image but it would be written into the executable's memory image after the program started executing. ItSelf-modifying code is also sometimes used by programs that doesdo not want to reveal their presence; such as [[computer virus]]es and some [[shellcode]]s use this technique.