Created
May 23, 2012 01:46
-
-
Save zdavkeos/2772786 to your computer and use it in GitHub Desktop.
Example of self modifying code in Dcpu16 assembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Sample of self modifying code in | |
; (Dcpu-16) assembly. Takes advantage | |
; of the fact that the opcodes for | |
; addition and subtraction only differ | |
; by a single bit. | |
; Computes the following series: | |
; B = 10 + 1 - 2 + 3 - 4 + 5 ... | |
SET A, 1 ; initial values | |
SET B, 10 | |
loop: | |
IFE A, 10 ; loop 10 times | |
SET PC, done ; break | |
op: ; mark location of instruction we want to modify | |
SUB B, A ; first run through, op will be SUB | |
SET C, [op] ; fetch instruction from memory | |
XOR C, 1 ; ADD -> SUB, SUB -> ADD | |
SET [op], C ; put modified op back in memory | |
ADD A, 1 ; increment | |
SET PC, loop ; loop again | |
done: ; end of program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment