Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!umich!yale!husc6!brutus.cs.uiuc.edu!zaphod.mps.ohio-state.edu!usc!polyslo!rcfische From: rcfische@polyslo.CalPoly.EDU (Raymond C. Fischer) Newsgroups: comp.sys.mac.programmer Subject: Re: Think C non-tail patching (& PACK3) Message-ID: <25d5eb36.31a8@polyslo.CalPoly.EDU> Date: 11 Feb 90 22:46:14 GMT References: <1990Feb8.150728.9760@digibd> Reply-To: rcfische@polyslo.CalPoly.EDU (Raymond C. Fischer) Distribution: na Organization: Cal Poly State University -- San Luis Obispo Lines: 47 In article <1990Feb8.150728.9760@digibd> merlyn@digibd (Brian Westley (Merlyn LeRoy)) writes: >How do I clean up the stack & jmp to a patched routine, instead >of tail-patching it, in Think C 4.0? Something like.. > > >{ >jmpto = NGetTrapAddress(trapnum, ToolTrap); >NSetTrapAddress(trapnum, myroutine, ToolTrap); >} >.... >myroutine(arg1, arg2) >{ > > CallPascal(arg1, arg2, jmpto); /* A nasty tail patch */ >} > >The CallPascal() call should be replaced by asm {} stuff that cleans up >the stack and does a jmp to "jmpto". However, I haven't been able to >figure it out and I'm tired of crashing my machine. Someone must have >invented this wheel already. How's about something like this ... asm { move jmpto,a0 ; get the trap address unlk a6 ; restore a6 and toss stack frame jmp (a0) ; go to trap address } Your trap patch MUST be declared as a pascal routine exactly the same way the trap is defined. C calling conventions and Pascal calling conventions are different and incompatible. In the example above you would use ... pascal void myroutine(arg1, arg2) >On a related note, how do I patch PACK3, which has different arguments >depending on how it's called? Similar to the above problem. Actually, since the above assembly code leaves the parameters on the stack along with the return address, it will work regardless of the number of parameters. Ray Fischer rcfische@polyslo.calpoly.edu