Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

really lame coder in need of help!

bigmike500bigmike500 Member Posts: 24
my problem is becoming a bit of a recouring one, i am just trying to do simple things in windows with assembly and the aid of Iczelion's tutorials as a guide, though he codes in masm and i am using tasm, but from what i have heard they are very simular, anyway all i am wanting to do is to program a window with some printed text in it, thought this has proved a problem, the code that i have used is pretty much the same thought it is not working, i have really not got a clue what the problem could be and was wondering if anyone has any ideas. the program assembles fine but when executed it creates errors and is shut down by windows, also i have tried to get a idea of the problem from debugging the problem, when i get to the API dispatchmeassageA the debugger returns a error 'exception : access violation write address 00000047', i am guessing that the programming is trying to write data outside its allowed 4Gb space but i don't know why. however when i comment out two lines of code (marked with ***)the program executes and the window appears but the text doesn't appear in the window. therefore there must be some problem with these lines of code, any ideas?
cheers
mike


.386
.model flat, stdcall

include C:assembly asm5includewindows.inc

extrn GetModuleHandleA:near
extrn RegisterClassExA:near
extrn CreateWindowExA:near
extrn GetMessageA:near
extrn DispatchMessageA:near
extrn ExitProcess:near
extrn DefWindowProcA:near
extrn PostQuitMessage:near
extrn MessageBoxA:near
extrn SendMessageA:near
extrn ShowWindow:near
extrn UpdateWindow:near
extrn LoadIconA:near
extrn LoadCursorA:near
extrn BeginPaint:near
extrn GetClientRect:near
extrn DrawTextA:near
extrn EndPaint:near
extrn TranslateMessage:near
extrn TextOutA:near

.data
;Definition of the window class:
testWindowclass WNDCLASSEX
Windowhandle dd 0

;GetMessage requires a buffer for an incoming Message (one Message = 7 DWORDS):
MessageBuffer dd 0,0,0,0,0,0,0

Windowtitle db "ordinary standard window",0
Windowclassname db "testWindowclass",0
Windowname db "First painted Window",0
displayText db "this is my first display of text in a window",0

;these constants are stand for themselves - change them, if you want ;-)
WindowsizeInX equ 450
WindowsizeInY equ 350
WindowpositionInX equ 100
WindowpositionInY equ 120

.code
main:

;many function require a handle identifying the running program
;GetModuleHandle (0) retrieves one

call GetModuleHandleA, 0
mov [testWindowclass.wcx_hInstance],eax

; Load the cursor for the application
call LoadCursorA, 0, IDC_ARROW
mov [testWindowclass.wcx_hCursor], eax ;save here

;load a icon to use in the window
call LoadIconA, 0,IDI_APPLICATION
mov [testWindowclass.wcx_hIcon],eax



;tell the OS about our new window class

call RegisterClassExA, offset testWindowclass


;create a window corresponding to our window class:
push 0
push [testWindowclass.wcx_hInstance]
push 0
push 0
push WindowsizeInY
push WindowsizeInX
push WindowpositionInY
push WindowpositionInX
push WS_EX_OVERLAPPEDWINDOW ;same as WS_EX_VISIBLE or WS_EX_OVERLAPPED or

WS_EX_CAPTION or WS_EX_SYSMENU or WS_EX_THICKFRAME or WS_EX_MINIMIZEBOX or WS_EX_MAXIMIZEBOX
push offset Windowname
push offset Windowclassname
push 0
call CreateWindowExA
mov Windowhandle,eax

;needed to show window on desktop (not really needed! but what the hell)
; push [Windowhandle]
; push SW_SHOWNORMAL
; call ShowWindow
;
; push [Windowhandle]
; call UpdateWindow


;The most important place in a windows program: The Message Pump / Message Loop. Here the
;incoming Messages get preprocessed and get send to the corresponding window procedure

Messageloop:
push 0
push 0
push 0
push offset MessageBuffer
call GetMessageA
or eax,eax
jz WM_QUIT_received
call DispatchMessageA, offset MessageBuffer ;problem occurs here
jmp Messageloop


WM_QUIT_received:
push 0
call ExitProcess

public WndProc

WndProc proc stdcall hwnd:DWORD, uiMsg:DWORD, wp:DWORD, lp:DWORD
LOCAL hdc:HDC
LOCAL rect:RECT
LOCAL ps:PAINTSTRUCT

