Search code examples
mipsself-modifying

Can I execute the number stored in a register as an instruction in MIPS?


If I take an instruction and break it down into the binary representation of its op code, rs, rt etc... could I then put this binary number into a register and get MIPS to treat it as an instruction?

For example:

The instruction: add $t0, $s0, $t0

Breaks down to:

000000 10010 01000 01000 00000 100000

Which corresponds to the integer: 18696

Could I store this integer in a register, and then get MIPS to treat it as an instruction?

I ask this with the idea of self-modifying code in mind.


Solution

  • The correct answer is - no. As pointed out in a comment by a user who read the question more carefully than I did the first time, the value must be first written to memory, then you load an address value of the memory where that value is stored, and then you jump to it.

    You may want to explore more how JIT compilers work, as they use a lot of code modification (to be clear, they do not modify the code generator, but they do a lot of live patching of the generated code during the execution).