home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 183_01 / c_asm.txt < prev    next >
Text File  |  1985-08-31  |  8KB  |  231 lines

  1.                      Using Assembler with C
  2.  
  3. This  program  is an attempt to demonstrate the use  of  assembly 
  4. language  routines  with C.  The whole idea is to write the  time 
  5. critical routines in ASM for it's obvious superiority (speedwise) 
  6. and call them from your C program as a function (with or  without 
  7. parameters) with a minimum of hassle. This procedure doesn't seem 
  8. to be too well documented, at least not in the books I have or in 
  9. the doc that is sold with Lattice C; the compiler used here.    
  10.  
  11. The  latest  version of Lattice has a new function called  INTDOS 
  12. that generates a DOS function call.  This, of course would be the 
  13. easy  way  to  do  the two things I  did,  namely  CLS  and  CPOS 
  14. (positions  the cursor).  But there will be times we need  to  do 
  15. things *this* way; that is the reason for this study. 
  16.         
  17. I  hope  you find this information useful;  I have tried hard  to 
  18. keep  it error free.  If I have failed in this,  or if  you  have 
  19. suggestions,  comments,  or criticisms,  I'd be very glad to hear 
  20. them.    
  21.  
  22. My current UID is 74176,3051 but I don't expect it to be so after 
  23. the middle of November 84.
  24.  
  25.    ********************************************************** 
  26.         
  27. This  function will display a number of text strings (defined  in 
  28. the header) centered on the screen.      
  29.        
  30. If  you  wish  to develop a working  program  here,  extract  the 
  31. listings  from this text file,  complile the C program,  assemble 
  32. the  two .ASM files to .OBJ files,  and LINK the  three  together 
  33. with C[S].OBJ, and LCS.LIB.
  34.  
  35. Unix  C compilers are usually initiated by issuing the command CC 
  36. (presumably for C Compiler!) and the .C source filespec.  If  you 
  37. are  using  Lattice  (as I am) you don't get to do  it  that  way 
  38. unless  you use a batch file  with that name.  While we're at  it 
  39. (writing  the  .BAT file so we can issue 'CC filespec'  like  the 
  40. 'big  boys') we may as well have CC do something for us!  Here is 
  41. what my .BAT looks like: 
  42.  
  43.  
  44.  
  45. lc1 %1                           ;1st pass.
  46. IF NOT EXIST %1.q GOTO quit      ;An error will keep the .Q file
  47.                                  ; from being generated, and we
  48.                                  ; might as well stop, eh?!
  49. lc2 %1                           ;If it's there, let's do pass 2.
  50. IF NOT EXIST %1.obj GOTO quit    ;Same story, here.
  51.                                  ;This is the confusing part.....
  52. link cs %1 %2 %3 %4,%1,,lcs;     ;%2, %3, etc are for 'extra' .OBJ 
  53.                                  ; modules if we have them; else 
  54.                                  ; they're ignored.
  55. DIR %1.*                         ;We wanna see what we've done, right?!
  56. :quit
  57.  
  58. Just make sure the first filespec you link with is the file  with 
  59. main  () in it.  The next three can be  CLS.OBJ,  CPOS.OBJ,  etc. 
  60. Also,  be sure you DON'T include the .ext's in the filenames. The 
  61. compiler and linker are smart enough to figure out what they are.
  62.  
  63. So, you could issue this command:
  64.  
  65.      CC MYPROG CLS CPOS <ENTER> 
  66.         
  67. where  MYPROG.C  and CLS.OBJ and CPOS.OBJ are all on the  default 
  68. disk.  The  resulting .EXE file (MYPROG.EXE) will have  the  .ASM 
  69. code included,  and our work will be complete. As always, be sure 
  70. you save everything BEFORE you try to run the program. You'd hate 
  71. to start all over, right?
  72.  
  73.  
  74.                  
  75. #include <stdio.h>
  76.  
  77. /* *********************************************************** */
  78. /*   These DEFINES can be changed to change output.            */
  79.  
  80. #define  LINE1   "BootStrap Micro"
  81. #define  LINE2   "R. B. Miller"
  82. #define  LINE3   "P. O. Box 6221"
  83. #define  LINE4   "Stanford, Calif 94305"
  84. #define  LINE5   "415-960-3773"
  85. #define  NLINES   5                 /* Nmbr of lines of text. */
  86.  
  87. /* *********************************************************** */
  88.  
  89.  
  90. #define  LLEN         80         /* Line length */
  91. #define  SCRN_LINES   25         /* Nmbr of lines on scrn. */
  92. #define  COL          0          /* Always position to col 0. */
  93.  
  94.  
  95. static char *data [5] = {
  96.                               LINE1,
  97.                               LINE2,
  98.                               LINE3,
  99.                               LINE4,
  100.                               LINE5  
  101.                                          } ;
  102.  
  103.  
  104.  
  105. MAIN ()                           /* Display and center text */
  106.  
  107.      int i, j, inset ;
  108.      static char *string; 
  109.  
  110.         cls ();                         /* Clear the screen. */
  111.         cpos ((SCRN_LINES - NLINES) / 2 , COL );/* Pos the cursor.   */
  112.  
  113.         for  (i=0 ; i <= NLINES-1 ; i++ )
  114.         {
  115.         
  116.                 string =  data[i];
  117.                 
  118.                 for  ( j = 1 ; j <= (( LLEN - strlen ( string )) / 2 ); j++ )
  119.                     putchar ( ' ' );
  120.                       
  121.                 printf ( "%s\n",string );
  122.                    
  123.         } 
  124.  
  125.   
  126.        cpos ( 22 , COL );                              /* Pos the cursor.  */
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. title   clear screen function
  136. name    cls
  137. page    ,132
  138.  
  139. comment @
  140.  
  141. This is the source code for the 'C' function CLS.  This
  142. function does exactly the same thing as the DOS COMmand
  143. by the same name except this can be LINKed with a 'C'
  144. module and called as a function:
  145.                                           
  146. USAGE:  no variable preparation, no return value,
  147.                                                   
  148.             CLS () ;
  149. @
  150.                                  
  151.                                 
  152.                                 
  153. pgroup  group   prog
  154. prog    segment byte    public  'prog'
  155. public  cls
  156.                                 
  157. assume  cs:pgroup
  158.                                 
  159. cls     proc    near            ;CLS and HOME the cursor.
  160.                                 
  161.                                 
  162.         mov     ax,0600h        ;Scroll the whole screen.
  163.         mov     cx,0            ;from 0,0 to...
  164.         mov     dx,184fh        ;ROW 24, COL 79.
  165.         mov     bh,7            ;'NORMAL' attribute.
  166.         int     10h
  167.                                         
  168.         mov     ah,2            ;SET_CURS function.
  169.         mov     dx,0            ;ROW, COL 0,0.
  170.         mov     bh,0            ;Page 0.
  171.         int     10h
  172.                                 
  173.         ret
  174.                                 
  175.  cls     endp
  176.  prog    ends
  177.          end
  178.  
  179. title        cursor position function
  180. name         cpos
  181. page         ,132 
  182.                                 
  183. comment @
  184.                                  
  185. This is the source code for the 'C' function CPOS ( ROW , COL );
  186. that does exactly what it sounds like it does.. it positions
  187. the cursor at the ROW and COLumn indicated in the arguments.
  188.                                         
  189. Usage:     
  190.                                 
  191. INT ROW , COL ;
  192.                                                 
  193. /* Assign values 0,0  to  24,79  */
  194.                                                 
  195. CPOS ( ROW , COL ) ;
  196. @
  197.                                 
  198.                                 
  199. prog         segment      byte     public    'prog'
  200. public       cpos
  201.                                 
  202. assume       cs:pgroup
  203.                                 
  204. cpos         proc         near
  205.                                 
  206.              push         bp
  207.              mov          bp,sp
  208.              mov          dx,[bp+4]          ;Skip over return address,
  209.                                              ; and saved bp, and 
  210.                                              ; get left parm (ROW).
  211.              mov          ax,[bp+6]          ;Get 2nd parm (COL).
  212.              pop          bp
  213.                                              
  214.              mov          dh,dl              ;ROW in DH,
  215.              mov          dl,al              ;COL in DL.
  216.              mov          bh,0               ;Page 0.
  217.              mov          ah,2               ;Funct 2 'SET_CPOS'.
  218.                                              
  219.              int          10h
  220.              ret
  221.                                 
  222.                                              
  223.  cpos         endp
  224.  prog         ends
  225.               end
  226.  
  227. -------------------------
  228. Downloaded  from  the IBMPC special interest area of  CompuServe, 
  229. uploaded to PCanada by Bob Leigh, PC1022.
  230.