push esi ; // MUST do that, but not a lot of manuals mention it
push edi
push ebx

mov eax, uiMsg
; cmp eax, WM_CREATE
; je @_WmCreate
cmp eax, WM_DESTROY
je @_WmDestroy
; cmp eax, WM_COMMAND
; je @_WmCommand
cmp eax, WM_PAINT
je @_WmPaint

; --- Call default procedure here:
push lp
push wp
push uiMsg
push hwnd
call DefWindowProcA
jmp @_Return_LRESULT ; Return what default processing returned.

@_WmPaint:

call BeginPaint, [hwnd], word PTR offset [ps]
mov [hdc],eax
;*** call GetClientRect, [hwnd], word PTR offset [rect]
;*** call DrawTextA, [hdc], offset displayText,-1, word PTR offset [rect],DT_SINGLELINE or

;DT_CENTER or DT_VCENTER
call EndPaint, [hwnd], word PTR offset [ps]

jmp @_Return_LRESULT_0

;@_WmCreate:
; jmp @_Return_LRESULT_0

;@_WmCommand:
; jmp @_Return_LRESULT_0

@_WmDestroy:
push 0
call PostQuitMessage
jmp @_Return_LRESULT_0

; Always return correct LRESULT!
; Some messages require not only zero (check MSDN)
@_Return_LRESULT_0:
xor eax, eax

@_Return_LRESULT: ; <- Jump here to return specific value from the code
pop ebx ; // Restore all back
pop edi
pop esi
ret

WndProc endp
end main

Comments

  • jakoblinjakoblin Member Posts: 21
    guess what you did wrong .


    : my problem is becoming a bit of a recouring one, i am just trying to do simple things in windows with assembly and the aid of Iczelion's tutorials as a guide, though he codes in masm and i am using tasm, but from what i have heard they are very simular, anyway all i am wanting to do is to program a window with some printed text in it, thought this has proved a problem, the code that i have used is pretty much the same thought it is not working, i have really not got a clue what the problem could be and was wondering if anyone has any ideas. the program assembles fine but when executed it creates errors and is shut down by windows, also i have tried to get a idea of the problem from debugging the problem, when i get to the API dispatchmeassageA the debugger returns a error 'exception : access violation write address 00000047', i am guessing that the programming is trying to write data outside its allowed 4Gb space but i don't know why. however when i comment out two lines of code (marked with ***)the program executes and the window appears but the text doesn't appear in the window. therefore there must be some problem with these lines of code, any ideas?
    : cheers
    : mike
    :
    :
    : .386
    : .model flat, stdcall
    :
    : include C:assembly asm5includewindows.inc
    :
    : extrn GetModuleHandleA:near
    : extrn RegisterClassExA:near
    : extrn CreateWindowExA:near
    : extrn GetMessageA:near
    : extrn DispatchMessageA:near
    : extrn ExitProcess:near
    : extrn DefWindowProcA:near
    : extrn PostQuitMessage:near
    : extrn MessageBoxA:near
    : extrn SendMessageA:near
    : extrn ShowWindow:near
    : extrn UpdateWindow:near
    : extrn LoadIconA:near
    : extrn LoadCursorA:near
    : extrn BeginPaint:near
    : extrn GetClientRect:near
    : extrn DrawTextA:near
    : extrn EndPaint:near
    : extrn TranslateMessage:near
    : extrn TextOutA:near
    :
    : .data
    : ;Definition of the window class:
    : testWindowclass WNDCLASSEX
    : Windowhandle dd 0
    :
    : ;GetMessage requires a buffer for an incoming Message (one Message = 7 DWORDS):
    : MessageBuffer dd 0,0,0,0,0,0,0
    :
    : Windowtitle db "ordinary standard window",0
    : Windowclassname db "testWindowclass",0
    : Windowname db "First painted Window",0
    : displayText db "this is my first display of text in a window",0
    :
    : ;these constants are stand for themselves - change them, if you want ;-)
    : WindowsizeInX equ 450
    : WindowsizeInY equ 350
    : WindowpositionInX equ 100
    : WindowpositionInY equ 120
    :
    : .code
    : main:
    :
    : ;many function require a handle identifying the running program
    : ;GetModuleHandle (0) retrieves one
    :
    : call GetModuleHandleA, 0
    : mov [testWindowclass.wcx_hInstance],eax
    :
    : ; Load the cursor for the application
    : call LoadCursorA, 0, IDC_ARROW
    : mov [testWindowclass.wcx_hCursor], eax ;save here
    :
    : ;load a icon to use in the window
    : call LoadIconA, 0,IDI_APPLICATION
    : mov [testWindowclass.wcx_hIcon],eax
    :
    :
    :
    : ;tell the OS about our new window class
    :
    : call RegisterClassExA, offset testWindowclass
    :
    :
    : ;create a window corresponding to our window class:
    : push 0
    : push [testWindowclass.wcx_hInstance]
    : push 0
    : push 0
    : push WindowsizeInY
    : push WindowsizeInX
    : push WindowpositionInY
    : push WindowpositionInX
    : push WS_EX_OVERLAPPEDWINDOW ;same as WS_EX_VISIBLE or WS_EX_OVERLAPPED or
    :
    : WS_EX_CAPTION or WS_EX_SYSMENU or WS_EX_THICKFRAME or WS_EX_MINIMIZEBOX or WS_EX_MAXIMIZEBOX
    : push offset Windowname
    : push offset Windowclassname
    : push 0
    : call CreateWindowExA
    : mov Windowhandle,eax
    :
    : ;needed to show window on desktop (not really needed! but what the hell)
    : ; push [Windowhandle]
    : ; push SW_SHOWNORMAL
    : ; call ShowWindow
    : ;
    : ; push [Windowhandle]
    : ; call UpdateWindow
    :
    :
    : ;The most important place in a windows program: The Message Pump / Message Loop. Here the
    : ;incoming Messages get preprocessed and get send to the corresponding window procedure
    :
    : Messageloop:
    : push 0
    : push 0
    : push 0
    : push offset MessageBuffer
    : call GetMessageA
    : or eax,eax
    : jz WM_QUIT_received
    : call DispatchMessageA, offset MessageBuffer ;problem occurs here
    : jmp Messageloop
    :
    :
    : WM_QUIT_received:
    : push 0
    : call ExitProcess
    :
    : public WndProc
    :
    : WndProc proc stdcall hwnd:DWORD, uiMsg:DWORD, wp:DWORD, lp:DWORD
    : LOCAL hdc:HDC
    : LOCAL rect:RECT
    : LOCAL ps:PAINTSTRUCT
    :
    : push esi ; // MUST do that, but not a lot of manuals mention it
    : push edi
    : push ebx
    :
    : mov eax, uiMsg
    : ; cmp eax, WM_CREATE
    : ; je @_WmCreate
    : cmp eax, WM_DESTROY
    : je @_WmDestroy
    : ; cmp eax, WM_COMMAND
    : ; je @_WmCommand
    : cmp eax, WM_PAINT
    : je @_WmPaint
    :
    : ; --- Call default procedure here:
    : push lp
    : push wp
    : push uiMsg
    : push hwnd
    : call DefWindowProcA
    : jmp @_Return_LRESULT ; Return what default processing returned.
    :
    : @_WmPaint:
    :
    : call BeginPaint, [hwnd], word PTR offset [ps]
    : mov [hdc],eax
    : ;*** call GetClientRect, [hwnd], word PTR offset [rect]
    : ;*** call DrawTextA, [hdc], offset displayText,-1, word PTR offset [rect],DT_SINGLELINE or
    :
    : ;DT_CENTER or DT_VCENTER
    : call EndPaint, [hwnd], word PTR offset [ps]
    :
    : jmp @_Return_LRESULT_0
    :
    : ;@_WmCreate:
    : ; jmp @_Return_LRESULT_0
    :
    : ;@_WmCommand:
    : ; jmp @_Return_LRESULT_0
    :
    : @_WmDestroy:
    : push 0
    : call PostQuitMessage
    : jmp @_Return_LRESULT_0
    :
    : ; Always return correct LRESULT!
    : ; Some messages require not only zero (check MSDN)
    : @_Return_LRESULT_0:
    : xor eax, eax
    :
    : @_Return_LRESULT: ; <- Jump here to return specific value from the code
    : pop ebx ; // Restore all back
    : pop edi
    : pop esi
    : ret
    :
    : WndProc endp
    : end main
    :

Sign In or Register to comment.