I want to send the number 5 to my procedure and write it to my file. What do i do wrong?
.386
include \masm32\include\masm32rt.inc
option casemap :none
openfile PROTO :DWORD
.data
number DWORD 5
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
call main ; branch to the "main" procedure
invoke openfile, number
invoke ExitProcess,eax ;Close everything
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
openfile proc text:DWORD
LOCAL hFile :DWORD ; file handle
LOCAL bwrt :DWORD ; variable for bytes written
LOCAL flco :DWORD ; file pointer
LOCAL txt :DWORD ; text handle
LOCAL new :DWORD ; new line
sas new, 13, 10
mov ecx, text
mov hFile, fopen("testfile.txt")
mov flco, fseek(hFile,0,FILE_END)
mov bwrt, fwrite(hFile,ecx,len(ecx)) ;How do i write the number 5 here?
mov bwrt, fwrite(hFile,new,len(new))
fclose hFile ; It is good programming style to close a file as soon as possible!
ret
openfile endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends
hi Chicken:
I don't think your fopen is working.
Also you have to mov hfile, eax
if it does work!
Also I suspect the file attributes are not right.
Regards herge
Hi Chicken:
; DUMMY.ASM
.386
include \masm32\include\masm32rt.inc
option casemap :none
openfile PROTO :DWORD
.data
number DWORD 5
filename db "filename.txt",0
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
call main ; branch to the "main" procedure
invoke openfile, number
invoke ExitProcess,eax ;Close everything
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
openfile proc text:DWORD
LOCAL hFile :DWORD ; file handle
LOCAL bwrt :DWORD ; variable for bytes written
LOCAL flco :DWORD ; file pointer
LOCAL txt :DWORD ; text handle
LOCAL new :DWORD ; new line
sas new, 13, 10
mov ecx, text
invoke CreateFile,ADDR filename,GENERIC_WRITE,\
0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,0
mov hFile,eax
mov flco, fseek(hFile,0,FILE_END)
mov bwrt, fwrite(hFile,ecx,len(ecx)) ;How do i write the number 5 here?
mov bwrt, fwrite(hFile,new,len(new))
fclose hFile ; It is good programming style to close a file as soon as possible!
ret
openfile endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start
.
Also you are writing biniary 5 and not ASCII 5
It will be hard too read!
Regards herge
Thank you for your help. Now i can print the number 5 in my textfile.
Why do i need to mov hfile, eax? And where?
.386
include \masm32\include\masm32rt.inc
option casemap :none
openfile PROTO :DWORD
.data
number DWORD 5
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
call main ; branch to the "main" procedure
invoke openfile, number
invoke ExitProcess,eax ;Close everything
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
openfile proc text:DWORD
LOCAL hFile :DWORD ; file handle
LOCAL bwrt :DWORD ; variable for bytes written
LOCAL flco :DWORD ; file pointer
LOCAL new :DWORD ; new line
LOCAL buffer :DWORD
invoke dwtoa ,text, addr buffer ;Make it ascii
mov hFile, fopen("testfile.txt")
mov flco, fseek(hFile,0,FILE_END)
mov bwrt, fwrite(hFile,addr buffer,len(addr buffer)) ; Now i do write the number 5 here!
sas new, 13, 10
mov bwrt, fwrite(hFile,new,len(new))
fclose hFile ; It is good programming style to close a file as soon as possible!
ret
openfile endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends
Quote from: Chicken on November 24, 2008, 08:33:37 PM
Thank you for your help. Now i can print the number 5 in my textfile.
Why do i need to mov hfile, eax? And where?
.386
include \masm32\include\masm32rt.inc
option casemap :none
openfile PROTO :DWORD
.data
number DWORD 5
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
call main ; branch to the "main" procedure
invoke openfile, number
invoke ExitProcess,eax ;Close everything
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
openfile proc text:DWORD
LOCAL hFile :DWORD ; file handle
LOCAL bwrt :DWORD ; variable for bytes written
LOCAL flco :DWORD ; file pointer
LOCAL new :DWORD ; new line
LOCAL buffer :DWORD
invoke dwtoa ,text, addr buffer ;Make it ascii
mov hFile, fopen("testfile.txt")
mov flco, fseek(hFile,0,FILE_END)
mov bwrt, fwrite(hFile,addr buffer,len(addr buffer)) ; Now i do write the number 5 here!
sas new, 13, 10
mov bwrt, fwrite(hFile,new,len(new))
fclose hFile ; It is good programming style to close a file as soon as possible!
ret
openfile endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends
You have to move eax to hFile because hFile is the handle of the file you are opening. hFile is a structure of information of your file, and you will need this to know how many bytes the file is and many thing.
You have to move the handle to a variable on every call to fOpen function.
Quote from: Farabi on November 25, 2008, 01:59:28 AM
Quote from: Chicken on November 24, 2008, 08:33:37 PM
Thank you for your help. Now i can print the number 5 in my textfile.
Why do i need to mov hfile, eax? And where?
.386
include \masm32\include\masm32rt.inc
option casemap :none
openfile PROTO :DWORD
.data
number DWORD 5
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
call main ; branch to the "main" procedure
invoke openfile, number
invoke ExitProcess,eax ;Close everything
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
openfile proc text:DWORD
LOCAL hFile :DWORD ; file handle
LOCAL bwrt :DWORD ; variable for bytes written
LOCAL flco :DWORD ; file pointer
LOCAL new :DWORD ; new line
LOCAL buffer :DWORD
invoke dwtoa ,text, addr buffer ;Make it ascii
mov hFile, fopen("testfile.txt")
mov flco, fseek(hFile,0,FILE_END)
mov bwrt, fwrite(hFile,addr buffer,len(addr buffer)) ; Now i do write the number 5 here!
sas new, 13, 10
mov bwrt, fwrite(hFile,new,len(new))
fclose hFile ; It is good programming style to close a file as soon as possible!
ret
openfile endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends
You have to move eax to hFile because eax is the handle of the file you are opening. hFile is a structure of information of your file, and you will need this to know how many bytes the file is and many thing.
You have to move the handle to a variable on every call to fOpen function.
Folks, you are on the wrong track:
mov hFile, fopen("testfile.txt")
First, there is nothing wrong with using the function version to assign the handle.
Second, there is something wrong with fopen: It works with existing files only.
What you need is fcreate. See below, excerpt from \masm32\macros\macros.asm
fopen MACRO filename
invoke CreateFile,reparg(filename),GENERIC_READ or GENERIC_WRITE,
NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
EXITM <eax> ;; return file handle
ENDM
fcreate MACRO filename
invoke CreateFile,reparg(filename),GENERIC_READ or GENERIC_WRITE,
NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
EXITM <eax> ;; return file handle
ENDM
Now i check if the file exist, otherwise i create it.
.386
include \masm32\include\masm32rt.inc
option casemap :none
openfile PROTO :DWORD
.data
number DWORD 5
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
call main ; branch to the "main" procedure
invoke openfile, number
invoke ExitProcess,eax ;Close everything
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
openfile proc text:DWORD
LOCAL hFile :DWORD ; file handle
LOCAL bwrt :DWORD ; variable for bytes written
LOCAL flco :DWORD ; file pointer
LOCAL new :DWORD ; new line
LOCAL buffer :DWORD
LOCAL fname :DWORD
invoke dwtoa ,text, addr buffer ;Make it ascii
sas fname, "testfile.txt"
.if rv(exist,fname) != 0 ; test if file exists
mov hFile, fopen(fname) ; open it if it does
.else
mov hFile, fcreate(fname) ; otherwise create a new file
.endif
mov flco, fseek(hFile,0,FILE_END)
mov bwrt, fwrite(hFile,addr buffer,len(addr buffer)) ; Now i do write the number 5 here!
sas new, 13, 10
mov bwrt, fwrite(hFile,new,len(new))
fclose hFile ; It is good programming style to close a file as soon as possible!
ret
openfile endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends
Hi Chicken:
LOCAL buffer[65] :BYTE
Always make the buffer too big! a DWORD is only eight bytes and
remember a byte for the NULL or zero byte at the end.
One day you are going to use more than eight bytes and you
are going over write either some code or data. If it's code
your program is going to Bang or the results will be trashed.
Regards herge
Quote from: Chicken on November 25, 2008, 02:53:01 PM
Now i check if the file exist, otherwise i create it.
Not necessary. You write to the file, so you can as well create it afresh. It would be different if you appended a string to an existing file: fopen, fseek, fwrite, fclose.
Quote from: herge on November 25, 2008, 06:35:27 PM
Hi Chicken:
LOCAL buffer[65] :BYTE
Always make the buffer too big! a DWORD is only eight bytes and
remember a byte for the NULL or zero byte at the end.
An even better strategy is to
know, at least roughly, how much buffer you need. Odd numbers such as 65 are not really recommended, but check in Olly how the assembler handles them. Last but not least, a DWORD is four bytes, not eight.