在汇编中读取文件

时间:2011-12-28 00:45:59

标签: assembly masm irvine32

第一: 程序集中的代码无法打开我输入其名称的文件

第二: 我不知道如何将此代码合并到我使用它来打开文件的代码

TITLE Reading a File (ReadFile.asm)
                                    ; Opens, reads, and displays a text file using
                                    ; procedures from Irvine32.lib.
INCLUDE Irvine32.inc
INCLUDE macros.inc
BUFFER_SIZE = 5000
.data
buffer BYTE BUFFER_SIZE DUP(?)
filename BYTE 80 DUP(0)
fileHandle HANDLE ?
.code
main PROC
                                           ; Let user input a filename.
mWrite "Enter an input filename: "
mov edx,OFFSET filename
mov ecx,SIZEOF filename
call ReadString
                                           ; Open the file for input.
mov edx,OFFSET filename
call OpenInputFile
mov fileHandle,eax
                                            ; Check for errors.
cmp eax,INVALID_HANDLE_VALUE                ; error opening file?
jne file_ok                                  ; no: skip
mWrite <"Cannot open file",0dh,0ah>
jmp quit                                     ; and quit
file_ok:
                                             ; Read the file into a buffer.
mov edx,OFFSET buffer
mov ecx,BUFFER_SIZE
call ReadFromFile
jnc check_buffer_size                        ; error reading?
mWrite "Error reading file. "                ; yes: show error message
call WriteWindowsMsg
jmp close_file
check_buffer_size:
cmp eax,BUFFER_SIZE                          ; buffer large enough?
jb buf_size_ok ; yes
mWrite <"Error: Buffer too small for the file",0dh,0ah>
jmp quit                                     ; and quit
buf_size_ok:
mov buffer[eax],0                            ; insert null terminator
mWrite "File size: "
call WriteDec                                ; display file size
call Crlf
                                             ; Display the buffer.
mWrite <"Buffer:",0dh,0ah,0dh,0ah>
mov edx,OFFSET buffer                        ; display the buffer
call WriteString
call Crlf
close_file:
mov eax,fileHandle
call CloseFile
quit:
exit
main ENDP
END main

1 个答案:

答案 0 :(得分:0)

如果您有一个要从驱动器c读取的文件,例如文件名应包含驱动器名称: filename必须是“c:\ anyFileName.txt”