Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site ncr-sd.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!lll-crg!ucdavis!ucbvax!decvax!ittatc!dcdwest!sdcsvax!ncr-sd!laman From: laman@ncr-sd.UUCP (Mike Laman) Newsgroups: net.unix-wizards Subject: Re: Weird code Message-ID: <307@ncr-sd.UUCP> Date: Thu, 17-Oct-85 12:27:02 EDT Article-I.D.: ncr-sd.307 Posted: Thu Oct 17 12:27:02 1985 Date-Received: Sun, 20-Oct-85 06:30:55 EDT References: <468@isucs1.UUCP> Reply-To: laman@ncr-sd.UUCP (Mike Laman) Organization: NCR Corporation, Torrey Pines Lines: 39 In article <468@isucs1.UUCP> justice@isucs1.UUCP writes: > >Ok you wizards out there, why does this program do what it does? >Here are a couple of hints: > 1) It only works on Vaxes : >main() : > int func(); > > fix((int *) func); : >fix(f) >int *f; >{ > *f &= ~0xc00; >} : Someone else will probably answer this before me -- we always seem to lag behind on the news. I apologize if someone else has answered it "already". The routine "fix" is modifying a mask that tells the "calls" (and friends) instruction which registers (if any) to save ON A VAX 11/780 (and friends). The mask is kept in the "first word" of the function which is where "func" points. "Fix" is changing the mask so registers 11 and 10 will NOT be saved/restored across function calls. On the VAX the UNIX C compiler allocates register 11 for the first register variable, then decreasing number in order of allocation of registers. This means the first two register variables (if the compiler see fit to use them as registers -- that's another story) will get overwritten in the calling routine ("main") every time it calls the routine that was "fixed", namely "func". In other words "func" is modifying "main"'s register variables. And I thought all that assembly I played with a year ago would be just a useless effort in futility :-). Mike Laman, NCR Rancho Bernardo UUCP: {ucbvax,philabs,sdcsla}!sdcsvax!ncr-sd!laman