The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: hutch-- on May 08, 2005, 01:37:43 PM

Title: Duplicate space trimmer.
Post by: hutch-- on May 08, 2005, 01:37:43 PM
I saw a question today about removing duplicate spaces from a zero terminated string. Her is an algo that ovewrites the original string with the result. It is in a console mode test piece.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    sptrim PROTO :DWORD

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main

    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    LOCAL tststr    :DWORD

    sas tststr,"    this    is      a    test     "

    print tststr,13,10

    invoke sptrim,tststr

    print tststr,13,10

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

align 4

sptrim proc src:DWORD

    mov ecx, [esp+4]
    xor eax, eax
    sub ecx, 1
    mov edx, [esp+4]

  align 4
  stlp:
    add ecx, 1
    mov al, [ecx]
    cmp al, 32
    jne @F
    cmp BYTE PTR [ecx+1], 32
    je overit
  @@:
    mov [edx], al
    add edx, 1
  overit:
    test al, al
    jnz stlp

    mov eax, [esp+4]

    ret 4

sptrim endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
Title: Re: Duplicate space trimmer.
Post by: hutch-- on May 11, 2005, 01:44:39 PM
Here is a more useful variation, a white space duplicate remover that replaces tabs with spaces and removes duplicate spaces.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

align 4

wsptrim proc src:DWORD

  ; ---------------------------------------------------------------
  ; remove any white space duplicates and substitute a single space
  ; ---------------------------------------------------------------
    mov ecx, [esp+4]
    xor eax, eax
    sub ecx, 1
    mov edx, [esp+4]

  align 4
  stlp:
    add ecx, 1
    mov al, [ecx]
    cmp al, 9
    jne @F
    mov al, 32                      ; replace tabs with spaces
  @@:
    cmp al, 32
    jne @F
    cmp BYTE PTR [ecx+1], 32        ; test for next space
    je overit
    cmp BYTE PTR [ecx+1], 9         ; test for next tab
    je overit
  @@:
    mov [edx], al
    add edx, 1
  overit:
    test al, al                     ; test for zero AFTER its written.
    jnz stlp

    mov eax, [esp+4]

    ret 4

wsptrim endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««