News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

[Help] Write File txt

Started by jeivarmarr, December 02, 2012, 04:25:01 AM

Previous topic - Next topic

jeivarmarr

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:

dedndave

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

jeivarmarr

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?


Gunther

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
You have to know the facts before you can distort them.

jeivarmarr

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?

Gunther

You have to know the facts before you can distort them.

dedndave

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

jj2007

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

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 ....