Content deleted Content added
Assessment: Computer science: importance=Mid (assisted) |
|||
Line 129:
Does anyone know if there is a citation for the anecdote of using self-mod code as a copy protection technique on the Apple II? I remember reading about it somewhere 20 years ago when I was 'into' the Apple II in high school (back when "20 megabytes" was considered "really in-humanly humungously big" LOL) [[User:Jimw338|Jimw338]] ([[User talk:Jimw338|talk]]) 21:35, 14 March 2013 (UTC)
== Javascript example is definitely not self-modifying code. ==
I'm a programmer, let me explain:
By assigning a new function to a variable, the variable will point to the new function, not the old function itself was modified.
I have two example, one in Javascript, one another in C/C++:
Javascript:
<syntaxhighlight lang="javascript">
var greet= function () {
alert("hello!");
}
var temp = hello;
greet = function () {
alert("What's 'greet'?");
}
greet(); // display "What's 'greet'?", you may think it is self-modifying, but wait!
temp(); // display "hello!", if it was self-modifying, why it is not "What's 'greet'?"
</syntaxhighlight>
Now, the similar code for C++ 14: Pointer assigning, it works just like javascript above
<syntaxhighlight lang="C++">
#include <iostream>
int main() {
using namespace std;
auto greet = [] {
cout << "hello!\n";
};
auto temp = greet;
greet = [] {
cout << "What's 'greet'?\n";
};
greet();
temp();
return 0;
}
</syntaxhighlight>
It's is modifying value of a pointer which point to a function, so it's normally modifying a variable. So, what is really self-modifying? Self-modifying is modifying the program structure itself in memory (RAM); when a program is opened, it entire mechine code will be load in a memory region in RAM, a self-modifier will modify that region instead of modifying variables.
|