News:

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

Write File in MASM?

Started by breaz, June 07, 2005, 12:42:21 AM

Previous topic - Next topic

breaz

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

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Jeff

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".  :)

breaz

jeff and hutch--,

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

thx.


brixton

Hello,

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

Thanks!
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

AeroASM

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.

brixton

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!
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..