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

  1. ;
  2. ; *** Listing 14-3 ***
  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 a JMP through a
  8. ; register.
  9. ;
  10.     jmp    Skip
  11. ;
  12. SourceString    label    byte
  13.     db    'This is a sample string, consisting of '
  14. X=1
  15.     rept    31
  16.     db    X
  17. X=X+1
  18.     endm
  19.     db    7fh
  20.     db    'both printable and non-printable '
  21.     db    'characters', 0
  22. DestinationString    label    byte
  23.     db    200 dup (?)
  24. ;
  25. ; Determines whether a character is printable (in the range
  26. ; 20h through 7Eh).
  27. ;
  28. ; Input:
  29. ;    AL = character to check
  30. ;    BP = return address
  31. ;
  32. ; Output:
  33. ;    Zero flag set to 1 if character is printable,
  34. ;        set to 0 otherwise
  35. ;
  36. ; Registers altered: none
  37. ;
  38. IsPrintable:
  39.     cmp    al,20h
  40.     jb    IsPrintableDone    ;not printable
  41.     cmp    al,7eh
  42.     ja    IsPrintableDone    ;not printable
  43.     cmp    al,al    ;set the Zero flag to 1, since the
  44.             ; character is printable
  45. IsPrintableDone:
  46.     jmp    bp    ;return to the address in BP
  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, BP
  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.     mov    bp,offset IsPrintableReturn
  67.                 ;set the return address for
  68.                 ; IsPrintable. Note that
  69.                 ; this is done outside the
  70.                 ; loop for speed
  71. CopyPrintableLoop:
  72.     lodsb            ;get the next byte to copy
  73.     jmp    IsPrintable    ;is it printable?
  74. IsPrintableReturn:
  75.     jnz    NotPrintable    ;nope, don't copy it
  76.     stosb            ;put the byte in the
  77.                      ; destination string
  78.     jmp    CopyPrintableLoop ;the character was
  79.                 ; printable, so it couldn't
  80.                 ; possibly have been 0. No
  81.                 ; need to check whether it
  82.                 ; terminated the string
  83. NotPrintable:
  84.     and    al,al        ;was that the
  85.                 ; terminating zero?
  86.     jnz    CopyPrintableLoop ;no, do next byte
  87.     stosb            ;copy the terminating zero
  88.     ret            ;done
  89. ;
  90. Skip:
  91.     call    ZTimerOn
  92.     mov    di,seg DestinationString
  93.     mov    es,di
  94.     mov    di,offset DestinationString
  95.             ;ES:DI points to the destination
  96.     mov    si,offset SourceString
  97.             ;DS:SI points to the source
  98.     call    CopyPrintable    ;copy the printable
  99.                 ; characters
  100.     call    ZTimerOff
  101.