News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Beginning Assembly-displaying .bmp images

Started by kbmarie86, March 23, 2014, 05:12:28 AM

Previous topic - Next topic

kbmarie86

I have been able to assemble the program but it doesn't run the .exe file. I'm in my first assembly class and have so far been able to figure out all but this last program. I'll attach the assignment also so it has some context. Can anyone figure out where I'm going wrong in this? Also we are using the Irvine book but my instructor wants us to use his lib and inc instead of the kip irvine library it's heath32. Thanks in advance!!


.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\gdi32.inc
include \masm32\include\Heath32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\Heath32.lib
includelib \masm32\lib\masm32.lib

WinMain proto :DWORD,:DWORD,:DWORD,:DWORD

.DATA
ImageName1 db "c:\masm32\MyFiles\Week 9\Tweety.bmp", 0
ImageName2 db "c:\masm32\MyFiles\Week 9\Sylvester.bmp", 0
BigPict dd FALSE

.Data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
maxX DD ?
maxY DD ?
ShapeNumber dd ?
startpoint POINT <>
endpoint POINT <>
hMemDC HDC ?
Bitmap HBITMAP ?
hBitmap HBITMAP ?

.CODE
start:
        invoke LoadImage, hInstance, ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
        mov hBitmap,eax
       
        WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
                LOCAL hDC:HDC
                LOCAL ps:PAINTSTRUCT
                LOCAL rect:RECT
                LOCAL hInnerDC:HDC
       
        invoke CreateCompatibleDC,hMemDC
        mov hInnerDC,eax

    .IF uMsg==WM_DESTROY
        invoke PostQuitMessage,NULL

    .ELSEIF uMsg==WM_RBUTTONDOWN
        mov eax,lParam
        and eax,0FFFFh
        mov startpoint.x,eax
        mov eax,lParam
        shr eax,16
        mov startpoint.y,eax
        invoke LoadImage, hInstance,ADDR ImageName2, IMAGE_BITMAP, 200, 200, LR_LOADFROMFILE
        mov hBitmap,eax
        mov BigPict, TRUE
        invoke InvalidateRect,hWnd,NULL,TRUE
        ret

    .ELSEIF uMsg==WM_LBUTTONDOWN
        mov eax,lParam
        and eax,0FFFFh
        mov startpoint.x,eax
        mov eax,lParam
        shr eax,16
        mov startpoint.y,eax
        invoke LoadImage, hInstance,ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
        mov hBitmap,eax
        mov BigPict, FALSE
        invoke InvalidateRect,hWnd,NULL,TRUE
        ret


     .ELSEIF uMsg==WM_PAINT
    invoke BeginPaint,hWnd, ADDR ps
        mov hDC,eax
        invoke CreateCompatibleDC,hMemDC
        mov hInnerDC,eax
        invoke SelectObject, hInnerDC,hBitmap
        .IF BigPict
        invoke BitBlt, hMemDC, startpoint.x, startpoint.y, 200, 200, hInnerDC, 0, 0, SRCCOPY
        .ELSE
        invoke BitBlt, hMemDC, startpoint.x, startpoint.y, 100, 100, hInnerDC, 0, 0, SRCCOPY
        .ENDIF
        invoke GetClientRect,hWnd,ADDR rect
        invoke SelectObject,hDC, hMemDC
        invoke BitBlt,hDC,0,0,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY
        invoke EndPaint,hWnd,addr ps
        invoke DeleteDC,hInnerDC
        invoke DeleteObject,hBitmap
        ret

       
    .ELSEIF uMsg==WM_CREATE
        invoke GetSystemMetrics, SM_CXSCREEN
        mov maxX, eax
        invoke GetSystemMetrics, SM_CYSCREEN
        mov maxY, eax
        invoke GetDC,hWnd
        mov hDC, eax
        invoke CreateCompatibleDC, hDC
        mov hMemDC, eax
        invoke CreateCompatibleBitmap, hDC, maxX, maxY
        mov Bitmap, eax
        invoke SelectObject, hMemDC, Bitmap
        invoke PatBlt, hMemDC, 0, 0, maxX, maxY, PATCOPY
        mov startpoint.x, 0
        mov startpoint.y, 0
        invoke LoadImage, hInstance,ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
        mov hBitmap,eax
        invoke InvalidateRect,hWnd,NULL,TRUE
        ret
       
     .ELSE
        invoke DefWindowProc,hWnd,uMsg,wParam,lParam
        ret
     .ENDIF
        xor eax,eax
        ret
        WndProc endp

