Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ginosko!uunet!portal!cup.portal.com!cliffhanger From: cliffhanger@cup.portal.com (Cliff C Heyer) Newsgroups: comp.arch Subject: Re: Self-modifying code Message-ID: <23062@cup.portal.com> Date: 13 Oct 89 23:40:32 GMT References: <1080@mipos3.intel.com> Organization: The Portal System (TM) Lines: 48 >Could any experts out there educate me WHY and HOW does self-modifying code use? The first program I ever wrote was self-modifying. In 1976 with the 8080 for class assignment I wrote a ping pong program with LEDs. The rest of the class wrote code for each direction, but I wrote one "shell" routine and then inserted instructions into it after calculating the "pong" direction. My program was about 1/5 as long as all the others, which was important when you had only 1 or 2K of memory. (The teacher had never heard of doing such a thing, and I had to convince him I deserved an A+) >What the advantage of using self-modifying code that non-self-modifying code >cannot achieve? On DECsystem-10s and 20s I wrote code to check the serial number so that people who stole the code could not run it (easily) on their processor. But if you put the instructions in the code itself, a smart hacker could remove them from the EXE file or insert a jump statement to skip over them. So I generated the check at run time by moving "numbers" to memory and then executing them. This make cracking the security much more difficult. I moved numbers to registers and then bit shifted them to make the instruction, then moved the PC to the register. (God forbid trying this on a VAX!) >Is there any compiler which will generate code that self-modified? If you mean C or BASIC, I'm sure there are some "hacker" compilers that allow you to play, but in general you have no way of knowing address locations that your C compiler will create, or even what instructions your C compiler will generate, so how can you write C statements that will modify something that does not yet exist? You have to use machine code, since then you know what is where. You can do it with any assembler as far as I know, but you have to fuss with the OS and/or compile switches to actually compile and run the code and suppress error messages. >A small and useful example of self-modifying will be very helpful. Read above.