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

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!world!siegel
  3. From: siegel@world.std.com (Rich Siegel)
  4. Subject: Re: Think C & #pragma parameter (was: help calling assembly from C)
  5. Message-ID: <C1E4Hz.5BA@world.std.com>
  6. Organization: GCC Technologies
  7. References: <2B6198C1.22487@news.service.uci.edu> <keith-240193173019@kip-50.taligent.com> <1jvlnrINNst4@crcnis1.unl.edu>
  8. Distribution: usa
  9. Date: Mon, 25 Jan 1993 03:32:23 GMT
  10. Lines: 70
  11.  
  12. In article <1jvlnrINNst4@crcnis1.unl.edu> mgleason@cse.unl.edu (Mike Gleason) writes:
  13. >
  14. >Well, I found out the hard way awhile back that this doesn't work like one
  15. >would expect in Think C 5.  Think C's #pragma parameter only works on "sub-
  16. >sequent inline function definitions" (p. 194 of the manual).  So in Think C,
  17. >you can't really use this for your own functions unless you bother with crap
  18. >like "void foo(void) = { 0x4e71, 0x4e71, etc... };"  I wouldn't mind if Think
  19. >C required the whole function to be in gazzembly, but accepting only raw hex
  20. >is unacceptable.
  21.  
  22. So big deal. What's assembly language without a little work? :-)
  23.  
  24. Here's a way to do it - it's not necessarily as nice as having a #pragma
  25. to do it for you, but it does work, and you could probably cons up some
  26. macros to help...
  27.  
  28. Declare your function as a void function, with no arguments, and at
  29. the beginning, you can have a bit of code which copies the register
  30. arguments into named local variables. You don't have to, though, if
  31. you're writing the entire function in assembly language:
  32.  
  33. void func(void)
  34. {
  35.     char *str;
  36.     short len;
  37.  
  38.     asm
  39.     {
  40.         move.w    d0, len
  41.         move.l    a0, str
  42.     }
  43.  
  44.     // ...and the rest of your function.
  45.  
  46.     // If you need to return a result to a C caller, return it in d0.
  47. }
  48.  
  49. To call this function, just have a bit of inline assembly that moves the
  50. arguments into registers and calls the function:
  51.  
  52.     asm
  53.     {
  54.         lea    char_buf, a0
  55.         move.w    buf_len, d0
  56.         jsr    func
  57.     }
  58.  
  59. (I'm coming into this thread late, and so you may have already considered
  60. this; however:)
  61.  
  62. You may wish to revisit your code and figure out whether it's worth
  63. taking the time to optimize your parameter passing; if it's so
  64. time-critical that you're worrying about the number of cycles it takes
  65. to push and pop arguments, then you may well be better off moving the
  66. function(s) you're going to call inline with the rest of your code.
  67. Besides, for a normal routine, the passing of arguments really gets
  68. lost in the noise; if it doesn't, then you -definitely- should move
  69. the function inline.
  70.  
  71. #pragma parameter is really a hack (which originated with MPW C) to
  72. provide a way to call register-based traps without requiring an
  73. external glue library; trying to use it for anything else is extending
  74. it somewhat beyond its limits...
  75.  
  76. R.
  77. -- 
  78. -----------------------------------------------------------------------
  79. Rich Siegel                              Internet: siegel@world.std.com
  80. Senior Software Engineer
  81. GCC Technologies
  82.