【发布时间】:2014-01-22 11:05:00
【问题描述】:
我正在尝试从文件中读取一些文本并将其打印到控制台,但我根本无法让它工作。它不打印任何东西... (我的文件只有 5 个文字) CreateFile 调用似乎工作正常。 ReadFile 调用返回 0,所以这可能就是问题所在。
任何帮助将不胜感激。
global _main
extern _printf
extern _ReadFile@20
extern _WriteFile@20
extern _GetStdHandle@4
extern _CreateFileA@28
extern _ExitProcess@4
section .data
fmt: db '%d', 0Dh, 0Ah, 0 ; newline and NULL terminated
access: dd 0x10000000
filename: db 'ThisIsATestFile.txt', 0
buflen: dd 2048 ; Size of our buffer to be used for read
dwBytesRead:dw 0
section .bss
buffer: resb 2048 ; A 2 KB byte buffer used for read
section .text
_main:
; CreateFile@28 - OpenFile
xor eax, eax
push eax ; 7 param - Handle
push 128 ; 6 param - FILE_ATTRIBUTE_NORMAL
push 4 ; 5 param - OPEN_ALWAYS
push eax ; 4 param - Default security
push eax ; 3 param - Do not share
push DWORD [access] ; 2 param - Generic All
push filename ; 1 param - FileName
call _CreateFileA@28
mov ebx, eax ; copy the handle into ebx
; ReadFile@20
xor eax, eax
push eax
push dwBytesRead
push buflen
push buffer
push ebx ; handle
call _ReadFile@20
mov esi, eax ; get the amount of bytes read
; GetHandle - Output
push -11
call _GetStdHandle@4
mov ebx, eax ; copy the handle into ebx
; WriteFile@20
xor eax, eax
push eax
push eax
push esi
push buffer
push ebx
call _WriteFile@20
; ExitProcess(0)
push 0
call _ExitProcess@4
非常感谢路德的帮助。奇迹般有效 :) 我最终用 ReadFile 和 WriteFile 这样做了。
; ReadFile@20
xor eax, eax
push eax
push dwBytesRead
push DWORD [buflen]
push buffer
push ebx ; handle
call _ReadFile@20
mov esi, eax
; GetHandle - Output
push -11
call _GetStdHandle@4
mov ebx, eax ; copy the handle into ebx
; WriteFile@20
add esi, DWORD [dwBytesRead] ; copy the length into esi
xor eax, eax
push eax
push eax
push esi
push buffer
push ebx
call _WriteFile@20
【问题讨论】: