home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / pcmag / vol7n20.arc / PP720.ARC / STRINGS2.ASM next >
Assembly Source File  |  1988-09-06  |  10KB  |  291 lines

  1. ;----------------------------------------------------------------------
  2. ; STRINGS2.ASM --- MASM String Package #2
  3. ; Copyright (c), 1988 Ziff Communications Co.
  4. ; PC Magazine * Ray Duncan * November 29, 1988
  5. ;----------------------------------------------------------------------
  6. bufsize equ     1024            ; size of buffer for temporary strings
  7.  
  8. _DATA   segment word public 'DATA'
  9.  
  10. lctab   dw      26              ; 'strlwr' translation table
  11.         dw      'A'
  12.         db      'abcdefghijklmnopqrstuvwxyz'
  13.  
  14. uctab   dw      26              ; 'strupr' translation table
  15.         dw      'a'
  16.         db      'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  17.  
  18. strbuf  db      bufsize dup (?) ; temporary string buffer
  19. strptr  dw      strbuf          ; current buffer pointer
  20.  
  21. _DATA   ends
  22.  
  23. _TEXT   segment word public 'CODE'
  24.         assume  cs:_TEXT
  25. ;----------------------------------------------------------------------
  26. ; STRCAT:       string concatenation routine
  27. ;
  28. ; Call with:    DS:SI = address of string1
  29. ;               BX    = length of string1
  30. ;               ES:DI = address of string2
  31. ;               DX    = length of string2
  32. ;
  33. ; Returns:      DS:SI = address of result string
  34. ;                       in temporary storage
  35. ;               BX    = length of result string
  36. ;
  37. ; Uses:         DI, ES
  38. ;----------------------------------------------------------------------
  39.         public  strcat
  40. strcat  proc    near
  41.  
  42.         push    cx              ; save register
  43.         push    di              ; save string2 address
  44.         push    es
  45.         mov     cx,bx           ; save string1 length
  46.         add     bx,dx           ; BX = result string length
  47.         call    strmem          ; get temporary storage for result string
  48.  
  49.         cld                     ; copy string1 to result
  50.         rep movsb
  51.  
  52.         pop     ds              ; get string2 address
  53.         pop     si
  54.         mov     cx,dx
  55.         rep movsb               ; copy string2 to result
  56.  
  57.         push    es              ; let DS:SI = address of
  58.         push    di              ; result string
  59.         pop     si
  60.         pop     ds
  61.         sub     si,bx           ; correct for bytes moved
  62.  
  63.         pop     cx              ; restore register
  64.         ret                     ; back to caller
  65.  
  66. strcat  endp
  67. ;----------------------------------------------------------------------
  68. ; STRDUP:       string duplication routine
  69. ;
  70. ; Call with:    DS:SI = address of string
  71. ;               BX    = length of string
  72. ;
  73. ; Returns:      DS:SI = address of string copy
  74. ;                       in temporary storage
  75. ;               BX    = length of string copy
  76. ;
  77. ; Uses:         nothing
  78. ;----------------------------------------------------------------------
  79.         public  strdup
  80. strdup  proc    near
  81.  
  82.         push    cx              ; save registers
  83.         push    di
  84.         push    es
  85.  
  86.         call    strmem          ; get temporary storage to hold string copy
  87.  
  88.         mov     cx,bx           ; make copy of string
  89.         cld
  90.         rep movsb               
  91.  
  92.         push    es              ; let DS:SI = address
  93.         push    di              ; of copy
  94.         pop     si
  95.         pop     ds
  96.         sub     si,bx           ; correct for bytes moved
  97.  
  98.         pop     es              ; restore registers
  99.         pop     di
  100.         pop     cx
  101.         ret                     ; back to caller
  102.  
  103. strdup  endp
  104. ;----------------------------------------------------------------------
  105. ; STRXTR:       string extraction routine
  106. ;
  107. ; Call with:    DS:SI = address of string
  108. ;               BX    = length of string
  109. ;               CX    = offset of substring
  110. ;               DX    = length of substring
  111. ;
  112. ; Returns:      DS:SI = address of substring
  113. ;                       in temporary storage
  114. ;               BX    = clamped length of substring
  115. ;
  116. ;               If BX = 0, then substring offset
  117. ;               was invalid and DS:SI is unchanged.
  118. ;
  119. ; Uses:         nothing
  120. ;----------------------------------------------------------------------
  121.         public  strxtr
  122. strxtr  proc    near
  123.  
  124.         push    cx              ; save register
  125.  
  126.         add     si,cx           ; point to substring
  127.         sub     bx,cx           ; adjust string length
  128.         jnb     sxtr1           ; length OK, proceed
  129.  
  130.         xor     bx,bx           ; bad substring,
  131.         jmp     sxtr3           ; return length = 0
  132.  
  133. sxtr1:  cmp     bx,dx           ; clamp length?
  134.         jb      sxtr2           ; yes, jump
  135.         mov     bx,dx           ; no, set substring length
  136.  
  137. sxtr2:  call    strdup          ; make copy of substring in temporary storage
  138.  
  139. sxtr3:  pop     cx              ; restore register
  140.         ret                     ; back to caller
  141.  
  142. strxtr  endp
  143. ;----------------------------------------------------------------------
  144. ; STRXLT:       string translation routine
  145. ;
  146. ; Call with:    DS:SI = address of text string
  147. ;               BX    = length of text string
  148. ;               ES:DI = address of translation table
  149. ;
  150. ;               The translation table has the 
  151. ;               following format:
  152. ;
  153. ;               dw   number of character codes in table (n)
  154. ;               dw   character code of first position (m)
  155. ;               db   translation value for character (m)
  156. ;               db   translation value for character (m+1)
  157. ;               .
  158. ;               .
  159. ;               db   translation value for character (m+n-1)
  160. ;
  161. ;               Any character positions in the table which 
  162. ;               contain zero are ignored.  Any characters
  163. ;               in the text string falling outside the range 
  164. ;               defined by the table are unchanged.
  165. ;
  166. ; Returns:      DS:SI = address of translated string
  167. ;                       in temporary storage
  168. ;               BX    = length of translated string
  169. ;               ES:DI = translation table address (unchanged)
  170. ;----------------------------------------------------------------------
  171.         public  strxlt
  172. strxlt  proc    near
  173.  
  174.         call    strdup          ; make copy of string to be translated
  175.  
  176.         push    bx              ; save registers
  177.         push    cx
  178.         push    si
  179.         mov     cx,bx           ; use CX for loop count
  180.         jcxz    sxlt3           ; exit if zero length
  181.  
  182. sxlt1:  mov     bl,[si]         ; next character
  183.         xor     bh,bh
  184.         sub     bx,es:[di+2]    ; correct for table base
  185.         js      sxlt2           ; jump, outside table
  186.         cmp     bx,es:[di]
  187.         jae     sxlt2           ; jump, outside table
  188.  
  189.         mov     bl,es:[bx+di+4] ; get translation value
  190.         or      bl,bl           ; is it zero?
  191.         jz      sxlt2           ; yes, ignore it
  192.         mov     [si],bl         ; store translated value
  193.  
  194. sxlt2:  inc     si              ; bump text string pointer
  195.         loop    sxlt1           ; process next character
  196.  
  197. sxlt3:  pop     si              ; restore registers
  198.         pop     cx
  199.         pop     bx
  200.         ret                     ; back to caller        
  201.  
  202. strxlt  endp
  203. ;----------------------------------------------------------------------
  204. ; STRLWR:       convert string to lower case
  205. ;
  206. ; Call with:    DS:SI = address of string
  207. ;               BX    = length of string
  208. ;
  209. ; Returns:      DS:SI = address of lower-cased 
  210. ;                       string in temporary storage
  211. ;               BX    = length of lower-cased string
  212. ;
  213. ; Uses:         nothing
  214. ;----------------------------------------------------------------------
  215.         public  strlwr
  216. strlwr  proc    near
  217.  
  218.         push    di              ; save registers
  219.         push    es
  220.  
  221.         mov     di,seg lctab    ; ES:DI = address of lower
  222.         mov     es,di           ; case translation table
  223.         mov     di,offset lctab
  224.  
  225.         call    strxlt          ; translate the string
  226.  
  227.         pop     es              ; restore registers
  228.         pop     di
  229.         ret                     ; back to caller
  230.  
  231. strlwr  endp
  232. ;----------------------------------------------------------------------
  233. ; STRUPR:       convert string to upper case
  234. ;
  235. ; Call with:    DS:SI = address of string
  236. ;               BX    = length of string
  237. ;
  238. ; Returns:      DS:SI = address of upper-cased 
  239. ;                       string in temporary storage
  240. ;               BX    = length of upper-cased string
  241. ;
  242. ; Uses:         nothing
  243. ;----------------------------------------------------------------------
  244.         public  strupr
  245. strupr  proc    near
  246.  
  247.         push    di              ; save registers
  248.         push    es
  249.  
  250.         mov     di,seg uctab    ; ES:DI = address of upper
  251.         mov     es,di           ; case translation table
  252.         mov     di,offset uctab
  253.  
  254.         call    strxlt          ; translate the string
  255.  
  256.         pop     es              ; restore registers
  257.         pop     di
  258.         ret                     ; back to caller
  259.  
  260. strupr  endp
  261. ;----------------------------------------------------------------------
  262. ; STRMEM:       allocate temporary storage for string
  263. ;
  264. ; Call with:    BX    = length needed
  265. ;
  266. ; Returns:      ES:DI = address of temporary storage
  267. ;               BX    = length (unchanged)
  268. ;----------------------------------------------------------------------
  269. strmem  proc    near
  270.  
  271.         mov     di,seg strptr   ; ES:DI = address within
  272.         mov     es,di           ; temporary string buffer
  273.         assume  es:_DATA
  274.         mov     di,strptr
  275.         add     strptr,bx       ; update buffer pointer
  276.  
  277.         cmp     strptr,offset (strbuf+bufsize)  ; check for buffer overflow
  278.         jb      smem1           ; jump if no overflow
  279.  
  280.         mov     di,offset strbuf; reset buffer pointer
  281.         mov     strptr,di
  282.         add     strptr,bx
  283.         assume  es:NOTHING
  284.  
  285. smem1:  ret                     ; back to caller
  286.  
  287. strmem  endp
  288.  
  289. _TEXT   ends
  290.         end
  291.