flat assembler
Message board for the users of flat assembler.
Index
> Windows > Read File and Output to display win7 |
Author |
|
AsmGuru62 31 Oct 2014, 18:08
Try this (just replace the file name with yours):
Code: .code start: ; ; Open file and save its handle into EBX ; invoke CreateFileA, achFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0 mov ebx, eax ; ; If file is not opened - get out! ; cmp eax, INVALID_HANDLE_VALUE je .quit ; ; ESI = file size in bytes ; invoke GetFileSize, ebx, 0 mov esi, eax mov [dwFileSize], eax ; ; Allocate memory for the file contents ; EDI = pointer to that buffer ; invoke VirtualAlloc, 0, esi, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE mov edi, eax mov [pFileBytes], eax ; ; If allocation fails - close file and get out! ; test eax, eax jz .close_file ; ; Read the file into buffer and then close file. ; invoke ReadFile, ebx, edi, [dwFileSize], dwNumLoaded, 0 invoke CloseHandle, ebx ; ; Now the file is fully loaded at the address in EDI... do whatever... ; nop nop nop nop nop ; ; Release memory and quit ; invoke VirtualFree, [pFileBytes], 0, MEM_RELEASE jmp .quit .close_file: invoke CloseHandle, ebx .quit: invoke ExitProcess, 0 .data pFileBytes dd 0 dwFileSize dd 0 dwNumLoaded dd 0 achFileName db 'C:\Users\Public\Pictures\Sample Pictures\Desert.Jpg',0 |
|||
31 Oct 2014, 18:08 |
|
aasom65 31 Oct 2014, 19:42
I'm integrated with my program but file don't read, and I don't understand adress of load file is my Raster1?
Code: format PE GUI 5.0 include 'win32wx.inc' width = 1280 height = 1024 .code start: ;Òóò ìîæíî ðèñîâàòü ; Open file and save its handle into EBX ; invoke CreateFileA, achFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0 mov ebx, eax ; ; If file is not opened - get out! ; cmp eax, INVALID_HANDLE_VALUE je .quit ; ; ESI = file size in bytes ; invoke GetFileSize, ebx, 0 mov esi, eax mov [dwFileSize], eax ; ; Allocate memory for the file contents ; EDI = pointer to that buffer ; invoke VirtualAlloc, 0, esi, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE mov edi, eax mov [pFileBytes], eax ; ; If allocation fails - close file and get out! ; test eax, eax jz .close_file ; ; Read the file into buffer and then close file. invoke ReadFile, ebx, Raster1, [dwFileSize], dwNumLoaded, 0 invoke CloseHandle, ebx ; ; Now the file is fully loaded at the address in EDI... do whatever... ; nop nop nop nop nop ; ; Release memory and quit ; invoke VirtualFree, [pFileBytes], 0, MEM_RELEASE .close_file: invoke CloseHandle, ebx invoke MessageBox,0,Raster1,0,MB_OK ;âûâîäèì ñîîáùåíèå ;mov esi,Raster ;ïåðåìåííàÿ Raster ñîäåðæèò èôîðìàöèþ î öâåòå ïèêñåëåé ;add esi,width*4 ;mov dword[esi-4],255 ;mov ecx,(width-1)*height ;@@:lodsd ;xor eax,[esi-width*4] ; mov [esi],eax ;loop @b ;;;;;;;;;;;;;;;;;;; ;@@:invoke GetDC,0 ;Ïîëó÷àåì Device Context ðàáî÷åãî ñòîëà ; invoke SetDIBitsToDevice,eax,0,0,width,height,0,0,0,height,Raster,bmi,0 ;Êîïèðóåì èçîáðàæåíèå èç ïåðåìåííîé Raster íà ýêðàí ; invoke GetAsyncKeyState,VK_ESCAPE ;Ïðîâåðÿåì ñîñòîÿíèå êëàâèøè ESCAPE ; test eax,eax ;Åñëè íå íàæàòà ;jz @b ;Ïåðåõîäèì íà ìåòêó @@ .quit: invoke ExitProcess,0 ;Âûõîä èç ïðîãðàììû .data pFileBytes dd 0 dwFileSize dd 0 dwNumLoaded dd 0 achFileName db '111.txt',0 Raster rd width*height bmi BITMAPINFOHEADER sizeof.BITMAPINFOHEADER,width,-height,1,24,0,width*height*3 Raster1 db 'C',0 .end start |
|||
31 Oct 2014, 19:42 |
|
AsmGuru62 31 Oct 2014, 20:01
Please try your code in debugger.
The file "111.txt" may be not in the working directory of the program. Try to use the full path for the file. |
|||
31 Oct 2014, 20:01 |
|
aasom65 01 Nov 2014, 07:15
All woking!!! I make mirror picture left right and up down and all woking,thanks!!!
Code: format PE GUI 5.0 include 'win32wx.inc' width = 1280 height = 1024 .code start: ;Òóò ìîæíî ðèñîâàòü ; Open file and save its handle into EBX ; invoke CreateFileA, achFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0 mov ebx, eax ; ; If file is not opened - get out! ; cmp eax, INVALID_HANDLE_VALUE je .quit ; ; ESI = file size in bytes ; invoke GetFileSize, ebx, 0 mov esi, eax mov [dwFileSize], eax ; ; Allocate memory for the file contents ; EDI = pointer to that buffer ; invoke VirtualAlloc, 0, esi, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE mov edi, eax mov [pFileBytes], eax ; ; If allocation fails - close file and get out! ; test eax, eax jz .close_file ; ; Read the file into buffer and then close file. invoke ReadFile, ebx, Raster, [dwFileSize], dwNumLoaded, 0 invoke CloseHandle, ebx ; ; Now the file is fully loaded at the address in EDI... do whatever... ; nop nop nop nop nop ; ; Release memory and quit ; invoke VirtualFree, [pFileBytes], 0, MEM_RELEASE .close_file: invoke CloseHandle, ebx ;invoke MessageBox,0, Raster,0,MB_OK ;âûâîäèì ñîîáùåíèå mov esi,Raster ;ïåðåìåííàÿ Raster ñîäåðæèò èôîðìàöèþ î öâåòå ïèêñåëåé ;add esi,width*4 ;mov dword[esi-4],255 mov ecx,(width-1)*height @@:lodsd ;xor eax,[esi-width*4] ; mov eax,[esi] ;mov [esi],eax loop @b ;;;;;;;;;;;;;;;;;;; @@:invoke GetDC,0 ;Ïîëó÷àåì Device Context ðàáî÷åãî ñòîëà invoke SetDIBitsToDevice,eax,0,0,width,height,0,0,0,height,Raster,bmi,0 ;Êîïèðóåì èçîáðàæåíèå èç ïåðåìåííîé Raster íà ýêðàí invoke GetAsyncKeyState,VK_ESCAPE ;Ïðîâåðÿåì ñîñòîÿíèå êëàâèøè ESCAPE test eax,eax ;Åñëè íå íàæàòà jz @b ;Ïåðåõîäèì íà ìåòêó @@ .quit: invoke ExitProcess,0 ;Âûõîä èç ïðîãðàììû .data pFileBytes dd 0 dwFileSize dd 0 dwNumLoaded dd 0 achFileName db 'f:\222.bmp',0 Raster rd width*height bmi BITMAPINFOHEADER sizeof.BITMAPINFOHEADER,width,-height,1,24,0,width*height*3 Raster1 dd ? .end start
|
|||||||||||
01 Nov 2014, 07:15 |
|
AsmGuru62 01 Nov 2014, 12:49
It is good that i works, however, 2 problems:
1. CloseHandle called twice 2. VirtualFree must be called just before ExitProcess. Basically, memory is getting released and then your code runs on a released memory. |
|||
01 Nov 2014, 12:49 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.