News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Replacing a Null Character

Started by myavo, February 28, 2010, 08:45:25 PM

Previous topic - Next topic

myavo

I am a MASM32 newbie who has not written an assembly program in 40 years. So, I thought this might be a good opportunity
to jump back into it. I currently have MS MASM 6.11 installed an running on one of my machines. I have a real world problem that
I need to solve.
Currently, I receive large text files every morning. They are about 50-60 meg each. From time to time one or more of these files will contain a hex"00" in one of the char. This null char terminates my processing of the file. This is what I would like to do.
      - Create a program that accepts the the path and Input file name as an argument.
      - Read the file, examine each line looking for the hex"00". If no null is found print a message to the console stating such.
          If a null is found read the file again and write the data to a new file replacing the null with a blank. Print each
          affected line to the console replacing the null char with a Tilda. Rename the new file back to the original file name.       

This is probably a little ambitious for someone in my position, but any suggestions or code examples would be appreciated.

Thanks.

hutch--

In the simple sense you set up a byte scanner that tests each byte and if it is an ascii zero you write a different byte to it, this can be just a space or perhaps a CR LF pair if you are writing from one buffer to another.

Here is a quick test piece that replaces the zeros.


IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    zrep PROTO :DWORD,:DWORD

    .data?
      value dd ?

    .data
      txt db "1234",0,"5678",0,"9012",0,"3456",0

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    invoke zrep,OFFSET txt,LENGTHOF txt-1

    print OFFSET txt,13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

zrep proc ptxt:DWORD,ltxt:DWORD

    mov edx, ptxt
    or ecx, -1

  label1:
    add ecx, 1
    cmp BYTE PTR [edx+ecx], 0
    jne nxt
    mov BYTE PTR [edx+ecx], 32
  nxt:
    cmp ecx, ltxt
    jne label1

    ret

zrep endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

qWord

in the attachment an quick implementation using file mapping - the program simply replace all zeros with blanks. (SSE2 is only used, because it so easy to implement  :P)
FPU in a trice: SmplMath
It's that simple!

hutch--

Here is an unrolled version of the earlier one, should be a bit faster.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

zrep proc ptxt:DWORD,ltxt:DWORD

    mov edx, [esp+4]        ; ptxt
    mov ecx, [esp+8]        ; ltxt

    add edx, ecx
    neg ecx
    jmp label0

  quit:
    ret 8

  align 4
  pre:
    mov BYTE PTR [edx+ecx], 32
    add ecx, 1
    jz quit

  align 4
  label0:
    cmp BYTE PTR [edx+ecx], 0
    je pre
    add ecx, 1
    jz quit

    cmp BYTE PTR [edx+ecx], 0
    je pre
    add ecx, 1
    jz quit

    cmp BYTE PTR [edx+ecx], 0
    je pre
    add ecx, 1
    jz quit

    cmp BYTE PTR [edx+ecx], 0
    je pre
    add ecx, 1
    jnz label0

    ret 8

zrep endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php