masm32读取文件函数x86-Windows
我一直在尝试读取写在.txt文件中的字符串,并在控制台上打印出来。但我似乎做得不对。有人能检查一下我的代码并告诉我出了什么问题吗?谢谢masm32读取文件函数x86-Windows,windows,assembly,command-line,x86,masm32,Windows,Assembly,Command Line,X86,Masm32,我一直在尝试读取写在.txt文件中的字符串,并在控制台上打印出来。但我似乎做得不对。有人能检查一下我的代码并告诉我出了什么问题吗?谢谢 include \masm32\include\masm32rt.inc .data txtFilter db "*.txt",0 txtFD WIN32_FIND_DATA <> txtHandle HANDLE ? fHandle HANDLE ? bufferLength db ? buffer db 5000 d
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
代码似乎无法读取我的txt文件中的内容。但是,我可以成功搜索我的txt文件并执行CreateFile功能,有四个问题:
缓冲区长度db?只保留一个字节。ReadFile将在那里存储一个DWORD并覆盖三个字节的缓冲区。如果有空值,StdOut将停止输出。是否将定义更改为bufferLength dd
lnt db 1024,0是一个字符串。ReadFile需要一个DWORD值。将其更改为lnt dd 1024
push FILE_APPEND_数据仅为写入创建句柄。将其更改为“推式读取”
推送偏移lnt传递一个指针。但是,ReadFile需要一个DWORD值。把它改成按lnt
就像这样:
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
调用ReadFile时,您正在推送偏移量lnt,而不仅仅是lnt。nNumberOfBytesToRead参数是按值传递的,而不是按地址传递的。此外,我怀疑您是否必须以ascii字符串形式提供长度lnt。我会试试“lnt dd 1024”