;Copyright 2000 - Mark McDonald All rights reserved
; DECLARE: FUNCTION reverse$(BYVAL string)
; DESC:    Reverse all the characters in a string.
; EXAMP:   NewString$ = reverse$(OldString$)

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

MCODE Segment Byte
        Assume  CS: MCODE

        Public  reverse

reverse Proc Far

ARG     StrHandle: WORD = Retbytes

        push    BP                      ;
        mov     BP,SP                   ;
        push    DS                      ;
        mov     AX,StrHandle            ; push string handle in AX
        push    AX                      ; push it on the on stack
        call    Get$Loc                 ; get location of the string
        mov     DS,DX                   ; put segment in DS
        mov     SI,AX                   ; put offset in SI
        xor     AX,AX                   ; assume null string
        jcxz    Exit                    ; is it null?

        push    CX                      ; push length on stack
        call    Get$Alloc               ; allocate string space
        push    AX                      ; save handle for exit
        push    AX                      ; push it on stack
        call    Get$Loc                 ; get location of new string
        mov     ES,DX                   ; put segment in ES
        mov     DI,AX                   ; put offset in DI
        add     DI,CX                   ; move offset to end of target string
        dec     DI                      ; minus one
CopyString:
        mov     AL,DS: [SI]             ; get char from source string
        mov     ES: [DI],AL             ; put it in destination string
        inc     SI                      ; move to next char in source
        dec     DI                      ; move to previous char in target
        loop    CopyString              ;
        pop     AX                      ; get string handle
Exit:
        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                ;
reverse EndP
MCODE EndS
        End