end start

mov hInstance,eax
invoke GetCommandLine

mov CommandLine,eax

invoke ExitProcess,eax

GoneFishing

Could not  compile your code :
Heath32.inc is missing
[EDIT1]: Well ,I just commented out  Heath32.inc / Heath32.lib lines . All compiles but doesn't run .Then I looked at your code and the first my question is why didn't you try to create a window ?

dedndave

the problem seems to be overall program flow (ignoring the Heath32 library)

after the END statement, you have a little bit of code that initializes hInstance
anything after END should be ignored by the assembler
and - the code at Start is executed with hInstance = 0

dedndave

by  re-ordering sections of your code,
i can see that a couple things are missing
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\gdi32.inc
;include \masm32\include\Heath32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib
;includelib \masm32\lib\Heath32.lib
includelib \masm32\lib\masm32.lib

WinMain proto :DWORD,:DWORD,:DWORD,:DWORD

.DATA
ImageName1 db "c:\masm32\MyFiles\Week 9\Tweety.bmp", 0
ImageName2 db "c:\masm32\MyFiles\Week 9\Sylvester.bmp", 0
BigPict dd FALSE

.Data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
maxX DD ?
maxY DD ?
ShapeNumber dd ?
startpoint POINT <>
endpoint POINT <>
hMemDC HDC ?
Bitmap HBITMAP ?
hBitmap HBITMAP ?

.CODE
start:

mov hInstance,eax
invoke GetCommandLine

mov CommandLine,eax

        invoke LoadImage, hInstance, ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
        mov hBitmap,eax


something here to create a window

something here to set up a message loop

invoke ExitProcess,eax
        WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
                LOCAL hDC:HDC
                LOCAL ps:PAINTSTRUCT
                LOCAL rect:RECT
                LOCAL hInnerDC:HDC
       
        invoke CreateCompatibleDC,hMemDC
        mov hInnerDC,eax

    .IF uMsg==WM_DESTROY
        invoke PostQuitMessage,NULL

    .ELSEIF uMsg==WM_RBUTTONDOWN
        mov eax,lParam
        and eax,0FFFFh
        mov startpoint.x,eax
        mov eax,lParam
        shr eax,16
        mov startpoint.y,eax
        invoke LoadImage, hInstance,ADDR ImageName2, IMAGE_BITMAP, 200, 200, LR_LOADFROMFILE
        mov hBitmap,eax
        mov BigPict, TRUE
        invoke InvalidateRect,hWnd,NULL,TRUE
        ret

    .ELSEIF uMsg==WM_LBUTTONDOWN
        mov eax,lParam
        and eax,0FFFFh
        mov startpoint.x,eax
        mov eax,lParam
        shr eax,16
        mov startpoint.y,eax
        invoke LoadImage, hInstance,ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
        mov hBitmap,eax
        mov BigPict, FALSE
        invoke InvalidateRect,hWnd,NULL,TRUE
        ret


     .ELSEIF uMsg==WM_PAINT
      invoke BeginPaint,hWnd, ADDR ps
        mov hDC,eax
        invoke CreateCompatibleDC,hMemDC
        mov hInnerDC,eax
        invoke SelectObject, hInnerDC,hBitmap
        .IF BigPict
        invoke BitBlt, hMemDC, startpoint.x, startpoint.y, 200, 200, hInnerDC, 0, 0, SRCCOPY
        .ELSE
        invoke BitBlt, hMemDC, startpoint.x, startpoint.y, 100, 100, hInnerDC, 0, 0, SRCCOPY
        .ENDIF
        invoke GetClientRect,hWnd,ADDR rect
        invoke SelectObject,hDC, hMemDC
        invoke BitBlt,hDC,0,0,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY
        invoke EndPaint,hWnd,addr ps
        invoke DeleteDC,hInnerDC
        invoke DeleteObject,hBitmap
        ret

       
    .ELSEIF uMsg==WM_CREATE
        invoke GetSystemMetrics, SM_CXSCREEN
        mov maxX, eax
        invoke GetSystemMetrics, SM_CYSCREEN
        mov maxY, eax
        invoke GetDC,hWnd
        mov hDC, eax
        invoke CreateCompatibleDC, hDC
        mov hMemDC, eax
        invoke CreateCompatibleBitmap, hDC, maxX, maxY
        mov Bitmap, eax
        invoke SelectObject, hMemDC, Bitmap
        invoke PatBlt, hMemDC, 0, 0, maxX, maxY, PATCOPY
        mov startpoint.x, 0
        mov startpoint.y, 0
        invoke LoadImage, hInstance,ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
        mov hBitmap,eax
        invoke InvalidateRect,hWnd,NULL,TRUE
        ret
       
     .ELSE
        invoke DefWindowProc,hWnd,uMsg,wParam,lParam
        ret
     .ENDIF
        xor eax,eax
        ret
        WndProc endp

