winapi - How do I read a file with ReadFile onto the stack in NASM x86 assembly? -


i have opened file openfile, , gotten size getfilesize. wish use readfile , use stack buffer requires, allocating enough room on stack size of file returned getfilesize. when run no output.

here 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 stack... push ebp mov ebp, esp  ;get standard output 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 on console if dump file ;and edit it there... ;push dword 0 ;push dword bytesread ;push dword 128 ;maximum path size openfile... ;push dword filepath ;push dword [hstdout] ;call [writefile]  ;opening file , getting handle... push dword 0 push dword ofstruct push dword filepath mov dword [hselffile]  ;getting file size... push dword 0 push dword hselffile call [getfilesize] mov dword [fsize], eax  ;allocating data on stack... sub esp, fsize  ;reading file... push dword 0 push dword bytesread push dword fsize push dword ebp ;ebp or esp? push dword [hselffile] call [readfile]  ;outputting read file... push dword 0 push dword bytesread push dword fsize push dword ebp ;ebp or esp? push dword [hstdout] call [writefile]  ;closing file handle... push dword hselffile call [closehandle]  ;cleaning 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 allow 

what doing wrong?

malloc? why link c library when there memory functions part of windows?

besides of mistakes in code, realize trying open executable file , print contents console? cannot (well can won't pretty) since exes contain non printable characters, see lot of garbage on screen.

here example code open text file in exes directory, size, allocate memory hold file, read file, , display contents console.

there absolutely no error checking involved here, of course add error checking in normal app.

create text file called test.txt , save in exes directory. can fill file whatever text want test. chose use bacon lorem ipsum generator, since 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                  ; handle 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 damn file     mov     [hselffile], eax         push    null     push    eax     call    getfilesize                     ; well, size of file read     add     eax, 1     mov     [lpfilesize], eax      push    eax     push    heap_zero_memory     push    dword [hheap]     call    heapalloc                       ; allocate memory hold contents     mov     [hreadbuf], eax      push    null     push    lpbyteswritten     push    dword [lpfilesize]     push    dword [hreadbuf]     push    dword [hselffile]     call    readfile                        ; slurp 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 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 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 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -