home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter16 / opt2.asm < prev    next >
Assembly Source File  |  1997-06-18  |  5KB  |  109 lines

  1. ;
  2. ;          Opt2         Final optimization word count
  3. ;          Written by   Michael Abrash
  4. ;          Modified by  Willem Clements
  5. ;                       C/ Moncayo 5,  Laurel de la Reina
  6. ;                       18140 La Zubia
  7. ;                       Granada
  8. ;                       Spain
  9. ;                       Tel 34-58-890398
  10. ;                       Fax 34-58-224102
  11. ;
  12. parms          struc
  13.                dw         2 dup(?)
  14. buffer         dw         ?
  15. bufferlength   dw         ?
  16. charflag       dw         ?
  17. wordcount      dw         ?
  18. parms          ends
  19.                .model     small
  20.                .data
  21. charstatustable label byte
  22.                rept       2
  23.                db         39 dup(0)
  24.                db         1
  25.                db         8 dup(0)
  26.                db         10 dup(1)
  27.                db         7 dup(0)
  28.                db         26 dup(1)
  29.                db         6 dup(0)
  30.                db         26 dup(1)
  31.                db         5 dup(0)
  32.                endm
  33.                .code
  34.                public     _ScanBuffer
  35. _ScanBuffer    proc       near
  36.                push       bp
  37.                mov        bp,sp
  38.                push       si
  39.                push       di
  40.                mov        si,[bp+buffer]
  41.                mov        bx,[bp+charflag]
  42.                mov        al,[bx]
  43.                mov        cx,[bp+bufferlength]
  44.                mov        bx,offset charstatustable
  45.                xor        di,di                 ; set wordcount to zero
  46.                shr        cx,1                  ; change count to wordcount
  47.                jc         oddentry              ; odd number of bytes to process
  48.                cmp        al,01h                ; check if last one is char
  49.                jne        scanloop4             ; if not so, search for char
  50.                jmp        scanloop1             ; if so, search for zero
  51. oddentry:      xchg       al,ah                 ; last one in ah
  52.                lodsb                            ; get first byte
  53.                inc        cx
  54.                cmp        ah,01h                ; check if last one was char
  55.                jne        scanloop5             ; if not so, search for char
  56.                jmp        scanloop2             ; if so, search for zero
  57. ;
  58. ;              locate the end of a word
  59. ;
  60. scanloop1:     lodsw                            ; get two chars
  61.                xlat                             ; translate first
  62.                xchg       al,ah                 ; first in ah
  63. scanloop2:     xlat                             ; translate second
  64.                dec        cx                    ; count down
  65.                jz         done1                 ; no more bytes left
  66.                cmp        ax,0101h              ; check if two chars
  67.                je         scanloop1             ; go for next two bytes
  68.                inc        di                    ; increase wordcount
  69.                cmp        al,01h                ; check if new word started
  70.                je         scanloop1             ; locate end of word
  71. ;
  72. ;              locate the begin of a word
  73. ;
  74. scanloop4:     lodsw                            ; get two chars
  75.                xlat                             ; translate first
  76.                xchg       al,ah                 ; first in ah
  77. scanloop5:     xlat                             ; translate second
  78.                dec        cx                    ; count down
  79.                jz         done2                 ; no more bytes left
  80.                cmp        ax,0                  ; check if word started
  81.                je         scanloop4             ; if not, locate begin
  82.                cmp        al,01h                ; check one-letter word
  83.                je         scanloop1             ; if not, locate end of word
  84.                inc        di                    ; increase wordcount
  85.                jmp        scanloop4             ; locate begin of next word
  86. done1:         cmp        ax,0101h              ; check if end-of-word
  87.                je         done                  ; if not, we have finished
  88.                inc        di                    ; increase wordcount
  89.                jmp        done
  90. done2:         cmp        ax,0100h              ; check for one-letter word
  91.                jne        done                  ; if not, we have finished
  92.                inc        di                    ; increase wordcount
  93. done:          mov        si,[bp+charflag]
  94.                mov        [si],al
  95.                mov        bx,[bp+wordcount]
  96.                mov        ax,[bx]
  97.                mov        dx,[bx+2]
  98.                add        di,ax
  99.                adc        dx,0
  100.                mov        [bx],di
  101.                mov        [bx+2],dx
  102.                pop        di
  103.                pop        si
  104.                pop        bp
  105.                ret
  106. _ScanBuffer    endp
  107.                end
  108.  
  109.