home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / TASMSWAN.ZIP / STRINGS.ASM < prev    next >
Assembly Source File  |  1989-07-12  |  8KB  |  395 lines

  1. %TITLE  "String Procedures -- Copyright 1989 by Tom Swan"
  2.  
  3.     IDEAL
  4.     DOSSEG
  5.     MODEL    small
  6.  
  7.     CODESEG
  8.  
  9.     PUBLIC    MoveLeft, MoveRight, StrNull, StrLength
  10.     PUBLIC    StrUpper, StrCompare,StrDelete,StrInsert
  11.     PUBLIC    StrConcat, StrCopy,StrPos,StrRemove
  12.  
  13. ;--------------------------------------------------------------------
  14. ;    For usage see book.
  15. ;-------------------------------------------------------------------
  16.  
  17. ASCNull     EQU    0
  18.  
  19. %NEWPAGE
  20. ;-------------------------------------------------------------------
  21. ; MoveLeft -  move byte-block left (down) in memory
  22. ;
  23. ;    Input:    si = address of source string (s1)
  24. ;        di = address of destination string (s2)
  25. ;        bx = index s1 (i1)
  26. ;        dx = index s2 (i2)
  27. ;        cx = number of bytes to move (count)
  28. ;    Output: count bytes from s1[i1] moved to the location starting at s2[i2]
  29. ;    Registers:  none
  30. ;
  31. ;-------------------------------------------------------------------
  32.  
  33. PROC    MoveLeft
  34.     jcxz    @@99
  35.     push    cx
  36.     push    si
  37.     push    di
  38.     add    si,bx
  39.     add    di,dx
  40.     cld
  41.     rep    movsb
  42.     pop    di
  43.     pop    si
  44.     pop    cx
  45. @@99:
  46.     ret
  47. ENDP    MoveLeft
  48. %NEWPAGE
  49. ;-------------------------------------------------------------------
  50. ; MoveRight -  move byte-block right (up) in memory
  51. ;
  52. ;    Input:    si = address of source string (s1)
  53. ;        di = address of destination string (s2)
  54. ;        bx = index s1 (i1)
  55. ;        dx = index s2 (i2)
  56. ;        cx = number of bytes to move (count)
  57. ;    Output: count bytes from s1(i1) moved to the location starting at s2(i2)
  58. ;    Registers:  none
  59. ;
  60. ;-------------------------------------------------------------------
  61.  
  62. PROC    MoveRight
  63.     jcxz    @@99
  64.     push    cx
  65.     push    di
  66.     push    si
  67.     add    si,bx
  68.     add    di,dx
  69.     add    si,cx
  70.     dec    si
  71.     add    di,cx
  72.     dec    di
  73.     std
  74.     rep    movsb
  75.     pop    si
  76.     pop    di
  77.     pop    cx
  78. @@99:
  79.     ret
  80. ENDP    MoveRight
  81. %NEWPAGE
  82. ;-------------------------------------------------------------------
  83. ; StrNull -  Erase all characters in a string
  84. ;
  85. ;    Input:    di = address of string (s)
  86. ;    Output: s[0] <- null character (ASCII 0)
  87. ;    Registers:  none
  88. ;
  89. ;-------------------------------------------------------------------
  90.  
  91. PROC    StrNull
  92.     mov    [byte ptr di], ASCNull
  93.     ret
  94. ENDP    StrNull
  95. %NEWPAGE
  96. ;-------------------------------------------------------------------
  97. ; StrLength -  Count non-null characters in a string
  98. ;
  99. ;    Input:    di = address of string (s)
  100. ;    Output: cx = number of non-null characters in (s)
  101. ;    Registers:  cx
  102. ;
  103. ;-------------------------------------------------------------------
  104.  
  105. PROC    StrLength
  106.     push    ax
  107.     push    di
  108.     xor    al,al
  109.     mov    cx,0ffffh
  110.     cld
  111.     repnz    scasb
  112.     not    cx
  113.     dec    cx
  114.     pop    di
  115.     pop    ax
  116.     ret
  117. ENDP    StrLength
  118. %NEWPAGE
  119. ;-------------------------------------------------------------------
  120. ; StrUpper  - Convert characters in string to uppercase
  121. ;
  122. ;    Input:    di = address of string to convert (s)
  123. ;    Output: Lowercase chars in string converted to uppercase
  124. ;    Registers:  none
  125. ;
  126. ;-------------------------------------------------------------------
  127.  
  128. PROC    StrUpper
  129.     push    ax
  130.     push    cx
  131.     push    di
  132.     push    si
  133.     call    StrLength
  134.     jcnxz    @@99
  135.     cld
  136.     mov    si,di
  137. @@10:
  138.     lodsb
  139.     cmp    al, 'a'
  140.     jb    @@20
  141.     cmp    al, 'z'
  142.     ja    @@20
  143.     sub    al, 'a'-'A'
  144. @@20:
  145.     stosb
  146.     loop    @@10
  147. @@99:
  148.     pop    si
  149.     pop    di
  150.     pop    cx
  151.     pop    ax
  152.     ret
  153. ENDP    StrUpper
  154. %NEWPAGE
  155. ;-------------------------------------------------------------------
  156. ; StrCompare   compare two strings
  157. ;
  158. ;    Input:    si = address of string 1 (s1)
  159. ;        di = address of string 2 (s2)
  160. ;    Output: flags set for conditional jump using jb,jbe,je,ja, or jae.
  161. ;    Registers:  none
  162. ;
  163. ;-------------------------------------------------------------------
  164.  
  165. PROC    StrCompare
  166.     push    ax
  167.     push    di
  168.     push    si
  169.     cld
  170. @@10:
  171.     lodsb
  172.     scasb
  173.     jne    @@20
  174.     or    al,al
  175.     jne    @@10
  176. @@20:
  177.     pop    si
  178.     pop    di
  179.     pop    ax
  180.     ret
  181. ENDP    StrCompare
  182. %NEWPAGE
  183. ;-------------------------------------------------------------------
  184. ; StrDelete - delete characters anywhere in a string
  185. ;
  186. ;    Input:    di = address of string (s)
  187. ;        dx = index (i) of first char to delete
  188. ;        cx = number of characters to delete (n)
  189. ;    Output: n characters deleted from string at s[i]
  190. ;        NOTE: prevents deleting past end of string
  191. ;    Registers:  none
  192. ;
  193. ;-------------------------------------------------------------------
  194.  
  195. PROC    StrDelete
  196.     push    bx
  197.     push    cx
  198.     push    di
  199.     push    si
  200.     mov    bx,dx
  201.     add    bx,cx
  202.     call    StrLength
  203.     cmp    cx,bx
  204.     ja    @@10
  205.     add    di,dx
  206.     mov    [byte ptr di],ASCNull
  207.     jmp    short @@99
  208. @@10:
  209.     mov    si,di
  210.     sub    cx,bx
  211.     inc    cx
  212.     call    MoveLeft
  213. @@99:
  214.     pop    si
  215.     pop    di
  216.     pop    cx
  217.     pop    bx
  218.     ret
  219. ENDP    StrDelete
  220. %NEWPAGE
  221. ;-------------------------------------------------------------------
  222. ; StrInsert - insert one string into another
  223. ;
  224. ;    Input:    si = address of string 1 (s1)
  225. ;        di = address of string 2 (s2)
  226. ;        dx = insertion index for s2 (i)
  227. ;        NOTE: s2 must be large enough to expand by length (s1)!
  228. ;    Output: chars from string s1 inserted at s2[i], s1 not changed
  229. ;    Registers:  none
  230. ;
  231. ;-------------------------------------------------------------------
  232.  
  233. PROC    StrInsert
  234.     push    ax
  235.     push    bx
  236.     push    cx
  237.     xchg    si,di
  238.     call    StrLength
  239.     xchg    si,di
  240.     mov    ax,cx
  241.     call    StrLength
  242.     sub    cx,dx
  243.     inc    cx
  244.     push    dx
  245.     push    si
  246.     mov    si,di
  247.     mov    bx,dx
  248.     add    dx,ax
  249.     call    MoveRight
  250.     pop    si
  251.     pop    dx
  252.     xor    bx,bx
  253.     mov    cx,ax
  254.     call    MovLeft
  255.     pop    cx
  256.     pop    bx
  257.     pop    ax
  258.     ret
  259. ENDP    StrInsert
  260. %NEWPAGE
  261. ;-------------------------------------------------------------------
  262. ; StrConcat - concatenate (join) two strings
  263. ;
  264. ;    Input:    si = address of source string (s1)
  265. ;        di = address of destination string (s2)
  266. ;        NOTE:  s2 must be large enough to expand by length (s1) !
  267. ;    Output: chars from s1 added to end of s2
  268. ;    Registers:  none
  269. ;
  270. ;-------------------------------------------------------------------
  271.  
  272. PROC    StrConcat
  273.     push    bx
  274.     push    cx
  275.     push    dx
  276.     call    StrLength
  277.     mov    dx,cx
  278.     xchg    si,di
  279.     call    StrLength
  280.     inc    cx
  281.     xchg    si,di
  282.     xor    bx,bx
  283.     call    MoveLeft
  284.     pop    dx
  285.     pop    cx
  286.     pop    bx
  287.     ret
  288. ENDP    StrConcat
  289. %NEWPAGE
  290. ;-------------------------------------------------------------------
  291. ; StrCopy - copy one string to another
  292. ;
  293. ;    Input:    si = address of source string (s1)
  294. ;        di = address of destination string (s2)
  295. ;    Output: chars in s1 copied to s2
  296. ;        NOTE: s2 must be at least length (s1) + 1 bytes long
  297. ;    Registers:  none
  298. ;
  299. ;-------------------------------------------------------------------
  300.  
  301. PROC    StrCopy
  302.     push    bx
  303.     push    cx
  304.     push    dx
  305.     xchg    si,di
  306.     call    StrLength
  307.     inc    cx
  308.     xchg    si,di
  309.     xor    bx,bx
  310.     xor    dx,dx
  311.     call    MoveLeft
  312.     pop    dx
  313.     pop    cx
  314.     pop    bx
  315.     ret
  316. ENDP    StrCopy
  317. %NEWPAGE
  318. ;-------------------------------------------------------------------
  319. ; StrPos - search for position of a substring in a string
  320. ;
  321. ;    Input:    si = address of substring to find
  322. ;        di = address of target string to scan
  323. ;    Output: if zf = 1 then dx = index of substring
  324. ;        if zf = 0 then substring was not found
  325. ;        NOTE: dx is meaningless if zf = 0
  326. ;    Registers:  dx
  327. ;
  328. ;-------------------------------------------------------------------
  329.  
  330. PROC    StrPos
  331.     push    ax
  332.     push    bx
  333.     push    cx
  334.     push    di
  335.     call    StrLength
  336.     mov    ax,cx
  337.     xchg    si,di
  338.     call    StrLength
  339.     mov    bx,cx
  340.     xchg    si,di
  341.     sub    ax,bx
  342.     jb    @@20
  343.     mov    dx,0ffffh
  344. @@10:
  345.     inc    dx
  346.     mov    cl,[byte bx + di]
  347.     mov    [byte bx + di],ASCNull
  348.     call    StrCompare
  349.     mov    [byte bx + di],cl
  350.     je    @@20
  351.     inc     di
  352.     cmp    dx,ax
  353.     jne    @@10
  354.     xor    cx,cx
  355.     inc    cx
  356. @@20:
  357.     pop    di
  358.     pop    cx
  359.     pop    bx
  360.     pop    ax
  361.     ret
  362. ENDP    StrPos
  363. %NEWPAGE
  364. ;-------------------------------------------------------------------
  365. ; StrRemove - remove substring from a string
  366. ;
  367. ;    Input:    si = address of substring to delete
  368. ;        di = address of string to delete substring from
  369. ;    Output: if zf = 1 then substring removed
  370. ;        if zf = 0 then substring was not found
  371. ;        NOTE: string at si is not changed
  372. ;        NOTE: if zf = 0 then string at di is not changed
  373. ;    Registers:  none
  374. ;
  375. ;-------------------------------------------------------------------
  376.  
  377. PROC    StrRemove
  378.     push    cx
  379.     push    dx
  380.     call    StrPos
  381.     jne    @@99
  382.     pushf
  383.     xchg    si,di
  384.     call    StrLength
  385.     xchg    si,di
  386.     call    StrDelete
  387.     popf
  388. @@99:
  389.     pop    dx
  390.     pop    cx
  391.     ret
  392. ENDP    StrRemove
  393.  
  394.     END
  395.