The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: breaz on June 07, 2005, 12:42:21 AM

Title: Write File in MASM?
Post by: breaz on June 07, 2005, 12:42:21 AM
INCLUDE Irvine32.inc

.data
buffer BYTE "144",0dh,0ah
bufSize = ($-buffer)
errMsg BYTE "Cannot create file",0dh,0ah,0
filename     BYTE "output.txt",0
fileHandle   DWORD ? ; handle to output file
bytesWritten DWORD ?    ; number of bytes written
temp DWORD ?

.code
main PROC
INVOKE CreateFile,
  ADDR filename, GENERIC_WRITE, DO_NOT_SHARE, NULL,
  OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0

mov fileHandle,eax ; save file handle
.IF eax == INVALID_HANDLE_VALUE
  mov  edx,OFFSET errMsg ; Display error message
  call WriteString
  jmp  QuitNow
.ENDIF

mov eax, 10

INVOKE WriteFile,
    fileHandle,
    ADDR buffer,
    bufsize,
    ADDR bytesWritten,
    0

INVOKE CloseHandle, fileHandle

QuitNow:
INVOKE ExitProcess,0
exit
main ENDP
END main

========================================

I want to how to write EAX's value ( = 10 ) to "output.txt"
this code is impossible. eax is DWORD, buffer is BYTE. so impossible...
can i converting EAX (DWORD) to BYTE? OR teach me how to write decimal to file.

I try this code..

mov eax, 97
mov temp, eax

INVOKE WriteFile,
    fileHandle,
    ADDR temp,
    bufsize,
    ADDR bytesWritten,
    0

But, this code write ASCII VALUE to output.txt
ex, 97->a / 98->b . I want write 97..Not a...

sorry, my english is not good.. :(
Title: Re: Write File in MASM?
Post by: hutch-- on June 07, 2005, 12:44:40 AM
breaz,

You would have to see what library support is available with Kip Irvine's package. We could show you how to do it using the MASM32 library but that may not be that much use to you if you are using Kip's system.
Title: Re: Write File in MASM?
Post by: Jeff on June 07, 2005, 01:39:38 AM
breaz,
you need to make adjustments to your writefile call:
mov eax, 97
mov temp, eax

INVOKE WriteFile,
    fileHandle,
    ADDR temp,
    4        ;size of your "buffer" (temp)
    ADDR bytesWritten,
    0


just be aware that what will be written will be the "raw" hex value, not its string representation.

converting to a string will take a bit more work.  have a look at irvine's "WriteDec" procedure to see how to convert a decimal number to a string.  look for the code on his site.

ps:  the end of your code doesnt need both:
INVOKE ExitProcess,0
exit

either one will do since "exit" is an alias to "INVOKE ExitProcess,0".  :)
Title: Re: Write File in MASM?
Post by: breaz on June 07, 2005, 02:02:38 AM
jeff and hutch--,

thx. I see 'WriteDec PROC' in IRVINE32.asm.
I think I can use this Proc.

thx.

Title: Re: Write File in MASM?
Post by: brixton on June 07, 2005, 12:42:23 PM
Hello,

How does one convert a decimal number to a string in MASM32 ?
Also, would you need a temp buffer of type DD ?

Thanks!
Title: Re: Write File in MASM?
Post by: AeroASM on June 07, 2005, 12:55:59 PM
Quote from: brixton on June 07, 2005, 12:42:23 PM
Hello,

How does one convert a decimal number to a string in MASM32 ?
Also, would you need a temp buffer of type DD ?

Thanks!

Remember: in assembly variables have no types, only sizes. Therefore you can use db, dd, dq, df or whatever. However the convention for defining string buffers is db for ASCII and dw for Unicode.

hex to string is OK, but decimal to string is harder. Try the dwtoa proc in masm32.lib.
Title: Re: Write File in MASM?
Post by: brixton on June 07, 2005, 12:59:13 PM
AeroASM,

Quote from: AeroASMRemember: in assembly variables have no types, only sizes.
Thanks, this slipped my mind!  It's a different convention to how many programming languages define a variable.

QuoteHowever the convention for defining string buffers is db for ASCII and dw for Unicode.
At least I knew this one  :bg

Quotehex to string is OK, but decimal to string is harder. Try the dwtoa proc in masm32.lib.
Thanks for the info!