Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!spool.mu.edu!uunet!munnari.oz.au!bruce!monu0.cc.monash.edu.au!monu4!ins760z From: ins760z@monu4.cc.monash.edu.au (mr c.r. hames) Newsgroups: comp.sys.amiga.programmer Subject: Re: Self modifying code Message-ID: <1991May29.015044.429@monu0.cc.monash.edu.au> Date: 29 May 91 01:50:44 GMT Article-I.D.: monu0.1991May29.015044.429 References: <5005@orbit.cts.com> <1991May28.120630.10150@cs.umu.se> Sender: news@monu0.cc.monash.edu.au (Usenet system) Organization: Monash University, Clayton, Vic 3168, Australia Lines: 64 In article <1991May28.120630.10150@cs.umu.se> dvljrt@cs.umu.se (Joakim Rosqvist) writes: >Ok, I have heard before that you should not use self modifying code. >Can anybody give a good reason for this, besides it being bad programming >pratice. The main reason is that if you use self modifying code it might not work on a 68010 or 68020 or 68030 or 68040 or future processor. Because for one reason or another the instructions that you are modifying could already be in the pc-cache of the processor and hence the change will not be made in the pc-cache and your code will stuff up. >I just got the RKM manuals and they only stated "for compatibilty with >010/020/030 etc. don't use selfmodifying code" >Could it be that data written to an address that is also in cachememory >will update the memory, but not the cache? Yep. > >Consider for instance that I would like to write a fast completely >unwinded linedrawing routine and wrote code for drawing one pixel. >Then when the program start, I would allocate memory for 320 such routines >and putting 320 copies of my routine there. Would this be considered as >selfmodifying code, and therefore forbidden? > That's self modifing code. Sure it sounds safe enough but take a look at this little baby that wouldn't work on my A3000 (sometimes). lea Jump+2,a0 lea Routine,a1 move.l a1,(a0) Jump: jmp $12345678 Routine: .......... Now the above routine seems fine but with burst mode of the instruction cache on and the correct alignment the jmp instruction can be prefetched before the move.l a1,(a0) and hence a jump to $12345678 is made instead of to Routine. A similar example to this was given to me last week. In 2.0 there is system routines to clear the cache, but with 1.2/1.3 look at the code of SetCPU V1.6 to make sure you do not write one of the many programs which do not work on some systems. >If so, compare it to when the OS is loading a program that contains >non-PC relative references to itself which will have to be rewritten >depending on the address the code was loaded to. > Although I beleive 1.2/1.3 did not, 2.0 flushes the cache. > > >/$DR.HEX$ > >-------------------------------------------- >Email to: dvljrt@cs.umu.se >2 Windows on the screen? Oh no, not me. I just Logout & Go. - Chris Hames Person to blame for DirWork(1.3 RSN), VMK, FSDirs, etc And soon for PC-Task? ins760z@monu4.cc.monash.edu.au