home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s196 / 2.img / ASMPROCS.TXT < prev    next >
Encoding:
Text File  |  1991-08-14  |  9.0 KB  |  218 lines

  1. Basm Tasm Mangle and Spaghetti It 
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. (A brief and simplified explanation of assembler interfacing to Turbo C++.)
  9.  
  10. (This article is intended for "REAL" DOS programmers: 
  11.  those who don't eat quiche or drive little plastic cars.)
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. by:  Victor E. Cummings
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. Trademarks contained herein are attributed to their respective owners.
  35.  
  36. Introduction:  
  37.  
  38. This is a frequently asked question:  just when and where should
  39. I use assembler interfaces in my C programs?  With the introduction
  40. of Turbo Assembler and Turbo C++, this question becomes even more
  41. difficult.  The facilities that Tasm offers and the inclusion of
  42. Basm the internal inline assembler make the interface so versatile
  43. and easy that the question is not how but why and where and what
  44. method of many do I use?  
  45.  
  46. This article is for the programmer who wishes to add assembler
  47. interfaces to C programs.  With the introduction of MASM 6.0 it is
  48. increasingly possible to do the reverse with ease.  MASM 6.0 got
  49. a marginal review from PC Magazine, but I'm not sure that I agree. 
  50. It offers loop constructs similar to a higher level language which
  51. work gracefully, and they have many good examples included with the
  52. assembler that you can modify for your own use.  Tasm 2.5 just does
  53. not offer that.  The idea of creating assembler programs with a few
  54. C interface routines is beyond the scope of this article, though
  55. calling C routines from assembler is discussed.  If you would like
  56. to add higher level constructs to Tasm so you have that facility, 
  57. The MS-DOS Developer's Guide by the Waite Group offers a tool kit
  58. which does just that, and may indeed have been where Microsoft got
  59. the idea. 
  60.  
  61. If you are an assembler programmer using C interface routines, it
  62. is doubtful that you have downloaded this article in the first
  63. place, because the scope is too small for you.
  64.  
  65. Wherever possible this article is written at the 7th grade level
  66. so that you may quickly get the information you need without long
  67. winded sentences.
  68.  
  69. The GAZINTAS and the GAZOWTAS:
  70. Definitions:
  71.  
  72. GAZINTA: What GAZINTA here >────┐
  73.                                 │
  74. Also mathematical term    ┌─────┼──────┐
  75. meaning quotient.         │     │      │    ┌────┐
  76. [origin obscure]          │     └>─────>──>─┤    │
  77.                           └────────────┘  ^ └────┘
  78.                                           │
  79. GAZOWTA: What GAZOWTA there ──────────────┘
  80.  
  81. Where ever possible, the GAZINTA and GAZOWTA method of definitions
  82. will be used in the following explanations.  This context is used
  83. to add some humor to a dry subject, but not to insult your intel-
  84. ligence.  The discussion does not get into a lot of detail, but
  85. attempts to help the reader gain the basic concepts quickly.
  86.  
  87.  
  88.  
  89.  
  90. When to use Basm:
  91.  
  92. Basm is the assembler which is internal to the new Turbo C++
  93. compiler.  It has obvious limitations which are documented in the
  94. manual.  Where is Basm.  Basm works silently.  No message is given
  95. by the compiler unless Basm is unsuccessful in compiling your
  96. inline statements.  Simply stated:  if you have existing statements
  97. which include the #pragma inline directives then remove them.  I
  98. have provided an example by Al Stevens which I have converted to
  99. Borland C++ 2.0 code.  In the example, the #pragma inline statement
  100. has been removed.  Compile the example "IBMPC.CPP" by using the
  101. command line:  bcc -c ibmpc.  Then remove the comment preceding
  102. #pragma inline, and the compiler will produce an assembler version 
  103. of the file and call Tasm to assemble it.  The Basm compiler works
  104. more efficiently to produce the code without having to produce
  105. assembler output and then assembling it.  The result is a faster
  106. compile with identical output.  
  107.  
  108. Inline assembly is really used at its best as in this example.  It
  109. should be used when only a few assembler statements are needed and
  110. where time is of the essence as in waiting to write a character
  111. after a video retrace.
  112.  
  113. Inline assembly is useful for this purpose.  Inline assembly allows
  114. more than it really should.  You can even use it to call a C
  115. function.  In a word: don't.  If you need to do something that
  116. complicated, you should consider making an assembler call.  It is
  117. really easy to do with Turbo Assembler 2.5 so why not?  The most
  118. you will want to do is pass a few variables to the inline code and
  119. let it work fast and return.  
  120.  
  121. When would I want to use Tasm with the #pragma inline directive?
  122.  
  123. Don't use it where Basm will work.  If you can remove it, then do. 
  124. Your code will compile more quickly.  If you wish to see the result
  125. of your assembly for debugging purposes then use the -S command
  126. line option supplied with the compiler to get an assembly output
  127. for your file and stop the assembly process.  The IBMPC.CPP file
  128. is a good file to try this with, because it does not include many
  129. of the library routines, and it will give you a simple example to
  130. study to discover how inlines are handled by the compiler.  The
  131. resulting IBMPC.ASM file has been included in this archive.
  132.  
  133. What is a mangled name?
  134.  
  135. Examine the resulting file produced with the -S compiler directive: 
  136. IBMPC.ASM.  The name is descriptive.  If you look at the resulting
  137. label names created by the compiler, you will see that the compiler
  138. has mangled the heck out of them as the name implies.  The way that
  139. the compiler mangles the names is not well documented in the
  140. compiler documentation.  The compiler tells you to create the
  141. assembly output in this manner to study the output.  They include
  142. this as a training measure for the mangled names without having to
  143. add to the mass of their manuals which are already quite bulky. 
  144. If you need to write assembler interfaces which will be used in
  145. object oriented programming style, then you may produce the
  146. function header for the function first in your C++ code.  Then
  147. compile the file using the -S compiler directive.  This will give
  148. you the mangled name to use in your assembly code commented above
  149. by the header that your created.  Then to speed this up you can
  150. just insert your code into the code that was started for you by the
  151. compiler.  Let the compiler do the work for you.  I have provided
  152. an example called FICTION.CPP which contains some headers for some
  153. fictional functions.  When you are designing your application, and
  154. you decide that assembly interfacing would be the way to go for
  155. several of your functions, then you may use this technique.  I
  156. compiled the FICTION.CPP file to get the FICTION.ASM file contained
  157. in this archive.  
  158.  
  159. When I looked at the assembler output that I saw, it really made
  160. me as a REAL DOS programmer mad.  Someone at Borland really decided
  161. to mangle the heck out of my public names!  Well if the compiler
  162. wants mangled names, I might as well let it mangle them for me. 
  163. One thing that I was really happy about:  my fingers get tired
  164. every time I have to type push bp, and the compiler already started
  165. the code which I can snip.  Really clean no?  Do these guys at
  166. Borland ever take a lunch break?  The compiler even commented the
  167. assembly code and added only the bare essentials which I would have
  168. needed to type anyway.  It even inserted my local variable names
  169. in as comments.  If the guys at Borland had taken one less lunch
  170. break, I would have taken me two less milliseconds to set up my
  171. local stack for the assembler call.  The compiler already
  172. calculated the ret instruction for the pascal call and even will
  173. insert the references to the base pointer if you initialize your
  174. variables.  Then all you have to do is make the references to the
  175. base pointer (bp) into EQU statements which you can then reference
  176. by name.  What could be easier.
  177.  
  178. Design Philosophy:
  179.  
  180. When designing an application, and you just know that you are going
  181. to need an assembler call, then just code the header for the
  182. fictional function, initialize all the variables and compile with
  183. the -S compiler option and snip the stub for your function.  An
  184. example that was created in about 40 seconds from the assembler
  185. output is contained in this archive and is called SNIP.ASM.  I
  186. added only a few changes to make a very optimal stub for my
  187. fictional routine.
  188.  
  189. The hard part:
  190.  
  191. The hard part is where do you use assembler interfacing and where
  192. do you not.  If your program is working fairly nicely and at a
  193. speed you are happy with then leave it in C.  But you get into the
  194. coding and you find that there is one spot which is just too
  195. SLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOW then you should resort to
  196. assembler.  In my next article for Real DOS programmers entitled
  197. "Its Got To Be Assembler Here-->"  I'll furnish you with a working
  198. example of where assembler can not be avoided with the equivalent
  199. C++ code that you can use in your programs.
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.   
  213.  
  214.  
  215.  
  216.  
  217. 
  218.