home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 181.img / TASM-101.ZIP / COUNT.ASM < prev    next >
Assembly Source File  |  1988-10-31  |  2KB  |  50 lines

  1. ; Small model C-callable assembler function to count the number    of
  2. ; lines and characters in a zero-terminated string.
  3. ;
  4. ; Function prototype:
  5. ;       extern unsigned int LineCount(char * near StringToCount,
  6. ;              unsigned int near * CharacterCountPtr);
  7. ; Input:
  8. ;       char near * StringToCount: pointer to the string on which a
  9. ;       line count is to be performed
  10. ;
  11. ;       unsigned int near * CharacterCountPtr: pointer to the
  12. ;                int variable in which the character count is to be stored
  13. ;
  14. NEWLINE  EQU     0ah                ;the linefeed character is C's
  15.                                     ; newline character
  16.          DOSSEG
  17.          .MODEL  SMALL
  18.          .CODE
  19.          PUBLIC  _LineCount
  20. _LineCount       PROC
  21.          push    bp 
  22.          mov     bp,sp
  23.          push    si                 ;preserve calling program's register
  24.                                     ; variable, if any
  25.          mov     si,[bp+4]          ;point SI to the string
  26.          sub     cx,cx              ;set character count to 0
  27.          mov     dx,cx              ;set line count to 0 
  28. LineCountLoop:
  29.          lodsb                      ;get the next character
  30.          and     al,al              ;is it null, to end the string?
  31.          jz      EndLineCount       ;yes, we're done
  32.          inc     cx                 ;no, count another character
  33.          cmp     al,NEWLINE         ;is it a newline?
  34.          jnz     LineCountLoop      ;no, check the next character
  35.          inc     dx                 ;yes, count another line
  36.          jmp     LineCountLoop
  37. EndLineCount:
  38.          inc     dx                 ;count the line that ends with the
  39.                                     ; null character
  40.          mov     bx,[bp+6]          ;point to the location at which to
  41.                                     ; return the character count
  42.          mov     [bx],cx            ;set the character count variable
  43.          mov     ax,dx              ;return line count as function value
  44.          pop     si                 ;restore calling program's register
  45.                                     ; variable, if any
  46.          pop     bp
  47.          ret
  48. _LineCount       ENDP
  49.          END
  50.