home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / mac / programm / 20575 < prev    next >
Encoding:
Internet Message Format  |  1993-01-03  |  6.5 KB

  1. Path: sparky!uunet!olivea!apple!goofy!mumbo.apple.com!gallant.apple.com!seuss.apple.com!user
  2. From: absurd@apple.apple.com (Tim Dierks, software saboteur)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: Need BOOK that explains how to write an INIT
  5. Message-ID: <absurd-030193145416@seuss.apple.com>
  6. Date: 4 Jan 93 00:29:13 GMT
  7. References: <1992Dec21.113758.1944@random.ccs.northeastern.edu>
  8. Sender: news@gallant.apple.com
  9. Followup-To: comp.sys.mac.programmer
  10. Distribution: comp.sys.mac.programmer
  11. Organization: MacDTS Marauders
  12. Lines: 109
  13.  
  14. In article <1992Dec21.113758.1944@random.ccs.northeastern.edu>,
  15. chuck@ccs.northeastern.edu (Chuck Wells) wrote:
  16. > I have quite a few Mac programming books, but none that explain the art of
  17. > INIT writing.  Can anyone recommend such a book?
  18. > Specifically, the init I am currently hoping to write just has to:
  19. >     1) Modify some files when I restart the computer
  20. >     2) Modify some files when I shutdown the computer
  21. > If I can do those things with an INIT, that will keep me happy for now.  Later,
  22. > I'll need to be able to: tell what keys the user is pressing, no matter what
  23. > application he is in, yet still allow the user to get those keypress events
  24. > to the applications.
  25. > Any book recommendations?  A while ago, Tim Dierks posted a long message on
  26. > how to write an INIT, but since I don't know how to patch traps, I was a bit
  27. > confused.  Thanks for any help.
  28.  
  29. Well, as far as I know, there isn't a book specifically on
  30. writing INITs, (although I'm currently toying with the idea of writing
  31. one);
  32. however, I believe a couple of books may have passing examples on doing it.
  33. Here's some info you might find helpful on traps and patching them:
  34.  
  35.    A lot (as in virtually all) of the Mac relies on the toolbox and
  36. operating system, a collection of routines which are used by virtually
  37. all Mac programs.  The code for these routines is shared by all the
  38. applications on a Mac; it is loaded by the system and made available to
  39. the routines.  The problem is how to link up the code loaded by the
  40. system with the code in the application so the application can easily
  41. call the system routines reliably.  Because code on the Mac can be loaded
  42. into any RAM address, and the ROMs on different Macs vary greatly, you
  43. can't just have a single address for each routine which the application
  44. can jump to; you need some kind of flexible mechanism to dispatch
  45. the application's requests to the appropriate system code.  This
  46. mechanism is the a-trap dispatcher.
  47.    The 68000 series of processors use a 16 bit instruction set; any
  48. processor instruction can be specified in one 16-bit word (which may
  49. be followed by more data specifying the operands).  For convenience
  50. and simplicity, information about the instruction is encoded into the
  51. bit pattern of the hex value that specifies it.  For example, any
  52. 68000 instruction which begins with the hex digit $D is some variety
  53. of an ADD instruction.  Some of the binary patterns were reserved
  54. for later expansion; for example, any instruction intended for the
  55. floating-point co-processor bgins with the hex digit $F.  Similarly,
  56. any instruction beginning with the hex digit $A is unassigned; if
  57. the processor encounters one, it stops what it is doing and calls
  58. a routine to deal with this problem; as far as it is concerned, you've
  59. just executed an illegal instruction.  On the Mac, the routine which
  60. deals with this problem is the trap dispatcher; it looks at the
  61. instruction which caused the problem and matches it up with a routine
  62. in the system software.  It then jumps to this routine, which
  63. returns to the application after it has completed its work.
  64. Because these instructions begin with the hex digit $A and because
  65. they act like a trap door, interrupting the program's flow and taking
  66. it elsewhere, we call these instructions A-traps.
  67.     Let's look at a concrete example.  Let's say I'm writing a program
  68. and I want to call PaintRect; I insert the following line into my
  69. application:
  70.  
  71.     PaintRect(&myRect);
  72.  
  73.     This results in the compiler placing the following assembly code
  74. in my program:
  75.  
  76.     Assembly code                                Machine code
  77.  
  78.     PEA        -$0008(A6)                        486E FFF8
  79.     _PaintRect                                   A8A2
  80.  
  81. (Note the hex machine code translation on the left).  First, we put the
  82. address of the rectangle I want to paint on the stack, then we
  83. have one of these mysterious A-traps.  When the processor is bipping
  84. along and hits this A-trap, it is mystified.  Unlike $486E (the PEA
  85. instruction), there isn't any processor instruction specified by $A8A1;
  86. the the processor doesn't know how to handle it in hardware, so it
  87. calls a software routine to deal with it.  This software routine is
  88. the trap dispatcher; it looks at what instruction you're trying to
  89. execute (in this case $A8A1) and looks in a table it has to find
  90. the routine that matches up with this trap.  In this case, the
  91. table holds the address $4082C48C, which is where the code for
  92. _PaintRect sits in the ROM on my machine.  The trap dispatcher then
  93. cleans up the stack and jumps to this routine; the end result is
  94. that to the system software, it looks like the application called
  95. it directly in the same way any routine linked in with the application
  96. would be.  When the system code is done, it returns to the code that
  97. called the A-trap, in this case my application; to the application,
  98. it all looks just like as if it had called a routine linked in in a
  99. more traditional manner.
  100.     OK, that covers the basic facts behind the dispatcher.  You can
  101. patch traps by replacing the address of the system supplied routine
  102. with the address of a routine of your own; the routines
  103. GetTrapAddress and SetTrapAddress manipulate the trap table.
  104. Basically, you can then intercept the calls the application makes;
  105. rather than the system software getting called when the application
  106. uses an a-trap, you will get control, allowing you to do your own
  107. nefarious acts.  It's your responsibility to pass control on to the
  108. system software when you're done, so you need to use GetTrapAddress
  109. to get the address of the trap before you patch it and remember that
  110. address; when you're done doing your stuff, you can jump to this
  111. address to allow the system code to do what the application is asking.
  112.  
  113. Well, I'm getting tired and I want to go home and play pinball, so
  114. I'm going to leave this there.  I've still got the previous post
  115. I wrote on this, which discusses a specific question and details a
  116. lot of how to pick which trap to patch; mail me if you want a copy.
  117.  
  118. Tim Dierks
  119. MacDTS, but I speak for myself
  120.