home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 214_01 / strfunr.csm < prev    next >
Text File  |  1979-12-31  |  4KB  |  156 lines

  1. ;/* STRFUNR.CSM    VERS:- 01.00  DATE:- 09/26/86  TIME:- 09:36:51 PM */
  2. ;
  3. ; Description:
  4. ;
  5. ; Library of assembly code functions to handle string operations.
  6. ;
  7. ; strlen()  strcmp()  strcpy()  strcat()
  8. ;
  9. ; Converted cug routines by M.J. Maney from asm to csm format.
  10. ;
  11. ;By J.A. Rupley, Tucson, Arizona
  12. ;Coded for BDS C compiler, version 1.50a
  13. ;*************************************************************************
  14. ;                strfun.asm
  15. ;
  16. ;    Copyright (C) 1980, M J Maney
  17. ;
  18. ;    First created 8/25/80
  19. ;    Last revised 8/30/80 19:15
  20. ;
  21. ;    Herein are the basic string functions that C people like to use,
  22. ;    coded in assembler to make them as fast as can be.
  23. ;
  24. ;    NOTE that I have used .NE. in the comments in place of the usual C
  25. ;    symbol, because MAC sometimes objects to such usage.
  26. ;
  27.     include "bds.lib"
  28. ;
  29. ;
  30. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  31. ;
  32. ;                string functions
  33. ;
  34. ;    strlen        strcmp        strcpy        strcat
  35. ;
  36. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  37. ;
  38. ;
  39. ;    strlen(str)
  40. ;    char *str;
  41. ;
  42. ;    Returns the length of the string pointed to by str. It is assumed
  43. ;    that the string is terminated by '\0', and the terminator is NOT
  44. ;    counted as a character, so the storage length of a string s is
  45. ;    strlen(s) + 1.
  46. ;
  47.     FUNCTION    STRLEN
  48.     call    arghak
  49.     lxi    d,0
  50.     lhld    ARG1        ;str to HL
  51.     mvi    a,0
  52. strlen1    cmp    m
  53.     JZ    strlen2        ;for (i=0; *str++ .NE. EOS; i++) ;
  54.     inx    h
  55.     inx    d
  56.     JMP    strlen1
  57. ;
  58. strlen2    xchg            ;return i;
  59.     ret
  60.     ENDFUNC    STRLEN
  61. ;
  62. ;
  63. ;    strcmp(sx,sy)
  64. ;    char *sx,*sy;
  65. ;
  66. ;    Returns 1, 0, or -1, depending upon the lexicographical relation
  67. ;    of the strings pointed to by sx and sy. The ordering is essentially
  68. ;    that which would be used if the strings were words being alphebetized
  69. ;    except that upper case and lower case are distinct, and digits and
  70. ;    punctuation are treated as significant characters. The relation
  71. ;    between any two characters is simply the relation between the bit
  72. ;    patterns that are used to represent them, treated as unsigned
  73. ;    integers. It is also assumed that the string terminator, '\0', has
  74. ;    a smaller value than any other character (in fact, its zero).
  75. ;
  76. ;    1 if sx > sy, 0 if sx == sy, or -1 if sx < sy
  77. ;
  78.     FUNCTION    STRCMP
  79.     call    arghak
  80.     lhld    ARG1
  81.     xchg            ;sx to DE
  82.     lhld    ARG2        ;sy to HL
  83. ;
  84. strcmp1    ldax    d
  85.     cmp    m        ;while (*sy == (c = *sx)) {
  86.     JNZ    strcmp2
  87.     inx    h        ;    sy++;
  88.     inx    d        ;    sx++;
  89.     ora    a
  90.     JNZ    strcmp1        ;    if (c == EOS)
  91.     lxi    h,0        ;        return 0;
  92.     ret            ;    }
  93. strcmp2    lxi    h,1        ;if (*sx > *sy)
  94.     rnc            ;    return 1;
  95.     dcx    h        ;else
  96.     dcx    h
  97.     ret            ;    return -1;
  98.     ENDFUNC    STRCMP
  99. ;
  100. ;
  101. ;    strcpy(des,src)
  102. ;    char *des,*src;
  103. ;
  104. ;    Copies string pointed to by src into memory starting at des. No
  105. ;    provision is made for limiting the amount of stuff copied: it is
  106. ;    the user's responsibility to insure that the string being copied
  107. ;    will fit where it is being copied to.
  108. ;
  109.     FUNCTION    STRCPY
  110.     call    arghak
  111.     lhld    ARG1
  112.     xchg            ;des to DE
  113.     lhld    ARG2        ;src to HL
  114. strcpy1    mov    a,m        ;do {
  115.     inx    h
  116.     stax    d
  117.     inx    d        ;    *des++ = c = *src++;
  118.     ora    a
  119.     JNZ    strcpy1        ;    }while (c .NE. EOS);
  120.     ret            ;return;
  121.     ENDFUNC    STRCPY
  122. ;
  123. ;
  124. ;    strcat(des,src)
  125. ;    char *des,*src;
  126. ;
  127. ;    Copies the string pointed to by src onto the tail end of the string
  128. ;    pointed to by des. This is equivalent to
  129. ;
  130. ;        strcpy(des+strlen(des),src)
  131. ;
  132. ;    and there is an identical lack of testing for overrun during the
  133. ;    copying process.
  134. ;
  135.     FUNCTION    STRCAT
  136.     call    arghak
  137.     lhld    ARG2
  138.     xchg            ;src to DE
  139.     lhld    ARG1        ;des to HL
  140.     sub    a
  141. strcat1    cmp    m        ;while (*des++ .NE. EOS)
  142.     inx    h        ;    ;
  143.     JNZ    strcat1
  144.     dcx    h        ;des--;
  145. strcat2    ldax    d        ;do {
  146.     inx    d
  147.     mov    m,a
  148.     inx    h        ;    *des++ = c = *src++;
  149.     ora    a
  150.     JNZ    strcat2        ;    }while (c .NE. EOS);
  151.     ret            ;return;
  152.     ENDFUNC    STRCAT
  153. ;
  154. ;
  155.     end
  156.