Reputation: 58
I've been trying to read the string written inside a .txt file and print it out on the console. But it seems I'm not doing it right. Can someone review my codes and tell me what's wrong? Thanks!
include \masm32\include\masm32rt.inc
.data
txtFilter db "*.txt",0
txtFD WIN32_FIND_DATA <>
txtHandle HANDLE ?
fHandle HANDLE ?
bufferLength db ?
buffer db 5000 dup(?)
lnt db "1024",0
okay db "Okay!",0
dokay db "Dokay!",0
.code
start:
push offset txtFD
push offset txtFilter
call FindFirstFile
mov txtHandle, eax
push offset txtFD.cFileName
call StdOut
push 0
push FILE_ATTRIBUTE_NORMAL
push OPEN_EXISTING
push 0
push 0
push FILE_APPEND_DATA
push offset txtFD.cFileName
call CreateFile
.if eax == INVALID_HANDLE_VALUE
jmp _error
.else
mov fHandle, eax
.endif
push 0
push offset bufferLength
push offset lnt
push offset buffer
push fHandle
call ReadFile
jmp _next
_error:
push offset dokay
call StdOut
jmp _next
_okay:
push offset okay
call StdOut
_next:
push offset buffer
call StdOut
push fHandle
call CloseHandle
push txtHandle
call FindClose
push 0
call ExitProcess
end start
The code can't seem to read what is inside my txt file. However I can successfully search my txt file and perform the function CreateFile
Upvotes: 0
Views: 1219
Reputation: 14409
Four issues:
bufferLength db ?
reserves only one byte. ReadFile
will store there a DWORD and overwrite three bytes of buffer
. If there is a NULL, StdOut
will stop the output. Change the definition to bufferLength dd ?
lnt db "1024",0
is a string. ReadFile
expects a DWORD value. Change it to lnt dd 1024
.
push FILE_APPEND_DATA
creates a handle only for writing. Change it to push GENERIC_READ
.
push offset lnt
passes a pointer. However, ReadFile
expects a DWORD value. Change it to push lnt
.
Like that:
include \masm32\include\masm32rt.inc
.data
txtFilter db "*.txt",0
txtFD WIN32_FIND_DATA <>
txtHandle HANDLE ?
fHandle HANDLE ?
; bufferLength db ?
bufferLength dd ?
buffer db 5000 dup(?)
; lnt db "1024",0
lnt dd 1024
okay db "Okay!",0
dokay db "Dokay!",0
.code
start:
push offset txtFD
push offset txtFilter
call FindFirstFile
mov txtHandle, eax
push offset txtFD.cFileName
call StdOut
; https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
push 0 ; HANDLE hTemplateFile
push FILE_ATTRIBUTE_NORMAL ; DWORD dwFlagsAndAttributes
push OPEN_EXISTING ; DWORD dwCreationDisposition
push 0 ; LPSECURITY_ATTRIBUTES lpSecurityAttributes
push 0 ; DWORD dwShareMode
; push FILE_APPEND_DATA ; DWORD dwDesiredAccess
push GENERIC_READ ; DWORD dwDesiredAccess
push offset txtFD.cFileName ; LPCTSTR lpFileName,
call CreateFile
.if eax == INVALID_HANDLE_VALUE
jmp _error
.else
mov fHandle, eax
.endif
; https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx
push 0 ; LPOVERLAPPED lpOverlapped
push offset bufferLength ; LPDWORD lpNumberOfBytesRead
; push offset lnt ; DWORD nNumberOfBytesToRead
push lnt ; DWORD nNumberOfBytesToRead
push offset buffer ; LPVOID lpBuffer
push fHandle ; HANDLE hFile
call ReadFile
jmp _next
_error:
push offset dokay
call StdOut
jmp _next
_okay:
push offset okay
call StdOut
_next:
push offset buffer
call StdOut
push fHandle
call CloseHandle
push txtHandle
call FindClose
push 0
call ExitProcess
end st
Upvotes: 1