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

;   memrchr(src, chr, len)
;   searches the memory area pointed to by src extending for len bytes,
;   looking for an occurrence of the byte value chr.  It returns NULL
;   if there is no such occurrence.  Otherwise it returns a pointer to
;   the LAST such occurrence.
;

        .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
; Line 19
        PUBLIC  _memrchr
_memrchr        PROC NEAR
        push    bp
        mov     bp,sp
        push    di
        push    si

;       src = 4
;       chr = 6
;       len = 8

        mov     di,[bp+4]       ;src
        mov     cx,[bp+8]       ;len
        mov     al,[bp+6]       ;chr
        add     di,cx
        dec     di
        std
        repnz   scasb
        jcxz    no_find
        mov     ax,di
        inc     ax
        jmp     short cleanup
no_find:
        xor     ax,ax
cleanup:
        cld
        pop     si
        pop     di
        mov     sp,bp
        pop     bp
        ret

_memrchr        ENDP
_TEXT   ENDS
END
