home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / alt / msdos / programm / 2256 < prev    next >
Encoding:
Text File  |  1992-08-25  |  2.6 KB  |  80 lines

  1. Path: sparky!uunet!decwrl!sdd.hp.com!swrinde!ringer!mlevis
  2. From: mlevis@ringer.cs.utsa.edu (Mike Levis)
  3. Newsgroups: alt.msdos.programmer
  4. Subject: Re: Calling ASM from BC++
  5. Message-ID: <1992Aug25.232741.15553@ringer.cs.utsa.edu>
  6. Date: 25 Aug 92 23:27:41 GMT
  7. References: <1992Aug24.204813.16175@schunix.uucp>
  8. Organization: University of Texas at San Antonio
  9. Lines: 69
  10.  
  11. In article <1992Aug24.204813.16175@schunix.uucp> sonix@schunix.uucp (Duane Morin) writes:
  12. >I'm trying to call an assembly routine from some standard Borland C++ (2.0)
  13. >code.  Here's what I've got (in the C++):
  14. >
  15. >extern foo_bar3(int G, int P);
  16. >.
  17. >.
  18. >.
  19. >foo_bar3(i, x);
  20. >
  21. >And, in the ASM module, I've got:
  22. >
  23. >    PUBLIC    _foo_bar3
  24. >PROC    foo_bar3    FAR
  25. >    ARGS    G:word, P:word
  26. >    .
  27. >    .
  28. >    .
  29. >
  30. >But C++ fails to recognize the foo_bar3 function.  Keeps telling me 
  31. >unrecognized symbol.  As if it doesn't get the connection, because I'm not
  32. >declaring the two int parameters in the actual definition in the ASM file.
  33. >I got the syntax for doing this out of an "Interfacing C and ASM" book, so
  34. >I assume that maybe I need to do somethng else to make this work in C++.
  35.  
  36. C++ uses name mangling.  Under C, the compiler pre-pends an underline
  37. to the the function name, and writes it to the .obj file for the linker.
  38. Under C++, the compiler also appends extra characters to the function
  39. name.  This is name mangling.
  40.  
  41. These extra characters are based on the parameters of the function.
  42. This is why you can overload functions in C++ (as long as the parameter
  43. types differ), where as in C you must specify a unique function name.
  44.  
  45. As the Borland manuals state, the algorithm that Borland C++ uses
  46. to mangle names is undocumented.  In other words, C++ compilers
  47. use different techniques for mangling names.
  48.  
  49. >Any suggestions?  Minor errors in my transcription of the code might be 
  50. >typos of mine just in this post, as I put that code in there from memory
  51. >and may have slipped up (ARG vs ARGS or something).  Any help, however, 
  52. >still appreciated.
  53.  
  54. Declare your assembler function with a C interface rather than a
  55. C++ interface.  Change
  56.  
  57.    extern foo_bar3 (int, int);
  58.  
  59. to
  60.  
  61.    extern "C" {
  62.       foo_bar3 (int, int);
  63.    }
  64.  
  65. There are other ways to call assembler functions from C++.  These
  66. other ways are documented in the Borland Manuals, such as chapter
  67. 5, "Interfacing Turbo Assembler with Borland C++".
  68.  
  69. >Thank you,
  70. > Duane Morin
  71. > Walker Sonix, Inc.
  72. > sonix@schunix.uucp
  73.  
  74.  
  75. -- 
  76. ==== Mike Levis   mlevis@ringer.cs.utsa.edu ====
  77. ::   ftp ftp.cs.widener.edu (147.31.254.132)  ::  .--.
  78. ::   for ``The Simpsons'' information.  Get   :: (OS/2)
  79. :::: ``Simpsons'' FAQL in dir /pub/simpsons ::::  ~--~
  80.