Skip to content

Instantly share code, notes, and snippets.

@zdavkeos
Created May 23, 2012 01:46
Show Gist options
  • Save zdavkeos/2772786 to your computer and use it in GitHub Desktop.
Save zdavkeos/2772786 to your computer and use it in GitHub Desktop.
Example of self modifying code in Dcpu16 assembly
; 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