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

;   memrev(dst, src, len)
;   moves len bytes from src to dst, in REVERSE order.  NUL characters
;   receive no special treatment, they are moved like the rest.  It is
;   to strrev as memcpy is to strcpy.
;
;   Note: this function is perfectly happy to reverse a block into the
;   same place, memrev(x, x, L) will work.
;
;   It will not work for partially overlapping source and destination.
;
;   Returns a pointer to the beginning of the dst.

        .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  _memrev
_memrev PROC NEAR
        push    bp
        mov     bp,sp
        mov     ax,8
        call    __chkstk
        push    di
        push    si

;       dsta = 4
;       register si = dsta
;       srca = 6
;       register di = srca
;       len = 8
;       dstz = -4
;       srcz = -6
;       result = -2

        mov     si,[bp+4]       ;dsta
        mov     di,[bp+6]       ;srca
        mov     [bp-2],si       ;result

        cmp     WORD PTR [bp+8],0       ;len
        jle     $I16


        mov     ax,[bp+8]       ;len
        add     ax,di
        mov     [bp-6],ax       ;srcz

        mov     ax,[bp+8]       ;len
        add     ax,si
        mov     [bp-4],ax       ;dstz

        jmp     SHORT $L20001
$WC17:

        dec     WORD PTR [bp-6] ;srcz
        mov     bx,[bp-6]       ;srcz
        mov     cl,[bx]

        dec     WORD PTR [bp-4] ;dstz
        mov     bx,[bp-4]       ;dstz
        mov     al,[di]
        inc     di
        mov     [bx],al

        mov     [si],cl
        inc     si

$L20001:
        cmp     [bp-6],di       ;srcz
        ja      $WC17


$I16:
        mov     ax,[bp-2]       ;result
        pop     si
        pop     di
        mov     sp,bp
        pop     bp
        ret

_memrev ENDP
_TEXT   ENDS
END
