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

  1. ; Large model C-callable assembler function to count the number
  2. ; of lines and characters in a zero-terminated string.
  3. ;
  4. ; Function prototype:
  5. ;       extern unsigned int LineCount(char * far StringToCount,
  6. ;              unsigned int * far CharacterCountPtr);
  7. ;       char far * StringToCount: pointer to the string on which
  8. ;                                 a line count is to be performed
  9. ;
  10. ;       unsigned int far * CharacterCountPtr: pointer to the int variable
  11. ;                                             in which the character count
  12. ;                                             is to be stored
  13. ;
  14. NEWLINE  EQU     0ah                  ;the linefeed character is C's newline
  15.                                       ; character
  16.          DOSSEG
  17.          .MODEL  LARGE
  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.          push    ds                   ;preserve C's standard data seg
  26.          lds     si,[bp+6]            ;point DS:SI to the string
  27.          sub     cx,cx                ;set character count to 0
  28.          mov     dx,cx                ;set line count to 0
  29. LineCountLoop:
  30.          lodsb                        ;get the next character
  31.          and      al,al               ;is it null, to end the string?
  32.          jz       EndLineCount        ;yes, we're done
  33.          inc      cx                  ;no, count another character
  34.          cmp      al,NEWLINE          ;is it a newline?
  35.          jnz      LineCountLoop       ;no, check the next character
  36.          inc      dx                  ;yes, count another line
  37.          jmp      LineCountLoop
  38. EndLineCount:
  39.          inc      dx                  ;count line ending with null character
  40.          les      bx,[bp+10]          ;point ES:BX to the location at
  41.                                       ; which to return character count
  42.          mov      es:[bx],cx          ;set the character count variable
  43.          mov      ax,dx               ;return the line count as
  44.                                       ; the function value
  45.          pop      ds                  ;restore C's standard data seg
  46.          pop      si                  ;restore calling program's
  47.                                       ; register variable, if any
  48.          pop      bp
  49.          ret
  50. _LineCount        ENDP
  51.          END
  52.