end start

jj2007

Hi kbmarie,
Welcome to the forum :icon14:

As Vertograd and Dave already wrote, there is a serious problem with the overall design. A Windows program generally consists of an entry area (start: or WinMain:) where the main window is created, and a callback function called WndProc.

Please study \Masm32\examples\exampl01\generic\generic.asm, and adapt your program accordingly.

dedndave

here is a simple window program

it also includes a resource file, with a manifest, and batch files to build it

use it as a guideline to see what the program flow should be

Gunther

You have to know the facts before you can distort them.

kbmarie86

Thank you all for the kind welcome, and for being kind where I am very new to this. Thank you for all the resources I really appreciate the help, working hard on my project right now and trying to apply what you have all shown me into my own design.

kbmarie86

Well I finally got it!! It's not the prettiest but it does the job and in all honesty I don't enjoy the art of programming enough to spend all the time needed to perfecting it, just trying to pass my class and understand enough to get through it and supplement my knowledge for my other classes.
Thanks again for ALL your help!!



.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\gdi32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\masm32.lib

WinMain proto :DWORD,:DWORD,:DWORD,:DWORD

.DATA
ClassName db "WinClass", 0
AppName db "Simple Window", 0
MouseClick db 0
ImageName1 db "c:\masm32\MyFiles\Week 9\Tweety.bmp", 0
ImageName2 db "c:\masm32\MyFiles\Week 9\Sylvester.bmp", 0
BigPict dd FALSE

.Data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
maxX DD ?
maxY DD ?
ShapeNumber dd ?
startpoint POINT <>
endpoint POINT <>
hMemDC HDC ?
Bitmap HBITMAP ?
hBitmap HBITMAP ?

.CODE
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke WinMain,hInstance, NULL, NULL, 0
invoke GetCommandLine
mov CommandLine,eax
invoke ExitProcess,eax

invoke LoadImage, hInstance, ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
mov hBitmap,eax

WinMain proc hInst:HINSTANCE, hPrevInst:HINSTANCE, CmdLine:LPSTR, CmdShow:DWORD
local wc:WNDCLASSEX
local msg:MSG
local hwnd:HWND
mov wc.cbSize, SIZEOF WNDCLASSEX
mov wc.style, CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc, OFFSET WndProc
mov wc.cbClsExtra, NULL
mov wc.cbWndExtra, NULL
push hInst
pop wc.hInstance
mov wc.lpszClassName, offset ClassName
invoke LoadIcon, NULL, IDI_APPLICATION
mov wc.hIcon, eax
mov wc.hbrBackground,COLOR_WINDOW+1
mov wc.lpszMenuName, NULL
mov wc.lpszClassName, offset ClassName
invoke LoadIcon, NULL, IDI_APPLICATION
mov wc.hIcon, eax
invoke LoadCursor, NULL, IDC_ARROW
mov wc.hCursor, eax
invoke RegisterClassEx, addr wc
invoke CreateWindowEx, 0, addr ClassName, addr AppName, WS_OVERLAPPEDWINDOW or WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
NULL, hInst, NULL
mov hwnd, eax
.while TRUE
invoke GetMessage, addr msg, NULL, 0, 0
.break .if (!eax)
invoke TranslateMessage, addr msg
invoke DispatchMessage, addr msg
.endw
mov eax, msg.wParam
ret
WinMain endp


