From: Ray.Arachelian@f204.n2603.z1.ieee.org (Ray Arachelian)
Newsgroups: comp.sys.mac.programmer
Subject: Patching CODE resources
Date: 29 Aug 92 17:43:04 GMT
Organization: FidoNet node 1:2603/204 - Not Even Odd, Forest Hills NY

In regards to my previous post about ZBasic speed problems, I've narrowed
down the problem a bit.  Perhaps someone familiar with patching applications
or with the inner workings of _WaitNextEvent could help with this?

Let me assure you that I do own and have Think C before anyone suggests that
I dump my Zbasic compiler and switch to C. :-)  (Actually this would not
be a bad idea.)

I've experimented with Zbasic and found it to slow down tremendously under
system 7, however it would work at normal fast speed under older systems.

I didn't know what the problem was until I built a bench mark progam which
tested a lot of things including printing and event handling.

The major difference was indeed event handling.  It seems ZBasic produces
code that calls _SystemTask and _GetNextEvent.  I've tried to build a patch
to the code procedures that call these routines to force ZBasic to call
_WaitNextEvent instead however the program crashes...

My question: Does anyone know how to append code at the end of a resource
so it does not interfere with the rest of the program?  (I take it that
either something is jumping in the middle of my patch, or the segment
loader can't deal with extra code being appended to the end of a segment.)

My patch looks something like this:

Zbasic code resource5, Anon procedure 291.

  MOVEM.L D1/D2/A0/A1,-(A7)
  SUBQ.L #$2,A7                 // Make space on stack for return val?
  MOVE.W #$0fff,-(A7)           // Event Mask
  PEA    -$0786(a5)
  _GetNextEvent
  TST.B  (A7)+                  // Check return value on stack.
  /\/\/\/\/\/\/\/\/\/\/\/\/\

There is also another procedure that calls GNE with a keyboard mask, and
it is only used to get keypressess.   It is similar to the one above.

There are also pieces of code which check the timer and call _SystemTask.

I've tried several patches to the code, and they look mostly like this:

  MOVEM.L D1/D2/A0/A1, -(A7) // same
  SUBQ.L #$2,A7
  MOVE.W #$0fff,-(A7)       // same
xxx:
  JMP    code5+$780C       // jump to my code
  BRA.S  xxx
yyy:
  TST.B (A7)+

The JMP replaces the PEA instruction above, the BRA branches to the JMP
incase of any other code resource jumping in the middle of my patch and
possibly causing a crash.

At code5+$780c my patch looks like this:

  MOVE.L #$00000007,-(A7)  // Push sleep value
  CLR.L -(A7)              // Push zero for MouseRgn value
  _WaitNextEvent
  JMP yyyy

I applied this patch to both calls to _GetNextEvent (I duplicated the patch
twice, so I have two return jumps.) I didn't want to use JSR/RTS as they
would place the return values returned by _WaitNextEVent in a differnet
location on the stack.  I also NOP'ed out all calls to _SystemTask, yet
the application crashes.  Sometimes it starts to work for one pass and
then when it restarts, it crashes.  What did I overlook?

Thanks for your patience.

 -- Ray
(P.S. If you could send a response to rarachel@ishara.poly.edu, this would be
preferred since I'd get your reply about a week ahead of the

internet<->fidonet exchange.)


--  
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
 Ray Arachelian - Internet: Ray.Arachelian@f204.n2603.z1.ieee.org
Newsgroups: comp.sys.mac.programmer