Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!hoptoad!tim From: tim@hoptoad.uucp (Tim Maroney) Newsgroups: comp.sys.mac.programmer Subject: Re: converting tail patches to head patches... Message-ID: <9099@hoptoad.uucp> Date: 28 Nov 89 06:24:46 GMT References: <63359@tiger.oxy.edu> Reply-To: tim@hoptoad.UUCP (Tim Maroney) Organization: Eclectic Software, San Francisco Lines: 57 In article <63359@tiger.oxy.edu> sonenbli@oxy.edu (Andrew D. Sonenblick) writes: > myPatch() > { > long rtsAddr; > asm{ > Move.L (SP)+,rtsAddr /* save return address in rtsAddr */ > } > doMyStuff(); > asm{ > Move.L rtsAddr,-(SP) > Lea @1,A0 > Move.L (A0),A0 > Jmp (A0) > } > } > > Ok, so, what's the ups with this? (I have tried all sorts of things > similar to this--most just make my mac hang, some crash, none works.) Almost certainly, it's the fact that you moved the stack pointer at the wrong time and by the wrong amount. This is the sort of code you should always step through with a low-level debugger; it'll show you what's happening very fast. So anyway, allocating an automatic variable causes that storage to be pushed on the stack. So, you are saving rtsAddr in rtsAddr and trashing it at the same time, since it's probably the top of the stack. There's no reason to save/restore rtsAddr at all; it's right where you want it to be. You are not taking into account the A6 frame pointer, either. In other words, lotsa problems. Here's my stab at it: myPatch() { doMyStuff(); asm{ unlk a6 Lea @1,A0 ; @1 is a dc.l that has the old trap stashed Move.L (A0),A0 Jmp (A0) } } This should work (but I haven't tested it and I make no guarantees). If it's not clear, take a look at Figure 5 of Inside Mac, Chapter 4 (page IM I-97). And if your have a problem, put a break on the trap you're patching, using MacsBug, then step into it and watch what happens.... Hope this works for you. One more thing -- there are a lot of Mac programmers outside the United States, many of whom read and post to this newsgroup. People shouldn't limit the distribution to usa unless there's some pressing reason to do so, like distributing the Mac source to a DES encrypter or something. -- Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com "Mere opinion without supporting argument is no more than the American Bandstand school of literary evaluation." -- Tom Maddox