Comments by "zenith parsec" (@zenithparsec) on "What NOT to do: Self Modifying Code - Computerphile" video.
-
A better example of self modifying code using the recipe would be to have some instructions like:
1. Copy this recipe's ingredients and instructions to a blank sheet of paper. Once complete, start following the copied instructions from step 2.
2. Find all numbers in the ingredients this on page which are bigger than 5. For each of those numbers, do the following: add 100 to it, cross out the original number, and write the new number in its place.
3. Find all instructions in this recipe which contain the words "add 100". For each instruction, cross it out.
4. Set oven to 150 degrees C.
5. ... (and the rest)
Notice that this would change the actual instructions you should run.
1. Copy the recipe to a blank sheet of paper == load the program into memory.
2. This instruction modifies the the code (if it said to use 5 eggs, now it will say you need 105 eggs. ) Notice it only modifies the "data" here.
3. This line finds the previous instruction, and removes it. It also finds the current instruction and removes that! This is actual self-modifying code. The purpose of this is in case you want to use this program/recipe again: if you don't remove this code, the next time you go to use it, you will have to use 205 eggs, and then 305 eggs, and eventually you'll notice something weird is happening. ;]
4. And now we start the actual recipe.
5. .. and we run out of eggs.
164