home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_09 / 2n09032a < prev    next >
Text File  |  1991-08-07  |  8KB  |  255 lines

  1. #include "GENERAL.H"
  2. #include "LIB.H"
  3.  
  4. #pragma inline
  5.  
  6. /***********************************************************
  7. *   BOOL chk_key(void) : uses Bios interrupt 16 to check   *
  8. *        if a key has been hit.  Returns FALSE if no key,  *
  9. *        TRUE if a key was hit.                            *
  10. ***********************************************************/
  11.  
  12. BOOL chk_key()
  13. {
  14.    asm mov ah,01h
  15.    asm int 16h
  16.    asm jz  short exit
  17.    asm mov ax,1
  18. exit:
  19.    asm mov ax,0
  20.    return(_AX);
  21. }
  22.  
  23. /***********************************************************
  24. *   void cls(void) : uses the fact that a change of video  *
  25. *        mode clears the screen.  Uses BIOS interrupts to  *
  26. *        read the current mode, then to set the same mode. *
  27. ***********************************************************/
  28.  
  29. void cls(void)
  30. {
  31.    asm mov ah,0fh   /* get current video mode */
  32.    asm int 10h
  33.    asm xor ah,ah    /* set current video mode, clears screen */
  34.    asm int 10h
  35. }
  36.  
  37. /**********************************************************
  38.  * BOOL cmpsnf(char far *b,char *s,l: WORD) : Returns     *
  39.  *   TRUE if 'l' bytes of near string 's' compare exactly *
  40.  *   with far buffer 'b'.  Returns FALSE if no compare,   *
  41.  *   or 'l' = 0.                                          *
  42.  *********************************************************/
  43.  
  44. BOOL cmpsnf(char far *buf1,char *str1, WORD l) {
  45.         asm XOR   AX,AX     /* assume fail */
  46.         asm MOV   CX,l      /* put length in cx */
  47.         asm JCXZ  CMP00     /* if 0 length no compare */
  48.         asm LES   DI,buf1   /* far ptr to buf1 */
  49.         asm MOV   SI,str1   /* near ptr to str1 */
  50.         asm REPZ  CMPSB
  51.         asm JNZ   CMP00     /* did they compare? */
  52.         asm MOV   AX,1      /* yes, TRUE = identical */
  53. CMP00: 
  54.         return(_AX);
  55. }
  56. /***********************************************************
  57. *   void delay_1(void) : Reads system timer tick value     *
  58. *        (updated every 1/18 second), adds 18 to it, and   *
  59. *        waits for a second to pass.                       *
  60. ***********************************************************/
  61.  
  62.  
  63. BYTE far *timer = (BYTE far *) 0x0040006C;
  64. /* where the system keeps the timer tick value */
  65.  
  66. void delay_1(void) {
  67.   BYTE time;
  68.   time = *timer + 18;
  69.   while (time != *timer) {};
  70. }
  71.  
  72. /***********************************************************
  73. *   WORD dosversion(void) : uses Int 21 to get and return  *
  74. *        the current version of DOS                        *
  75. ***********************************************************/
  76.  
  77. WORD dosversion()
  78. {
  79.    _AX = 0x3000;
  80.    asm int 21h
  81.  
  82.    /* move version # to High BYTE for easier */
  83.    /* compares */
  84.    asm xchg ah,al
  85.    return(_AX);
  86. }
  87.  
  88. /****************************************************
  89.  *  WORD fputsh(char *str) : Print a 'C' string     *
  90.  *       using the ROM BIOS.  Replaces "C" fputs    * 
  91.  ***************************************************/
  92.  
  93. WORD fputsh(char *str) {
  94. /* use the rom bios to pring a "C" string (ending in \0) */
  95.  
  96. /* first, find the length of the string we have to print */
  97.  
  98.         asm   MOV     SI,str  /* ptr to str */
  99.         asm   PUSH    SI
  100.         asm   CALL    strlen  /* find length of str */
  101.         asm   POP     SI
  102.         asm   MOV     CX,AX   /* put str length in C */
  103.         asm   JCXZ    DONE    /* if length 0, leave */
  104.         asm   PUSH    AX     
  105.  
  106.               /* save string length to return, not really
  107.               necessary, but easy to do */
  108.  
  109.         asm   MOV     AH,0EH  /* get interrput request */
  110.         asm   XOR     BH,BH   /* point to page 0 */
  111. TTY:    
  112.         asm   LODSB           /* get the character */
  113.         asm   INT     10H     /* print a character */
  114.         asm   LOOP    TTY     /* print them all */
  115.  
  116.         asm   POP AX          /* return string length */
  117. DONE:
  118.         return(_AX);
  119. }
  120.  
  121. /***********************************************************
  122. * void hi-lite(BYTE r,BYTE c,BYTE a, WORD l) : uses several*
  123. *      bios interrupts.  Positions to screen location r,c  *
  124. *      reads 'l' bytes from the screen (one at a time ),   *
  125. *      changes the attribute to 'a' and puts it back on    *
  126. *      the screen with the new attribute.                  *
  127. ***********************************************************/
  128.  
  129. void hi_lite(BYTE row, BYTE col, BYTE attr, WORD length)
  130. {
  131.    asm mov cx,length /* get length of string to hi lite */
  132.    asm jcxz done     /* if zero, nothing to hilite */
  133.    asm xor bh,bh
  134.    asm mov dh,row
  135.    asm mov dl,col    /* point to string on screen */
  136. hi_lt:
  137.    asm mov ah,02h    /* position to byte to hi lite */
  138.    asm int 10h
  139.    asm mov ah,08h    /* read the character on the screen */
  140.    asm int 10h
  141.    asm mov bl,attr   /* change attribute to "attr" */
  142.    asm mov ah,09h      
  143.    asm push cx
  144.    asm mov cx,01h
  145.    asm int 10h /* put it back with passed in attribute */
  146.    asm pop cx
  147.    asm inc dl
  148.    asm loop hi_lt    /* do the whole line */
  149.    asm mov dl,col
  150.    asm mov ah,02h
  151.    asm int 10h  /* reposition to the beginning of line */
  152. done:;
  153. }
  154.  
  155. /***********************************************************
  156. *   WORD get_key(void) : Uses bios interrupt 16h to get a  *
  157. *        key stroke, and return it.                        *
  158. ***********************************************************/
  159.  
  160. WORD get_key()
  161. {
  162.    asm mov ah,0
  163.    asm int 16h
  164.    return(_AX);
  165. }
  166.  
  167. /**********************************************************
  168.  * WORD movesnf(char far *b,char *s) : Copy all chars in  *
  169.  *   near string to far buffer.  Uses strlen to find      *
  170.  *   length.  Returns length of string copied.            *
  171.  *********************************************************/
  172.  
  173. WORD movesnf(char far *buf1,char near *str1) {
  174.      asm MOV   SI,str1  /* near ptr to str1 */
  175.      asm PUSH  SI       /* pass the pointer */
  176.      asm CALL  strlen   /* get length of S */
  177.      asm POP   SI       /* retrive pointer */
  178.      asm MOV   CX,AX    /* put length in CX */
  179.      asm JCXZ  DONE     /* don't copy if length '0' */
  180.      asm INC   CX       /* include the terminating '0' */
  181.      asm PUSH  CX       /* save length */
  182.      asm LES   DI,buf1  /* get far pointer to buffer */
  183.      asm TEST  DI,1     
  184.          /* if on byte boundary, adjust to word */
  185.      asm JZ    MOVE00
  186.      asm MOVSB
  187.      asm DEC   CX
  188. MOVE00: 
  189.      asm SHR   CX,1    
  190.          /* save odd byte count, adjust for word move */
  191.      asm REP   MOVSW     /* move words */
  192.      asm ADC   CX,CX     /* see if last byte */
  193.      asm REP   MOVSB     /* move if yes */
  194.      asm POP   AX        /* restore copied length */
  195. DONE:
  196.      return(_AX);
  197. }
  198.  
  199. /***********************************************************
  200. * void printstr(char *str) : uses int 21 string print      *
  201. *      routine to print passed in string.  The string must *
  202. *      be terminated with a $.                             *
  203. ***********************************************************/
  204.  
  205. void printstr(char *str)
  206. {
  207.    _AX = 0x0900;
  208.    asm mov dx,str      ;/* ptr to STR */
  209.    asm int 21h;
  210. }
  211.  
  212. /***********************************************************
  213. * void set_curs(Byte r, Byte c) : uses bios set cursor
  214. *      routine to position cursor to r,c on screen.        *
  215. ***********************************************************/
  216.  
  217. void set_curs(BYTE row, BYTE col)
  218. {
  219.    asm mov ah,02h
  220.    asm xor bh,bh
  221.    asm mov dh,row
  222.    asm mov dl,col
  223.    asm int 10h
  224. }
  225.  
  226. /***********************************************************
  227. *   WORD strlen (char *str) : Returns non-0 length of      *
  228. *        of string 'str'.                                  *
  229. ***********************************************************/
  230.  
  231. WORD strlen(char *str) {
  232.         asm MOV   DI,DS      /* point ES to DS */
  233.         asm MOV   ES,DI      /* done */
  234.         asm MOV   DI,str     /* ptr to str */
  235.         asm MOV   CX,-1      /* get maximum count */
  236.         asm XOR   AX,AX      /* setup 0 search */
  237.         asm REPNZ SCASB
  238.         asm NOT   CX
  239.         asm DEC   CX         /* len 'str' */
  240.         asm MOV   AX,CX
  241.         return(_AX);
  242. }
  243.  
  244. /****************************************************
  245.  *  void w_tty(char c) : Print a character using    *
  246.  *       using the ROM BIOS Write TTY routine       *
  247.  ****************************************************/
  248.  
  249. void w_tty(char c)
  250. {
  251.    asm mov ah,0eh
  252.    asm mov al,c
  253.    asm int 10h
  254. }
  255.