Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!ames!lll-winken!sun-barr!newstop!sun!stpeter.Eng.Sun.COM!cmcmanis From: cmcmanis@stpeter.Eng.Sun.COM (Chuck McManis) Newsgroups: comp.sys.amiga.tech Subject: Re: Whats wrong with self Modifying Code? Keywords: religion, gurus, whats up? Message-ID: <138523@sun.Eng.Sun.COM> Date: 7 Jul 90 05:18:54 GMT References: <1990Jul6.201328.24660@csmil.umich.edu> <1990Jul6.201743.24777@csmil.umich.edu> Sender: news@sun.Eng.Sun.COM Organization: Sun Microsystems, Mt. View, Ca. Lines: 55 There isn't anything "wrong" with it per se as long as the system supports it. When asked in context to the Amiga there are a couple of issues : Issue : How do you get reliable self modifying code execution when there is an instruction cache present ? Let's take for example the code that goes something like : mov #JumpTable, D0 ; Load D0 with address of table add D1, D0 ; Calculate offset mov D1, #JumpTarget ; Store in in the Jump instruction .dw 4efch ; opcode for a jump immediate JumpTarget: .ds 4 ; 4 byte jump address JumpTable: ; Table of addresses. .dl function1 .dl function2 .dl function3 In this example, We allow the parameter passed in D1 to be used as an offset into a jumptable. The final jump instruction is self modified, and the routine branches appropriately. Now on an instruction cache machine, all of the instructions get fetched into the cache. When the 'mov D1,#JumpTarget' instruction happens, while it changes the version in memory, the cache is _not_ changed. Poof! The code breaks on 68020 and 68030 Amigas with the instruction cache turned on. Generally the 68K family is flexible enough in its addressing modes that you can accomplish what you would have done one way, another "legal" way that doesn't involve self modifying code. This allows your program to continue to function on high end Amigas. Issue : What do you do when a version of LoadSeg makes the code hunk of your program "execute-only" on the MMU? Using the same example above, when an MMU is present it is possible (and probable) that at sometime in the future the MMU will "protect" your code from getting stomped on by making the memory it is running in "execute/read" only. When that is the case the "mov D1,#JumpTarget" instruction generates a CPU exception. This exception will be treated by LoadSeg as either a runaway process or some other valid reason to shut down your task. Suddenly you are hosed again. So the bottom line isn't that it is morally wrong, simply that it won't work on some legitimate Amiga systems and is thus "illegal" in terms of having full Amiga compatibility. If one chooses to use self modifying code in a commercial or even freeware Amiga program they should be sure to spell out clearly on either the package or in a README file that the code will not work reliably on a 68020 or 68030 system. -- --Chuck McManis Sun Microsystems uucp: {anywhere}!sun!cmcmanis BIX: Internet: cmcmanis@Eng.Sun.COM These opinions are my own and no one elses, but you knew that didn't you. "I tell you this parrot is bleeding deceased!"