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

  1. ;
  2. ; *** Listing 14-2 ***
  3. ;
  4. ; Copies a zero-terminated string to another string,
  5. ; filtering out non-printable characters by means of a
  6. ; subroutine that performs the test. The subroutine is
  7. ; invoked with a JMP and returns with another JMP.
  8. ;
  9.     jmp    Skip
  10. ;
  11. SourceString    label    byte
  12.     db    'This is a sample string, consisting of '
  13. X=1
  14.     rept    31
  15.     db    X
  16. X=X+1
  17.     endm
  18.     db    7fh
  19.     db    'both printable and non-printable '
  20.     db    'characters', 0
  21. DestinationString    label    byte
  22.     db    200 dup (?)
  23. ;
  24. ; Determines whether a character is printable (in the range
  25. ; 20h through 7Eh).
  26. ;
  27. ; Input:
  28. ;    AL = character to check
  29. ;
  30. ; Output:
  31. ;    Zero flag set to 1 if character is printable,
  32. ;        set to 0 otherwise
  33. ;
  34. ; Registers altered: none
  35. ;
  36. IsPrintable:
  37.     cmp    al,20h
  38.     jb    IsPrintableDone    ;not printable
  39.     cmp    al,7eh
  40.     ja    IsPrintableDone    ;not printable
  41.     cmp    al,al    ;set the Zero flag to 1, since the
  42.             ; character is printable
  43. IsPrintableDone:
  44.     jmp    short IsPrintableReturn
  45.             ;this hardwires IsPrintable to
  46.             ; return to just one place
  47. ;
  48. ; Copies a zero-terminated string to another string,
  49. ; filtering out non-printable characters.
  50. ;
  51. ; Input:
  52. ;    DS:SI = source string
  53. ;    ES:DI = destination string
  54. ;
  55. ; Output: none
  56. ;
  57. ; Registers altered: AL, SI, DI
  58. ;
  59. ; Direction flag cleared
  60. ;
  61. ; Note: Does not handle strings that are longer than 64K
  62. ;    bytes or cross segment boundaries.
  63. ;
  64. CopyPrintable:
  65.     cld
  66. CopyPrintableLoop:
  67.     lodsb            ;get the next byte to copy
  68.     jmp    IsPrintable    ;is it printable?
  69. IsPrintableReturn:
  70.     jnz    NotPrintable    ;nope, don't copy it
  71.     stosb            ;put the byte in the
  72.                      ; destination string
  73.     jmp    CopyPrintableLoop ;the character was
  74.                 ; printable, so it couldn't
  75.                 ; possibly have been 0. No
  76.                 ; need to check whether it
  77.                 ; terminated the string
  78. NotPrintable:
  79.     and    al,al        ;was that the
  80.                 ; terminating zero?
  81.     jnz    CopyPrintableLoop ;no, do next byte
  82.     stosb            ;copy the terminating zero
  83.     ret            ;done
  84. ;
  85. Skip:
  86.     call    ZTimerOn
  87.     mov    di,seg DestinationString
  88.     mov    es,di
  89.     mov    di,offset DestinationString
  90.             ;ES:DI points to the destination
  91.     mov    si,offset SourceString
  92.             ;DS:SI points to the source
  93.     call    CopyPrintable    ;copy the printable
  94.                 ; characters
  95.     call    ZTimerOff
  96.