home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / zendisk2.zip / LST13-14.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  3KB  |  101 lines

  1. ;
  2. ; *** Listing 13-14 ***
  3. ;
  4. ; Copies a zero-terminated string to another string,
  5. ; optionally converting characters to uppercase. The
  6. ; decision as to whether to convert to uppercase is made
  7. ; once at the beginning of the subroutine; if conversion
  8. ; is not desired, the register containing the value of the
  9. ; start of the lowercase range is simply set to cause all
  10. ; tests for lowercase to fail. This avoids one test in the
  11. ; case where conversion to uppercase is desired, since the
  12. ; single test for the start of the lowercase range is able
  13. ; to perform both that test and the test for whether
  14. ; conversion is desired.
  15. ;
  16.     jmp    Skip
  17. ;
  18. SourceString    label    byte
  19.     db    'This is a sample string, consisting of '
  20.     db    'both uppercase and lowercase characters.'
  21.     db    0
  22. DestinationString    label    byte
  23.     db    100 dup (?)
  24. ;
  25. ; Copies a zero-terminated string to another string,
  26. ; optionally converting characters to uppercase.
  27. ;
  28. ; Input:
  29. ;    DL = 1 if conversion to uppercase during copying is
  30. ;        desired, 0 otherwise
  31. ;    DS:SI = source string
  32. ;    ES:DI = destination string
  33. ;
  34. ; Output: none
  35. ;
  36. ; Registers altered: AX, SI, DI
  37. ;
  38. ; Direction flag cleared
  39. ;
  40. ; Note: Does not handle strings that are longer than 64K
  41. ;    bytes or cross segment boundaries.
  42. ;
  43. CopyAndConvert:
  44.     cld
  45.     mov    ah,0ffh    ;assume conversion to uppercase is
  46.             ; not desired. In that case, this
  47.             ; value will cause the initial
  48.             ; lowercase test to fail (except
  49.             ; when the character is 0FFh, but
  50.             ; that's rare and will be rejected
  51.             ; by the second lowercase test
  52.     and    dl,dl    ;is conversion to uppercase desired?
  53.     jz    CopyAndConvertLoop    ;no, AH is all set
  54.     mov    ah,'a'    ;set the proper lower limit of the
  55.             ; lowercase range
  56. CopyAndConvertLoop:
  57.     lodsb                ;get the next byte
  58.                     ; to check
  59.     cmp    al,ah            ;less than 'a'?
  60.                     ; (If conversion
  61.                     ; isn't desired,
  62.                     ; AH is 0FFh, and
  63.                     ; this fails)
  64.     jb    CopyAndConvertUC    ;yes, not lowercase
  65.     cmp    al,'z'            ;greater than 'z'?
  66.     ja    CopyAndConvertUC    ;yes, not lowercase
  67.     and    al,not 20h        ;make it uppercase
  68. CopyAndConvertUC:
  69.     stosb                ;put the byte in the
  70.                     ; destination string
  71.     and    al,al            ;was that the
  72.                     ; terminating zero?
  73.     jnz    CopyAndConvertLoop    ;no, do next byte
  74.     ret
  75. ;
  76. Skip:
  77.     call    ZTimerOn
  78. ;
  79. ; First, copy without converting to uppercase.
  80. ;
  81.     mov    di,seg DestinationString
  82.     mov    es,di
  83.     mov    di,offset DestinationString
  84.             ;ES:DI points to the destination
  85.     mov    si,offset SourceString
  86.             ;DS:SI points to the source
  87.     sub    dl,dl    ;don't convert to uppercase
  88.     call    CopyAndConvert    ;copy without converting
  89.                 ; to uppercase
  90. ;
  91. ; Now copy and convert to uppercase.
  92. ;
  93.     mov    di,offset DestinationString
  94.             ;ES:DI points to the destination
  95.     mov    si,offset SourceString
  96.             ;DS:SI points to the source
  97.     mov    dl,1    ;convert to uppercase this time
  98.     call    CopyAndConvert    ;copy and convert to
  99.                 ; uppercase
  100.     call    ZTimerOff
  101.