;Copyright 2000 - Mark McDonald All rights reserved
; DESC:    Returns number of space delimited words in string, 0 if none.
; EXAMP:   t = words(Text$)

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

MCODE Segment Byte
        Assume  CS: MCODE

        Public  words

words    Proc Far

ARG     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,32                   ; 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                ;
words    EndP
MCODE EndS
        End