home *** CD-ROM | disk | FTP | other *** search
/ ftp.shrubbery.net / 2015-02-07.ftp.shrubbery.net.tar / ftp.shrubbery.net / pub / pc / unix / unx.arc / STRINGS.ASM < prev    next >
Assembly Source File  |  1986-05-07  |  1KB  |  78 lines

  1. ;----- strings.asm ------------
  2. ; A set of string functions like those in c's stdio library, but
  3. ; which use register to pass arguments and results.
  4. ;
  5. ; int cx = strlen(es:di);
  6. ; Returns cx = length of null-terminated string at es:di.
  7. ;
  8. ; char *si=rindex(ds:si, al)
  9. ; Returns si = pointer to last occurrence of char al in string si.
  10. ; If no occurrence, returns zero.
  11. ; Alters AX, SI; all others preserved.
  12. ;
  13. ; flags strcmp(ds:si, es:di)
  14. ; Compares two null-terminated strings for equality.
  15. ; Preserves all registers other than si and di.
  16.  
  17.     public rindex, strcmp, strlen
  18.  
  19. code    segment para public 'CODE'
  20. assume cs:code
  21.  
  22. ;---- strlen ----
  23. ; input: es:di points to string
  24. ; output: cx is string length
  25. ; Uses ax
  26.  
  27. strlen    proc    near
  28.     mov    cx, 65535
  29.     mov    al, 0    
  30.     repnz    scasb
  31.     neg    cx
  32.     dec    cx
  33.     dec    cx
  34.     ret
  35. strlen    endp
  36.  
  37. rindex    proc    near
  38.     push    bx
  39.     cld
  40.     mov    ah, al
  41.     mov    bx, 1
  42. rloop:
  43.     lodsb
  44.     or    al, al
  45.     jz    rdone
  46.     cmp    al, ah
  47.     jnz    rloop
  48.     mov    bx, si
  49.     jmp    rloop
  50. rdone:    mov    si, bx
  51.     pop    bx
  52.     dec    si
  53.     ret
  54. rindex    endp
  55.  
  56. ;----- strcmp -----------------------------------------------------
  57. ; Compares two null-terminated strings for equality; results returned in flags.
  58. ; Uses SI and DI, preserves all others.
  59.  
  60. strcmp    proc    near
  61.     cld
  62.     push    ax
  63. sloop:
  64.     cmpsb
  65.     jnz    sdone
  66.     dec    si
  67.     lodsb
  68.     or    al, al
  69.     jnz    sloop
  70. sdone:
  71.     pop    ax
  72.     ret
  73. strcmp    endp
  74.  
  75. code    ends
  76.  
  77.     end
  78.