home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / mac / programm / 22256 < prev    next >
Encoding:
Text File  |  1993-01-27  |  3.8 KB  |  89 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!pageworks.com!world!eff!sol.ctr.columbia.edu!spool.mu.edu!agate!apple!mumbo.apple.com!gallant.apple.com!seuss.apple.com!user
  3. From: absurd@apple.apple.com (Tim Dierks, software saboteur)
  4. Subject: Re: help calling assembly from C
  5. Sender: news@gallant.apple.com
  6. Message-ID: <absurd-260193143226@seuss.apple.com>
  7. Date: Tue, 26 Jan 1993 22:57:21 GMT
  8. Distribution: usa
  9. References: <2B6198C1.22487@news.service.uci.edu> <keith-240193173019@kip-50.taligent.com>
  10. Organization: MacDTS Marauders
  11. Followup-To: comp.sys.mac.programmer
  12. Lines: 75
  13.  
  14. In article <keith-240193173019@kip-50.taligent.com>, keith@taligent.com
  15. (Keith Rollin) wrote:
  16. > In article <2B6198C1.22487@news.service.uci.edu>, eapg070@orion.oac.uci.edu
  17. > (Caroll Elke Pohl) wrote:
  18. > > 
  19. > > i wonder if someone out there can assist me with a small problem.
  20. > > i'm using MPW (3.2) and want to call an assembly language subroutine
  21. > > i wrote from a C program _however_ i want to pass the parameters in
  22. > > A0 and A1 instead of using the stack (much like certain memory
  23. > > manager traps). how can i get the C compiler to recognize this fact?
  24. > You came really close to answering your own question. You know that there
  25. > are Mac OS functions that work like this, so why don't you declare your
  26. > function the same way they are? For instance:
  27. > #pragma parameter DisposePtr(__A0)
  28. > pascal void DisposePtr(Ptr p); 
  29. > This is an example of a function that simply takes a parameter in A0.
  30. > Here's a more complex example:
  31. > #pragma parameter __D0 HoldMemory(__A0,__A1)
  32. > pascal OSErr HoldMemory(void *address,unsigned long count); 
  33. > HoldMemory takes the first parameter ("address") in A0, and the second
  34. > parameter ("count") in A1. It returns its result in D0 (which will be
  35. > treated like a short, because the function is declared to return an OSErr).
  36. > -----
  37. > Keith Rollin
  38. > Phantom Programmer
  39. > Taligent, Inc.
  40.  
  41. Unfortunately, MPW doesn't currently work this way (please correct me
  42. if I'm wrong).  While it does use the #pragma register syntax to specify
  43. register passing, I believe it only works for hex inlined functions;
  44. i.e., it's only useful for specifying Toolbox calls or other code
  45. that doesn't require linking, because you can't specify symbols.
  46. This means that if your assembly routine is short (or even if it's not),
  47. you can assemble it, convert it to a bunch of hex constants, and
  48. define it in the same way MPW defines inline code:
  49.  
  50. #pragma parameter __D0 MyWackyFunction(__A0)
  51. long MyWackyFunction(char *data) = {0xDEAD, 0xFACE, 0xBABE};
  52.  
  53. or whatever your hex constants are.  This should work as long as your
  54. assembly routine doesn't call other routines or refer to global
  55. variables; i.e., any routine which is entirely self-contained.
  56. Also, your function needs to take its arguments in particular
  57. registers: #pragma parameter can only pass arguments or get
  58. results in the "scratch" registers: D0, D1, D2, A0, and A1.
  59.  
  60. This approach obviously isn't for all code.  You've got two alternatives:
  61. the best one is just to write a little routine in assembly which pulls
  62. the arguments for the function off of the stack and puts them into
  63. registers, then calls the function, or convert your function so it
  64. takes its parameters on the stack.  An alternative is to use a hex
  65. inlined function which takes its destination in a register and JSRs
  66. to it:
  67.  
  68. #pragma parameter __D0 CallMyWackyFunction(__A0,__A1)
  69. long CallMyWackyFunction(char *data,void *myWackyFunctionPtr) = 0x4E91;
  70.  
  71. (0x4E91 is the hex opcode for a JSR (A1)).  You can call this like so:
  72.  
  73. result = CallMyWackyFunction(data,MyWackyFunction);
  74.  
  75. By passing a pointer to the routine you wish to call, this avoids the
  76. problem that the hex inline can't call the function itself because
  77. it can't contain symbols that need to be linked.
  78.  
  79. Hope all this helps;
  80. Tim Dierks
  81. MacDTS, but I speak for myself
  82.