home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / asmlib.lbr / POSSTR.AZM / POSSTR.ASM
Encoding:
Assembly Source File  |  1991-06-25  |  2.8 KB  |  97 lines

  1. ;----------------------------------------------------------------
  2. ;       This is a module in the ASMLIB library.
  3. ;
  4. ; This module searches for the first occurrence of a string
  5. ; in another string. This is similar to the PL/I-80 INDEX 
  6. ; function.
  7. ; This module returns the accumulator = 0 if the string is not found or
  8. ; the position in the string where the string was found.
  9. ; This is a routine from 'Z-80 Subroutines By Saville and Leventhall'
  10. ; Modifications have been done to run under RMAC and to fit into
  11. ; ASMLIB parameter passing schemes.
  12. ; On entry DE -> main string, HL -> smaller string to find in large string.
  13. ; On exit A = index position in the larger string or 00 for error.
  14. ;
  15. ;            Written       R.C.H.    1/10/83
  16. ;            Last Update    R.C.H.        1/10/83
  17. ;----------------------------------------------------------------
  18. ;
  19.     name    'posstr'
  20.     public    posstr
  21.     maclib    z80
  22. ;
  23. posstr:
  24.     xchg                ; Fit into ASMLIB param. methods
  25.     push    b            ; Save this
  26.     shld    string            ; Save address of major string
  27.     xchg
  28.     mov    a,m
  29.     ora    a            ; Is substring length = 0 ?
  30.     jrz    notfnd            ; Exit with an error then
  31.     inx    h            ; Point to first character
  32.     shld    substg            ; Save substring address
  33.     sta    sublen            ; Save length of substring
  34.     mov    c,a            ; C = substring length
  35.     ldax    d            ; Get major string address
  36.     ora    a
  37.     jrz    notfnd            ; Exit with error if = 0
  38. ;
  39. ; Number of searches for a match = major length - substring length + 1
  40. ;
  41.     sub    c            ; Now A = major length - sub. length
  42.     jrc    notfnd            ; Exit if major shorter that sub.
  43.     inr    a
  44.     mov    b,a            ; Load a counter
  45.     sub    a            ; Start at character 0
  46.     sta    index            ; Save as the match index 
  47. ;
  48. ; Here we search till the string is shorter than the substring.
  49. ;
  50. slp1:
  51.     lxi    h,index            ; Point to the index
  52.     inr    m            ; Incerment
  53.     lxi    h,sublen
  54.     mov    c,m            ; now C = length of substring
  55.     lhld    string
  56.     inx    h
  57.     shld    string            ; Bump string pointer
  58.     lded    substg            ; Load address of substring
  59. ;
  60. ; Here we try for a match
  61. ;
  62. cmplp:
  63.     ldax    d            ; Load a substring byte
  64.     cmp    m            ; Equal to the string byte ?
  65.     jrnz    slp2            ; Jump if not the same
  66.     dcr    c            ; One less substring character to check
  67.     jrz    found            ; If all substring done the a match
  68.     inx    h
  69.     inx    d            ; Bump pointers to strings
  70.     jr    cmplp
  71. ; Arrive here if a single character match fails.
  72. ;
  73. slp2:
  74.     djnz    slp1            ; Go and try next major character
  75. ;
  76. notfnd:    ; Here and there was no matching substring in the major string
  77.     sub    a            ; Load a 0 into A
  78.     pop    b
  79.     ret
  80. ;
  81. found:
  82.     lda    index
  83.     pop    b
  84.     ret                ; Exit with result in A
  85. ;
  86.     dseg
  87. string:    db    00,00            ; Major string address
  88. substg:    db    00,00            ; Substring address
  89. slen:    db    00            ; Length of major string
  90. sublen:    db    00            ; Substring length
  91. index:    db    00            ; Index to the substring.
  92.  
  93.     end
  94.  
  95.  
  96.  
  97.