我有以下问题。我正在尝试在汇编程序中编写一段代码,它将使用win api函数在控制台中编写file.txt的内容。我有一个打印“hello world”的示例代码,但是当我尝试将文件源作为参数传递时,它会显示源而不是内容。
.586
.MODEL flat, stdcall
STD_OUTPUT_HANDLE equ -11
GetStdHandle PROTO nStdHandle:DWORD
WriteFile PROTO hFile:DWORD,
lpBuffer:NEAR32,
nNumberOfBytesToWrite:DWORD,
lpNumberOfBytesWritten:NEAR32,
lpOverlapped:NEAR32
ExitProcess PROTO dwExitCode:DWORD
PUBLIC __start
.DATA
msg DB "Hello World !", 13, 10
written DW 0
hStdOut DD 0
.CODE
__start:
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov hStdOut, eax
invoke WriteFile, hStdOut, OFFSET msg, LENGTHOF msg, OFFSET written, 0
invoke ExitProcess, 0
END
我会非常感谢一些提示。
答案 0 :(得分:0)
这应该让你去,没有错误检查,以保持简单,这是你要添加。
include masm32rt.inc
.data
szFileName db "file.txt", 0
.data?
hConOut dd ?
hFile dd ?
hHeap dd ?
dummy dd ?
.code
start:
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov hConOut, eax
invoke GetProcessHeap
mov hHeap, eax
invoke CreateFile, offset szFileName, GENERIC_READ, NULL, NULL, OPEN_EXISTING, NULL, NULL
mov hFile, eax
invoke GetFileSize, eax, NULL
mov edi, eax
invoke HeapAlloc, hHeap, HEAP_ZERO_MEMORY, eax
mov esi, eax
invoke ReadFile, hFile, esi, edi, offset dummy, NULL
invoke WriteFile, hConOut, esi, edi, offset dummy, NULL
invoke HeapFree, hHeap, 0, esi
invoke CloseHandle, hFile
invoke ExitProcess, 0
end start