Path: utzoo!utgpu!water!watmath!clyde!ima!cfisun!lakart!dg From: dg@lakart.UUCP (David Goodenough) Newsgroups: comp.lang.c Subject: Re: Self-modifying code Message-ID: <185@lakart.UUCP> Date: 12 Jul 88 17:11:16 GMT References: <225800044@uxe.cso.uiuc.edu> Organization: Lake - The systems people Lines: 50 From article <225800044@uxe.cso.uiuc.edu>, by mcdonald@uxe.cso.uiuc.edu: > >> [discussion of execute-only code segments] >> > Besides, I thought that self-modifying code was (a) extremely difficult >> >to write, and (b) considered poor programming practice. >> >> Yes, it is and it is. >> > Yes, it is and yes it is, but only by those who don't need it. There > are good uses for self-modifying code: I have used it recently in > two places (one on an IBMPC and the second on a PDP-11. These are MY OPINIONS ONLY - you are free to agree and disagree and flame as you see fit. I have used S-M-C only once when doing a section of code that handled single stepping. The problem W/ the Z80 (comp.lang.c ??????) is that it has conditional jumps, calls AND returns. Now I go and fetch an instruction out of the code portion (i.e. where my PC is pointing to). It's 0xc2. Aha, I have a conditional instruction. Now to figure out whether the condition is met I have two choices: 1. Decode the bits that determine which flag is being looked at, and whether the flag should be set or reset. Get the flags into some register where I can use them. Mask out the flag in question. Do a condional jump on the result of the mask and whether the flag shold be set or clear. 2. Turn the instruction into a conditional jump (and with a mask then or with a mask - turns any condional (except the relative jumps) into a conditional jump). Drop the condional jump into a hole - get the flags and do the jump. If someone wants to see the code that I produced for both of the above I can e-mail. I ask you to take it on faith that 1 was about 40-50 lines, whereas 2 was 6 lines. Also BECAUSE I COMMENTED, it was possible to figure out what was going on. My mark of good commenting is code that can be read a year later and still understood. I agree that this is not for the faint at heart, but it was faster, and to my mind easier to understand. Note also that in this application speed was a premium: when I'm single stepping 1000 instructions I want things to happen PDQ as I would like to see the program appear to run as fast as possible. But then I'm a little insane, because who in their right mind writes a single step utility for a dead micro like the Z80 :-) Like everything it has it's place: and is not to be misused. Misuse of S-M-C *_IS_* a sin (well I think so), but where it is justified I will use it. -- dg@lakart.UUCP - David Goodenough +---+ | +-+-+ ....... !harvard!cca!lakart!dg +-+-+ | +---+