I try to write number to file via Assembler
Include Irvine32.inc
.data
fileName DB 'number.txt',0
FileHandle DD ?
numberBytes DD ?
number DD 101
numberChar DD $-cislo
.code
WriteToFileP proc
;create file
push NULL
push FILE_ATTRIBUTE_NORMAL
push CREATE_ALWAYS
push NULL
push 0
push GENERIC_WRITE
push offset fileName
call CreateFileA
mov FileHandle,eax
;write
push NULL
push offset numberBytes
push numberChar
push number
push FileHandle
call WriteFile
; close file
push FileHandle
call CloseHandle
ret
WriteToFileP ENDP
end
It doesn´t work. I have tried to change push number
for push offset number
but it doesnt´work too.Any idea how to write number to file?
The assembler had converted the decimal "101" in the line
number DD 101
to the internal CPU format "dword".WriteFile
writes that as a sequence of bytes to the file and an editor would show crap, since it interprets it as a sequence of characters. First the dword has to be converted to a string.The WinApi contains a function which can be used as conversion routine:
wsprintf
. Since the Irvine32 library declares this function, it can be used without further circumstances:Your code has a couple of problems, it doesn't check for errors, and if you want to write a number in "text format", you need to make it a string, like this:
number DB '101',0
You should be able to fix your code, you're almost there. Here's some code I wrote a long time ago if you want to take a look. It's MASM32 asm and makes use of some macros.
invoke
is just like pushing and calling by hand, but in one line.