home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TOOLS4.ZIP / TRIM.ASM < prev    next >
Encoding:
Assembly Source File  |  1988-07-07  |  5.5 KB  |  133 lines

  1. ;****************************************************************
  2. ; function Trim(S : String) : String
  3. ;
  4. ; Returns string with leading and trailing white space removed
  5. ; from S.  White space is considered to be a space ' ' (ascii
  6. ; code 20h or 32 decimal), and all characters less than a
  7. ; space.  In other words characters with ascii codes 0..32 are
  8. ; whitespace.  If a line of all white space is passed as S,
  9. ; then an empty string '' will be returned.
  10. ;
  11. ; ex.
  12. ; Trim('  Hello there  ') would return 'Hello there'
  13. ; Trim(#9#9'if C > 0 then'#9) would return 'if C > 0 then'
  14. ;
  15. ; by Richard S. Sadowsky
  16. ; version .5   7/7/88
  17. ; Copyright (c) 1988, Richard S. Sadowsky
  18. ; Los Angeles, California
  19. ;
  20. ;****************************************************************
  21.  
  22.  
  23. CODE      SEGMENT BYTE PUBLIC
  24.           ASSUME CS:CODE
  25.  
  26.           PUBLIC Trim
  27.  
  28. ;****************************************************************
  29. ; Some equates to refer to the function result, and the function's
  30. ; string parameter.
  31. ;****************************************************************
  32.  
  33. TrimResult       EQU   DWORD PTR [BP+0Ah]
  34. TrimStr          EQU   DWORD PTR [BP+06h]
  35.  
  36. Trim             PROC FAR
  37.        PUSH      BP                ; save BP
  38.        MOV       BP,SP             ; set up base pointer
  39.        PUSH      DS                ; save DS, we use it
  40.        LDS       SI,TrimStr        ; source str is function param
  41.        LES       DI,TrimResult     ; dest str is function result
  42.        CLD                         ; foward string ops
  43.        XOR       BX,BX             ; use BX as counter
  44.        LODSB                       ; get the length byte
  45.        STOSB                       ; copy it to dest str
  46.        XOR       CH,CH             ; zero high byte of CX
  47.        MOV       CL,AL             ; mov str length into CL
  48.        JCXZ      ExitCode          ; all done is length is 0
  49.  
  50. ;****************************************************************
  51. ; First we search forward for white space.  We continue looking
  52. ; until the first non white space character is found, or we
  53. ; have searched the whole string.  When a non white space
  54. ; character is found, we fall into the NotWhiteSpace loop
  55. ; below.
  56. ;****************************************************************
  57.  
  58. Leading:
  59.        LODSB                       ; get next byte
  60.        CMP       AL,' '            ; Is it white space?
  61.        JG        NotWhiteSpace     ; if not jump to NotWhiteSpace
  62.        LOOP      Leading           ; repeat for next char
  63.        JMP       SHORT AdjLength   ; done with string
  64.  
  65. ;****************************************************************
  66. ;  The follwoing code simply copies however many characters
  67. ;  are left in the string to the destination.  BL is used
  68. ;  to count the characters copied.  As soon as the first
  69. ;  Non whitespace character is encountered in the string
  70. ;  we fall into this Loop.
  71. ;****************************************************************
  72.  
  73. NotWhiteSpace:
  74.        STOSB                       ; copy the byte to dest
  75.        INC       BL                ; inc our char count
  76.        LODSB                       ; get next char in AL
  77.        LOOP      NotWhiteSpace     ; loop while CX <> 0
  78.  
  79. ;****************************************************************
  80. ; Not we adjust the length byte of the destination str to the
  81. ; number of characters we copied in the NotWhiteSpace loop.
  82. ;****************************************************************
  83.  
  84. AdjLength:
  85.        LES       DI,TrimResult     ; point to length byte in dest
  86.        MOV       ES:[DI],BL        ; mov in new length
  87.  
  88. ;****************************************************************
  89. ; Now we point to the last byte of the destination string and
  90. ; set the direction flag (backward string ops).  We then see how
  91. ; many trailing white space characters there are, and subtract
  92. ; this number from the length byte.
  93. ;****************************************************************
  94.  
  95. Trailing:
  96.        XOR       CH,CH             ; zero high byte of CX
  97.        MOV       CL,BL             ; length of dest string
  98.        JCXZ      ExitCode          ; if zero we're done
  99.  
  100.        XOR       DX,DX             ; we use DX as a counter
  101.        LDS       SI,TrimResult     ; Point to TrimRes
  102.        ADD       SI,BX             ; point to last char in TrimRes
  103.        STD                         ; backward string ops
  104.  
  105. Trail: LODSB                       ; Get byte in AL
  106.        CMP       AL,' '            ; is it whitespace?
  107.        JA        AdjLength2        ; if not, then adjust length
  108.        INC       DL                ; otherwise inc our counter
  109.        LOOP      Trail             ; repeat for next char
  110.  
  111. ;****************************************************************
  112. ; Now we adjust the length byte of the function result to the
  113. ; number of characters after trailing white space is removed.
  114. ; BL contains the length of the function result and DL contains
  115. ; the number of trailing white space characters to be removed.
  116. ;****************************************************************
  117.  
  118. AdjLength2:
  119.        OR        DL,DL             ; is DL zero?
  120.        JZ        ExitCode          ; yes, so exit
  121.        SUB       BL,DL             ; subtract DL from BL
  122.        LES       DI,TrimResult     ; point to length of TrimRes
  123.        MOV       ES:[DI],BL        ; mov in new length
  124.  
  125. ExitCode:
  126.        POP       DS                ; restore DS (or else)
  127.        POP       BP                ; restore BP
  128.        RET       04h               ; pop parameters off stack
  129. Trim             ENDP
  130.  
  131. CODE     ENDS
  132.          END
  133.