Definition of "self modifying code"?
lardom
Posts: 1,659
in Propeller 1
I'm going through a PASM object line by line with a pen and paper to help me follow what's happening at the bit level. I got stuck on the movd instruction. The manual says it's helpful with 'self modifying code'.
I would like to know what self modifying code is.
I would like to know what self modifying code is.
Comments
They all uses the lower 9bits in the value you feed it, movs is just plane copy w/ mask, movd and movi (the later not used often) are shifted-up/inserted to correct slot.
They are useful when you need to reset a loop source/destination pointer, as this is the only way a P1 can do it as it don't have indirect addressing.
The actual +1 or is done with add label, #1 or
add label, _9bit 'the label name of 1<<9 value.
Suppose you had the following instruction:
When compiled, this will jump to the register 73 (as per your example). Now, suppose you wanted to jump to the next instruction instead. You could do this by calling
This would update the s-field of the register located at :j_1 to 74. Now, when the DJNZ executes, it will jump to register 74 instead of 73. It will continue to do this until you either use MOVS to modify it or reload the cog.
That's showing that the first 6 bits are the instruction value, the next 4 are the "effects" field (zero, carry, result, and immediate/indirect. The next 4 are the conditionals flags, then the dest and source registers are 9 bits each. MOVI sets the INSTR bits, MOVD sets the DEST bits, and MOVS sets the SRC bits.
In a COG, the instructions being executed are just 32 bit values stored in memory. You can do things like this: This is a contrived example, but you can do really useful stuff with self modifying code. Lots of things use it to change the addresses of variables being worked on because you can't do indirect addressing with values in a COG, but you can just modify the code as it runs.
Finding "0-0" as the source or destination field of a PASM instruction probably means that instruction is gonna get modified.
Yes, that is the preferred method to make the reader aware that this part of the instruction is being modified by another instruction(s) elsewhere in the code.
Be aware that when you use movs, movd, movi, that there must be an instruction between the instruction and the instruction being modified due to pipelining.
If I don't understand a concept well enough to explain it to myself in plain English then I don't truly understand it.
I am going to read the PDF manual from beginning to end starting today and I will refer back to this thread after I have done so.
It's RAM, so there is nothing stopping you from changing those numbers on the fly (but not just not before it's executed due to pipeline)
for example, If you have a lookup table that a loop x8 spits out data.
His example below makes it perfectly clear.