home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / stdlib.zip / STRREV2.ASM < prev    next >
Assembly Source File  |  1990-08-10  |  1KB  |  89 lines

  1. stdlib        segment    para public 'slcode'
  2.         assume    cs:stdlib
  3. ;
  4.         extrn    sl_malloc:far
  5. ;
  6. ;
  7. ; strrev2- reverses the characters in a string.
  8. ;
  9. ; inputs:
  10. ;
  11. ;    ES:DI- Points at the string to reverse.
  12. ;
  13. ; outputs:
  14. ;
  15. ;    ES:DI- Points at new string on the heap.
  16. ;    Carry=1 if memory allocation error, 0 if no error.
  17. ;
  18. ;
  19. ; Created by Mike Blaszczak (.B ekiM)  8/8/90
  20. ; Some minor tweaking by R. Hyde 8/9/90
  21. ;
  22. ;
  23.         public    sl_strrev2
  24. ;
  25. ;
  26. sl_strrev2    proc    far
  27.         push    ds
  28.         push    si
  29.         push    ax
  30.         push    cx
  31.         pushf
  32.         cld
  33. ;
  34. ;
  35. ; Compute the length (+1 for zero byte) of the string:
  36. ;
  37.         mov    cx, 0ffffh
  38.         mov    al, 0
  39.     repne    scasb
  40.         neg    cx
  41.         dec    cx
  42. ;
  43. ; Save ptr to end of the string
  44. ;
  45.         mov    si, es
  46.         mov    ds, si
  47.         lea    si, -1[di]        ;Points at zero byte.
  48. ;
  49. ; Allocate storage for the new string:
  50. ;
  51.         mov    ax, cx            ;Save length
  52.         call    sl_malloc
  53.         mov    cx, ax            ;Restore length
  54.         jc    BadStrRev
  55.         push    es            ;Save ptr to string.
  56.         push    di
  57. ;
  58. ; Note that string length is always at least one (for the zero byte).
  59. ; Copy and reverse the string down here.
  60. ;
  61. CopyBytes:    dec    si
  62.         mov    al, [si]
  63.         stosb
  64.         loop    CopyBytes
  65.         mov    byte ptr es:[di], 0
  66.         pop    di            ;Restore ptr to new string.
  67.         pop    es
  68. ;
  69.         popf
  70.         pop    cx
  71.         pop    ax
  72.         pop    si
  73.         pop    ds
  74.         clc
  75.         ret
  76. ;
  77. BadStrRev:    popf
  78.         pop    cx
  79.         pop    ax
  80.         pop    si
  81.         pop    ds
  82.         stc
  83.         ret
  84. sl_strrev2    endp
  85. ;
  86. ;
  87. stdlib        ends
  88.         end
  89.