; DESC:    Returns number of CHAR delimited xwords in string, 0 if none.
; EXAMP:   t = xwords(Text$,CHAR)

Extrn   Get$Loc: Far
Extrn   Rls$Alloc: Far

MCODE Segment Byte
        Assume  CS: MCODE

        Public  xwords

xwords   Proc Far

ARG     mchar:WORD, StrHandle: WORD = Retbytes

        push    BP                      ;
        mov     BP,SP                   ;
        push    DS                      ;
                                        ; GET STRING POINTER
        mov     AX, StrHandle           ; put string handle in AX
        push    AX                      ; push it on the on stack
        call    Get$Loc                 ; get location of string
        mov     DS,DX                   ; put segment in DS
        mov     SI,AX                   ; put offset in SI
                                        ; SET VARS
        mov     AX,0                    ; clear AX
        jcxz    Exit                    ; is string null?
        xor     BX,BX                   ; clear BX
        mov     DX,0                    ; clear DX
        mov     BX,1

ReadChar:
        lodsb                           ; load a character
        cmp     AX,mchar                ; is character a space?
        jnz     Notsp                   ; if not 0 jump to Notsp
        mov     BX,0
        jmp     R2

Notsp:
        cmp     BX,2                    ; char flag previously set?
        jz      R2
        mov     BX,2
        inc     DX                      ; add 1 to word count
R2:
        loop ReadChar
Exit:
        mov     AX,DX                   ; return word count
        push    AX                      ; save return val
        push    Word Ptr StrHandle      ; push string handle
        call    Rls$Alloc               ; release string
        pop     AX
        pop     DS                      ;
        pop     BP                      ;
        retf    Retbytes                ;
xwords   EndP
MCODE EndS
        End