dcpu-16

Can you write to [PC]?


According to the DCPU specification, the only time a SET instruction fails is if the a value is a literal.

So would the following work?

SET [PC],0x1000

A more useful version would be setting an offset of PC, so a rather strange infinite loop would be:

SET [PC+0x2],0x89C3 ; = SUB PC,0x2

Solution

  • Probably (= I think it should work but I didn't try).

    This is called "self modifying" code and was quite common the 8bit era because of a) limited RAM and b) limited code size. Code like that is very powerful but error prone. If your code base grows, this can quickly become a maintenance nightmare.

    Famous use cases:

    1. Windows 95 used code like this to build graphics rendering code on the stack.
    2. Viruses and trojans use this as an attack vector (write code on the stack or manipulate return addresses to simluate a JMP)
    3. Simulate switch statements on the C64