问题
I have opened a file with OpenFile, and gotten its size with GetFileSize. I wish to use ReadFile and use the stack as the buffer it requires, allocating enough room on the stack with the size of the file returned from GetFileSize. When I run this I get no output.
Here is my code...
extern GetStdHandle
extern GetModuleFileNameA
extern OpenFile
extern ReadFile
extern WriteFile
extern CloseHandle
extern GetFileSize
extern ExitProcess
import GetStdHandle kernel32.dll
import GetModuleFileNameA kernel32.dll
import OpenFile kernel32.dll
import ReadFile kernel32.dll
import WriteFile kernel32.dll
import CloseHandle kernel32.dll
import GetFileSize kernel32.dll
import ExitProcess kernel32.dll
global ..start
segment .code USE32
..start:
;Setting up the stack...
push ebp
mov ebp, esp
;Get standard output to console
push dword -11
call [GetStdHandle]
mov dword [hStdOut], eax
;Get filepath...
push dword filepath
push dword 0
call [GetModuleFileNameA]
;Outputting filepath
;Doesn't show up on the console but if you dump it to a file
;and edit it it is there...
;push dword 0
;push dword bytesRead
;push dword 128 ;Maximum path size for OpenFile...
;push dword filepath
;push dword [hStdOut]
;call [WriteFile]
;Opening the file and getting the handle...
push dword 0
push dword ofstruct
push dword filepath
mov dword [hSelfFile]
;Getting the file size...
push dword 0
push dword hSelfFile
call [GetFileSize]
mov dword [fSize], eax
;Allocating data on the stack...
sub esp, fSize
;Reading the file...
push dword 0
push dword bytesRead
push dword fSize
push dword ebp ;ebp or esp?
push dword [hSelfFile]
call [ReadFile]
;Outputting the read file...
push dword 0
push dword bytesRead
push dword fSize
push dword ebp ;ebp or esp?
push dword [hStdOut]
call [WriteFile]
;Closing the file handle...
push dword hSelfFile
call [CloseHandle]
;Cleaning up the stack...
mov esp, ebp
pop ebp
xor eax, eax
push eax
call [ExitProcess]
segment .data
segment .bss
hStdOut resd 1
hSelfFile resd 1
bytesRead resd 1
ofstruct resb 136 ;The size in bytes of ofstruct
fSize resd 1
filepath resb 128 ;Maximum path OpenFile will allow
What am I doing wrong?
回答1:
malloc? Why would you link to the C library when there are memory functions that are part of Windows?
Besides all of the mistakes in your code, you do realize that you are trying to open an executable file and print the contents to the console? You cannot do that (well you can but you won't get anything pretty) since exes contain non printable characters, you will see a lot of garbage on the screen.
Here is some example code that will open a text file in the exes directory, get the size, allocate some memory to hold file, read file, and display the contents to the console.
There is absolutely no error checking involved here, you would of course add error checking in a normal app.
Create a text file called test.txt and save it in the exes directory. You can fill this file with whatever text you want for this test. I chose to use a bacon lorem ipsum generator, since I like bacon :-)
%define STD_OUTPUT_HANDLE -11
%define OPEN_EXISTING 3
%define NULL 0
%define HEAP_ZERO_MEMORY 8
%define FILE_READ_DATA 1
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
section .bss
hConOut resd 1
lpBytesWritten resd 1
hSelfFile resd 1
hHeap resd 1
lpFileSize resd 1
hReadBuf resd 1
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
section .data
szTestFile db 'test.txt', 0
szCRLF db 13, 10
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global Start
section .text
Start:
call GetProcessHeap ; get handle to apps heap
mov [hHeap], eax
push STD_OUTPUT_HANDLE ; um, self explanitory
call GetStdHandle
mov [hConOut], eax
push NULL
push NULL
push OPEN_EXISTING
push NULL
push 0
push FILE_READ_DATA
push szTestFile
call CreateFileA ; Let's open the damn file
mov [hSelfFile], eax
push NULL
push eax
call GetFileSize ; well, get the size of file to read
add eax, 1
mov [lpFileSize], eax
push eax
push HEAP_ZERO_MEMORY
push dword [hHeap]
call HeapAlloc ; now allocate some memory to hold contents
mov [hReadBuf], eax
push NULL
push lpBytesWritten
push dword [lpFileSize]
push dword [hReadBuf]
push dword [hSelfFile]
call ReadFile ; slurp it into memory
push dword [hSelfFile]
call CloseHandle ; don't need anymore
push NULL
push lpBytesWritten
push dword [lpFileSize]
push dword [hReadBuf]
push dword [hConOut]
call WriteFile ; print to console
push NULL
push lpBytesWritten
push 2
push szCRLF
push dword [hConOut]
call WriteFile ; "display carrage return/linefeed"
push NULL
push hReadBuf
call HeapFree ; free the our buffer memory
push 0
call ExitProcess
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
extern GetStdHandle
extern CreateFileA
extern ReadFile
extern WriteFile
extern CloseHandle
extern GetFileSize
extern ExitProcess
extern GetProcessHeap
extern HeapAlloc
extern HeapFree
来源:https://stackoverflow.com/questions/31272377/how-do-i-read-a-file-with-readfile-onto-the-stack-in-nasm-x86-assembly