0

I have a following problem. I'm trying to write a piece of code in assembler that will use win api functions to write the content of file.txt in console. I have a sample code that prints "hello world" but when I try to pass source of a file as an argument it displays the source instead of the content.

    .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

I will be very greatful for some tips.

4

1 回答 1

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

在此处输入图像描述

于 2013-06-25T03:54:49.593 回答