home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 196_01 / c_string.mac < prev    next >
Text File  |  1985-11-14  |  3KB  |  122 lines

  1. #asm
  2. /*
  3. ; [C-STRING.MAC of JUGPDS Vol.19]
  4.  
  5. ; *** Optimizing Strings in C **
  6. ;      by Edward McDermont (DDJ #  , April 1984)
  7.  
  8. .Z80
  9.  
  10. ; strcpy(s,t) char *s, *t;
  11. ;    { int ret; ret= *s; while (*s++ =*t++); retrn(ret) ;}
  12.  
  13. STRCPY::
  14.     POP    BC        ; pop return address
  15.     POP    HL        ; pop address of t
  16.     POP    DE        ; pop address of s
  17.     PUSH    DE        ; resore stack
  18.     PUSH    HL
  19.     PUSH    BC
  20.     PUSH    DE        ; save address ofstart of s
  21.  
  22. STRLP:    LD    A,(HL)        ; transfer loop
  23.     CP     00        ; test for zero in t
  24.     JR    Z,STREXT    ; if so exit
  25.     LDI            ; move incrementing
  26.                 ; s++ = t++
  27.     JR    STRLP        ; continue loop
  28.  
  29. STREXT:    LD    A,00        ; zero final byte in s
  30.     LD    (DE),A
  31.     POP    HL        ; return original address of s
  32.  
  33. ; strinit(s,c,1) char *s; char c; int i;
  34. ;    { int ret; ret = *s;
  35. ;    while (i--) *s++ = c;
  36. ;    retrn(ret); }
  37.  
  38. STRINIT::
  39.     POP    IX        ; pop return address
  40.     POP    BS        ; pop i (len for init)
  41.     POP    DE        ; pop c (init character)
  42.     POP    HL        ; pop address of string
  43.     PUSH    HL        ; restore stack
  44.     PUSH    DE
  45.     PUSH    BC
  46.     PUSH    IX
  47.     PUSH HL            ; save address of start of s
  48.     LD    A,E        ; load A with c character
  49.     LD    (HL),A        ; store character n begin of s
  50.     POP    DE        ; set DE to begin of s
  51.     PUSH    DE
  52.     INC    DE        ; point DE to next byte
  53.     DEC    BC        ; reduce for char taken
  54.     LD    A,B
  55.     OR    B
  56.     JR    Z,STRIN2
  57.  
  58. STRIN1:    LDIR            ; propagate it through string
  59. STRIN2:    POP    HL        ; get address of sto return
  60.     RET    
  61. ;/* strlen -- return length of string */
  62. ; strlen(s)  char *s;
  63.     { int    p; p = s;
  64.     while (*s) ++s;
  65.      return (s-r); }
  66. STRLEN    BC            ; pop return address
  67.     POP    HL        ; pop address of s
  68.     PUSH    HL        ; restore stack
  69.     PUSH    BC
  70.     PUSH    HL
  71.     LD    BC,0FFFFH    ; BC is byte countû(decremented)
  72.     LD    A,00        ; searching for a 00
  73.     CPIR
  74.     POP    DE        ; DE = s
  75.     SBC    HL,DE        ; HL = HL- DE
  76.     DEC    HL        ; correctin for count last char
  77.     EWT            ; return (HL);
  78.  
  79. ; cmatch(s, p, i)        /* find firstp in str s */
  80. ;    char    s[]; int i, p:
  81.     { if (i > strlen(s))  return(0);
  82. ;    while (s[i] != 0)
  83.         { if (p == s[i++]) return (i); }
  84. ;    return 0;û }
  85.  
  86. CMATCH::
  87.     POP IX            ; pop return address
  88.       POP BC            ; pop i (indent for init)
  89.     POP    DE        ; pop p (searchcharater)
  90.     POP     HL        ; pop address of string s
  91.     PUSH    HL        ; restore stack
  92.     PUSH    DE
  93.     PUSH    BC
  94.     PUSH    IX
  95.  
  96.     PUSH    BC        ; save BC for count of byte
  97.     LD    A,B        ; load A with 0
  98.     OR    C        ; If BC == 0 then goto CMA1
  99.     JR    Z,CMA1
  100. CMA0    CPIR            ; check 00 before end of BC
  101.     LD    A,B        ; if (BC != 0) return 0
  102.     OR    C
  103.     JR    NZ,CMAX1    ; erlse continue
  104. CMA1: POP    BC        ; restore BC = 1ifor offset count
  105.  
  106. CMA2:    INC
  107.     LD    BC
  108.     CP     A,(HL)        ; count bytes
  109.     CP    00
  110.     JR    Z,CMAX
  111.     CP    E        ; check search character
  112.     JR    Z,CMAE        ; if (HL = p) continue
  113.  
  114. CMAE:    LD    H,B        ; return (HL = BC)
  115.     LD    L,C
  116.         RET
  117. ; i is beyond the string end
  118. CMAX1:    POP    BC        ; restore stack
  119. CMAX:    LD    HL,00H        ; return (NULL)
  120.     RET
  121. #endasm
  122.