The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: hutch-- on July 31, 2009, 02:27:13 PM

Title: Unix, MAC text to standard PC CRLF algo.
Post by: hutch-- on July 31, 2009, 02:27:13 PM
I thought someone may like this, any combination of CR and LF to standard PC CRLF. You can probably make it faster but it seems to run OK. One memory read per iteration and does the compares as DWORD.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

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

    any2crlf PROTO :DWORD,:DWORD

    .data
      item db "line 1",13,10,10,10
           db "line 2",13
           db "line 3",10
           db "line 4",10,10,0

      pInp dd item                  ; DWORD PTR to "item".

    .code

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

    call main
    inkey
    exit

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

main proc

    LOCAL void  :DWORD
    LOCAL pBuf  :DWORD
    LOCAL buffer[256]:BYTE

    mov pBuf, ptr$(buffer)          ; set buffer address to pointer

    invoke any2crlf,pInp,pBuf       ; call the procedure

    print pBuf,13,10                ; display result to screen

  ; --------------------------------------------
  ; write result as file to check CRLF placement
  ; --------------------------------------------
    mov void, OutputFile("result.txt",pBuf,len(pBuf))

    ret

main endp

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

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

    align 4

any2crlf proc pSrc:DWORD,pDst:DWORD

    mov ecx, [esp+4]                ; source address in ECX
    mov edx, [esp+8]                ; destination address in EDX
    jmp mainloop                    ; jump to main loop

  writebyte:
    mov [edx], al                   ; write byte in AL to address in EDX
    add edx, 1
    add ecx, 1

  mainloop:
    movzx eax, BYTE PTR [ecx]       ; zero extend byte into EAX
    test eax, eax                   ; exit on zero
    jz outa_here
    cmp eax, 13                     ; test for 13
    je subloop
    cmp eax, 10                     ; test for 10
    jne writebyte                   ; write byte if not 0, 10, 13

  subloop:
    add ecx, 1
    movzx eax, BYTE PTR [ecx]       ; zero extend byte into EAX
    test eax, eax                   ; exit on zero
    jz outa_here
    cmp eax, 13                     ; loop back on 13
    je subloop
    cmp eax, 10                     ; loop back on 10
    je subloop

    mov WORD PTR [edx], 0A0Dh       ; write CRLF to output buffer
    add edx, 2
    jmp mainloop

  outa_here:
    mov WORD PTR [edx], 0A0Dh       ; write CRLF to output buffer
    add edx, 2
    mov BYTE PTR [edx], 0           ; terminate string

    ret 8

any2crlf endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

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

end start
Title: Re: Unix, MAC text to standard PC CRLF algo.
Post by: herge on August 02, 2009, 08:35:49 AM
 Hi Hutch:

It works for me!

C:\masm32\bin>debug result.txt
-d 100 22
         ^ Error
-d 100 22h
         ^ Error
-d 100
1538:0100  6C 69 6E 65 20 31 0D 0A-6C 69 6E 65 20 32 0D 0A   line 1..line 2..
1538:0110  6C 69 6E 65 20 33 0D 0A-6C 69 6E 65 20 34 0D 0A   line 3..line 4..
1538:0120  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00   ................
1538:0130  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00   ................
1538:0140  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00   ................
1538:0150  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00   ................
1538:0160  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00   ................
1538:0170  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00   ................
-q

C:\masm32\bin>result       << Hit Tab after you type t >>



Have not used Debug in years.

PS from a Dos prompt Hit tab if you can not
remember your File extension, Dos will get it
right away.


C:\masm32\bin>result

Hit Tab after you Type result

Regards herge