;       Static Name Aliases
;
        TITLE   strnlen
;       NAME    strnlen.C

;   int strnlen(src, len)
;   returns the number of characters up to the first NUL in src, or len,
;   whichever is smaller.

        .287
_TEXT   SEGMENT  BYTE PUBLIC 'CODE'
_TEXT   ENDS
_DATA   SEGMENT  WORD PUBLIC 'DATA'
_DATA   ENDS
CONST   SEGMENT  WORD PUBLIC 'CONST'
CONST   ENDS
_BSS    SEGMENT  WORD PUBLIC 'BSS'
_BSS    ENDS
DGROUP  GROUP   CONST,  _BSS,   _DATA
        ASSUME  CS: _TEXT, DS: DGROUP, SS: DGROUP, ES: DGROUP
EXTRN   __chkstk:NEAR
_TEXT      SEGMENT
        PUBLIC  _strnlen
_strnlen        PROC NEAR
        push    bp
        mov     bp,sp
        push    si

;       s = 4
;       register si = s
;       n = 6
;       register cx = n
;       L = ax

        mov     si,[bp+4]       ;s
        mov     cx,[bp+6]       ;n
        xor     bx,bx                   ;L
$cmp:
        jcxz    cleanup
        cmp     BYTE PTR [si][bx],0
        je      cleanup
        dec     cx
        inc     bx              ;L
        jmp     $cmp
cleanup:
        xchg    ax,bx
        pop     si
        mov     sp,bp
        pop     bp
        ret

_strnlen        ENDP
_TEXT   ENDS
END
