Skip to content

Instantly share code, notes, and snippets.

@SharzyL
Created September 22, 2019 06:30
Show Gist options
  • Save SharzyL/2ddd56a6ae58d10e533236d6fde423a4 to your computer and use it in GitHub Desktop.
Save SharzyL/2ddd56a6ae58d10e533236d6fde423a4 to your computer and use it in GitHub Desktop.
A self-copying program using a simple machine language
| LOAD: ;load some consts
0x00 | load (const1), 1
0x02 | load (span), 0x70
0x04 | load (code-start), 0 #
0x06 | load (data-offset), 0x3C
0x08 | load (code-len), 0x46
0x0A | load (data-len), 0x0A
|
| COPY_LOOP: ;copy current code to 0x70
0x0C | mov (code-start), (crnt-addr)
0x0E | add (zero), (code-start), (code-len) ;init loop var
|
0x10 | store (crnt-addr), [pos1](0x17) #
0x12 | add (tmp), (crnt-addr), (span)
0x14 | store (tmp), [pos2](0x19) #
|
0x16 | mov (tmp), <pos1>
0x18 | store (tmp), <pos2> ;copy
|
0x1A | add (crnt-addr), (crnt-addr), (const1) ;increment addr
0x1C | jmp (crnt-addr), .MODIFY_LOOP #
0x1E | jmp (zero), COPY_LOOP + 4 #
|
|
| MODIFY_LOOP: ;modify copied code
0x20 | add (crnt-addr), (code-start), (data-offset)
0x22 | add (zero), (crnt-addr), (data-len) ;init loop var
|
0x24 | store (crnt-addr), [pos3](0x27) #
|
0x26 | mov (tmp), <pos3> ;get code offset
0x28 | add (tmp), (tmp), (span)
0x2A | add (tmp), (tmp), (code-start) ;get code addr
0x2C | store (tmp), [pos4](0x31) #
0x2E | store (tmp), [pos5](0x35) #
|
0x30 | mov (tmp), <pos4> ;load code
0x32 | add (tmp), (tmp), (span) ;modify code
0x34 | store (tmp), <pos5> ;write back
|
0x36 | add (crnt-addr), (crnt-addr), (const1) ;increment addr
0x38 | jmp (crnt-addr), 0x70 #
0x3A | jmp (zero), .MODIFY_LOOP + 4 #
|
|
| DATA: ;offsets to modify later
0x3C | 0x05, 0x11,
0x3E | 0x15, 0x1D,
0x40 | 0x1F, 0x25,
0x42 | 0x2D, 0x2F,
0x44 | 0x39, 0x3B
0x46 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment