The MASM Forum

General => The Campus => Topic started by: jeivarmarr on December 02, 2012, 04:25:01 AM

Title: [Help] Write File txt
Post by: jeivarmarr on December 02, 2012, 04:25:01 AM
Hi I would like to help me.

I have a hexadecimal string and want to write in a txt file.

could help me please.

:redface:
Title: Re: [Help] Write File txt
Post by: dedndave on December 02, 2012, 04:45:24 AM
1) create the file
2) write the string to the file
3) close the file

        INCLUDE \masm32\include\masm32rt.inc

        .DATA

szFileName db 'MyFile.txt',0

        .DATA?

hFile  HANDLE ?
nBytes dw ?

        .CODE

_main   PROC

        mov     eax,12345678h
        mov     edx,uhex$(eax)     ;EDX = address of hexidecimal string

;create the file

        push    edx
        INVOKE  CreateFile,offset szFileName,GENERIC_WRITE,FILE_SHARE_READ,
                NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        mov     hFile,eax
        pop     edx

;write the file

        INVOKE  WriteFile,eax,edx,8,offset nBytes,NULL

;close the file

        INVOKE  CloseHandle,hFile

;terminate

        INVOKE  ExitProcess,0

_main   ENDP

        END     _main
Title: Re: [Help] Write File txt
Post by: jeivarmarr on December 02, 2012, 06:51:26 AM
Quote from: dedndave on December 02, 2012, 04:45:24 AM
1) create the file
2) write the string to the file
3) close the file

        INCLUDE \masm32\include\masm32rt.inc

        .DATA

szFileName db 'MyFile.txt',0

        .DATA?

hFile  HANDLE ?
nBytes dw ?

        .CODE

_main   PROC

        mov     eax,12345678h
        mov     edx,uhex$(eax)     ;EDX = address of hexidecimal string

;create the file

        push    edx
        INVOKE  CreateFile,offset szFileName,GENERIC_WRITE,FILE_SHARE_READ,
                NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        mov     hFile,eax
        pop     edx

;write the file

        INVOKE  WriteFile,eax,edx,8,offset nBytes,NULL

;close the file

        INVOKE  CloseHandle,hFile

;terminate

        INVOKE  ExitProcess,0

_main   ENDP

        END     _main


thanks worked for me, but let's say I have two hexadecimal?

eg

mov eax, 12345678h
mov eax, 90123456h

how do I print the two in one line?

Title: Re: [Help] Write File txt
Post by: Gunther on December 02, 2012, 07:00:15 AM
Hi jeivarmarr,

Quote from: jeivarmarr on December 02, 2012, 06:51:26 AM
thanks worked for me, but let's say I have two hexadecimal?

eg

mov eax, 12345678h
mov eax, 90123456h

how do I print the two in one line?

convert both numbers into ASCII strings, concatenate both strings and print the new string out.

Gunther
Title: Re: [Help] Write File txt
Post by: jeivarmarr on December 02, 2012, 07:17:39 AM
Quote from: Gunther on December 02, 2012, 07:00:15 AM
Hi jeivarmarr,

Quote from: jeivarmarr on December 02, 2012, 06:51:26 AM
thanks worked for me, but let's say I have two hexadecimal?

eg

mov eax, 12345678h
mov eax, 90123456h

how do I print the two in one line?

convert both numbers into ASCII strings, concatenate both strings and print the new string out.

Gunther

as convert?
Title: Re: [Help] Write File txt
Post by: Gunther on December 02, 2012, 08:45:17 AM
Quote from: jeivarmarr on December 02, 2012, 07:17:39 AM
as convert?

You can only print ASCII characters.

Gunther
Title: Re: [Help] Write File txt
Post by: dedndave on December 02, 2012, 09:57:04 AM
in my example, i used the "hex$" macro to do the conversion
it always accepts a dword and returns the address of an 8-byte null-terminated string
you can find the hex$ macro in \masm32\macros\macros.asm
when you look at that macro, you will see that it uses the masm32 "dw2hex" routine
that code may be found in \masm32\m32lib\dw2hex.asm

now - as mentioned, you can concatonate 2 strings
or - you can use that routine to build 2 hex strings
the routine terminates the 8-byte string with a null byte
so - if you do the high-order dword first, when you do the low-order one, it will overwrite the first null byte

in this example, EDX:EAX contains a 64-bit value...

        INCLUDE  \masm32\include\masm32rt.inc

        .DATA?

szHexBuf  db 17 dup(?)

        .CODE

        mov     eax,12345678h   ;EAX = low-order dword
        mov     edx,90123456h   ;EDX = high-order dword

        push    eax
        INVOKE  dw2hex,edx,offset szHexBuf
        pop     eax
        INVOKE  dw2hex,eax,offset szHexBuf+8
;
;
;now, you may write 16 characters to the file at once


another way to go is to use the hex$ macro as before
only - convert and write 8 bytes at a time
once you have written the high-order dword to the file
repeat the hex$ + WriteFile again using the low-order dword

WriteFile maintains a file pointer that is updated each time you write to the file
Title: Re: [Help] Write File txt
Post by: jj2007 on December 02, 2012, 10:12:00 AM
Quote from: jeivarmarr on December 02, 2012, 04:25:01 AM
I have a hexadecimal string and want to write in a txt file.

So you basically want a hexdump...:

include \masm32\MasmBasic\MasmBasic.inc   ; download (http://masm32.com/board/index.php?topic=94.0)

Src   db "aaaaaaa.... this is just some string in memory that we would like to convert into hex values", 0

   Init
   mov esi, offset Src
   Open "O", #1, "MyHexDump.txt"
   xor ecx, ecx
   .Repeat
      lodsd   ; get 4 bytes from memory and advance the pointer in esi
      ; bswap eax   ; remove the comment to test the order
      Print #1, Hex$(eax), " "
      inc ecx
   .Until ecx>=sizeof Src/4
   Close #1
   ; now read the file into a buffer and show it:
   Inkey FileRead$("MyHexDump.txt")
   Exit
end start


Output:
- without bswap:
61616161 2E616161 202E2E2E 73696874 20736920 7473756A 6D6F7320 74732065 ....

- with bswap:
61616161 6161612E 2E2E2E20 74686973 20697320 6A757374 20736F6D 65207374 ....