mov CommandLine,eax

        invoke LoadImage, hInstance, ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
        mov hBitmap,eax

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
                LOCAL hDC:HDC
                LOCAL ps:PAINTSTRUCT
                LOCAL rect:RECT
                LOCAL hInnerDC:HDC
       
        invoke CreateCompatibleDC,hMemDC
        mov hInnerDC,eax

    .IF uMsg==WM_DESTROY
        invoke PostQuitMessage,NULL

    .ELSEIF uMsg==WM_RBUTTONDOWN
        mov eax,lParam
        and eax,0FFFFh
        mov startpoint.x,eax
        mov eax,lParam
        shr eax,16
        mov startpoint.y,eax
        invoke LoadImage, hInstance,ADDR ImageName2, IMAGE_BITMAP, 200, 200, LR_LOADFROMFILE
        mov hBitmap,eax
        mov BigPict, TRUE
        invoke InvalidateRect,hWnd,NULL,TRUE
        ret

    .ELSEIF uMsg==WM_LBUTTONDOWN
        mov eax,lParam
        and eax,0FFFFh
        mov startpoint.x,eax
        mov eax,lParam
        shr eax,16
        mov startpoint.y,eax
        invoke LoadImage, hInstance,ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
        mov hBitmap,eax
        mov BigPict, FALSE
        invoke InvalidateRect,hWnd,NULL,TRUE
        ret


     .ELSEIF uMsg==WM_PAINT
      invoke BeginPaint,hWnd, ADDR ps
        mov hDC,eax
        invoke CreateCompatibleDC,hMemDC
        mov hInnerDC,eax
        invoke SelectObject, hInnerDC,hBitmap
        .IF BigPict
        invoke BitBlt, hMemDC, startpoint.x, startpoint.y, 200, 200, hInnerDC, 0, 0, SRCCOPY
        .ELSE
        invoke BitBlt, hMemDC, startpoint.x, startpoint.y, 100, 100, hInnerDC, 0, 0, SRCCOPY
        .ENDIF
        invoke GetClientRect,hWnd,ADDR rect
        invoke SelectObject,hDC, hMemDC
        invoke BitBlt,hDC,0,0,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY
        invoke EndPaint,hWnd,addr ps
        invoke DeleteDC,hInnerDC
        invoke DeleteObject,hBitmap
        ret

       
    .ELSEIF uMsg==WM_CREATE
        invoke GetSystemMetrics, SM_CXSCREEN
        mov maxX, eax
        invoke GetSystemMetrics, SM_CYSCREEN
        mov maxY, eax
        invoke GetDC,hWnd
        mov hDC, eax
        invoke CreateCompatibleDC, hDC
        mov hMemDC, eax
        invoke CreateCompatibleBitmap, hDC, maxX, maxY
        mov Bitmap, eax
        invoke SelectObject, hMemDC, Bitmap
        invoke PatBlt, hMemDC, 0, 0, maxX, maxY, PATCOPY
        mov startpoint.x, 0
        mov startpoint.y, 0
        invoke LoadImage, hInstance,ADDR ImageName1, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE
        mov hBitmap,eax
        invoke InvalidateRect,hWnd,NULL,TRUE
        ret
       
     .ELSE
        invoke DefWindowProc,hWnd,uMsg,wParam,lParam
        ret
     .ENDIF
        xor eax,eax
        ret
        WndProc endp

end start


MichaelW

Your code does not display a window because CreateWindowEx is failing and returning the last error code value ERROR_CANNOT_FIND_WND_CLASS, or at least this is what happens under Windows XP SP3.

When your WNDCLASSEX structure is a local variable, allocated from the stack and not automatically initialized (so the members contain whatever "garbage" values happen to be in the stack memory), you should generally either initialize all of the members in your code, or first zero the entire structure with RtlZeroMemory, or similar.
Well Microsoft, here's another nice mess you've gotten us into.