home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1991 / number3 / l4.asm < prev    next >
Assembly Source File  |  1991-05-28  |  4KB  |  91 lines

  1. ; Assembly subroutine for Listing 2. Scans through Buffer, of
  2. ; length BufferLength, counting words and updating WordCount as
  3. ; appropriate, using a look-up table-based approach. BufferLength
  4. ; must be > 0. *CharFlag and *WordCount should equal 0 on the
  5. ; first call. Tested with TASM 2.0.
  6. ; C near-callable as:
  7. ;       void ScanBuffer(char *Buffer, unsigned int BufferLength,
  8. ;               char *CharFlag, unsigned long *WordCount);
  9.  
  10. parms   struc
  11.         dw      2 dup(?)        ;pushed return address & BP
  12. Buffer  dw      ?               ;buffer to scan
  13. BufferLength dw ?               ;length of buffer to scan
  14. CharFlag dw     ?               ;pointer to flag for state of last
  15.                                 ; char processed on entry (0 on
  16.                                 ; initial call). Updated on exit
  17. WordCount dw    ?               ;pointer to 32-bit count of words
  18.                                 ; found (0 on initial call)
  19. parms   ends
  20.  
  21.         .model  small
  22.         .data
  23. ; Table of char/not statuses for byte values 0-255 (128-255 are
  24. ; duplicates of 0-127 to effectively mask off bit 7, which some
  25. ; word processors set as an internal flag).
  26. CharStatusTable label   byte
  27.         REPT    2
  28.         db      39 dup(0)
  29.         db      1               ;apostrophe
  30.         db      8 dup(0)
  31.         db      10 dup(1)       ;0-9
  32.         db      7 dup(0)
  33.         db      26 dup(1)       ;A-Z
  34.         db      6 dup(0)
  35.         db      26 dup(1)       ;a-z
  36.         db      5 dup(0)
  37.         ENDM
  38.  
  39.         .code
  40.         public  _ScanBuffer
  41. _ScanBuffer     proc    near
  42.         push    bp              ;preserve caller's stack frame
  43.         mov     bp,sp           ;set up local stack frame
  44.         push    si              ;preserve caller's register vars
  45.         push    di
  46.  
  47.         mov     si,[bp+Buffer]  ;point to buffer to scan
  48.         mov     bx,[bp+WordCount]
  49.         mov     di,[bx]         ;get current 32-bit word count
  50.         mov     dx,[bx+2]
  51.         mov     bx,[bp+CharFlag]
  52.         mov     al,[bx]         ;get current CharFlag
  53.         mov     cx,[bp+BufferLength] ;get # of bytes to scan
  54.         mov     bx,offset CharStatusTable
  55. ScanLoop:
  56.         and     al,al           ;ZF=0 if last byte was a char,
  57.                                 ; ZF=1 if not
  58.         lodsb                   ;get the next byte
  59.                                 ;***doesn't change flags***
  60.         xlat                    ;look up its char/not status
  61.                                 ;***doesn't change flags***
  62.         jz      ScanLoopBottom  ;don't count a word if last byte was
  63.                                 ; not a character
  64.         and     al,al           ;last byte was a character; is the
  65.                                 ; current byte a character?
  66.         jz      CountWord       ;no, so count a word
  67. ScanLoopBottom:
  68.         dec     cx              ;count down buffer length
  69.         jnz     ScanLoop
  70. Done:
  71.         mov     si,[bp+CharFlag]
  72.         mov     [si],al         ;set new CharFlag
  73.         mov     bx,[bp+WordCount]
  74.         mov     [bx],di         ;set new word count
  75.         mov     [bx+2],dx
  76.  
  77.         pop     di              ;restore caller's register vars
  78.         pop     si
  79.         pop     bp              ;restore caller's stack frame
  80.         ret
  81.  
  82.         align   2
  83. CountWord:
  84.         add     di,1            ;increment the word count
  85.         adc     dx,0
  86.         dec     cx              ;count down buffer length
  87.         jnz     ScanLoop
  88.         jmp     Done
  89. _ScanBuffer     endp
  90.         end
  91.