News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Need some help

Started by psunlock, January 10, 2010, 04:03:35 PM

Previous topic - Next topic

psunlock

i copied some source code in asm in my attempt to learn and after fixing all errors sw runs but shows in background dos prompt screen

here is source

thanks

.586P
.MODEL FLAT, STDCALL

WM_DESTROY EQU 2 ;CLOSED WINDOW
WM_CREATE EQU 1 ; CREATED WINDOW
WM_LBUTTONDOWN EQU 201h ; leftmousebuttonclickedin program
WM_RBUTTONDOWN EQU 204h ; RIGHTmousebuttonclickedin program
CS_VREDRAW EQU 1h
CS_HREDRAW EQU 2h
CS_GLOBALCLASS EQU 4000h
WS_OVERLAPPEDWINDOW EQU 000CF0000H
Style equ CS_HREDRAW+CS_VREDRAW+CS_GLOBALCLASS
IDI_APPLICATION EQU 32512
IDC_CROSS EQU 32515
SW_SHOWNORMAL EQU 1


EXTERN MessageBoxA@16:NEAR
EXTERN CreateWindowExA@48:NEAR
EXTERN DefWindowProcA@16:NEAR
EXTERN DispatchMessageA@4:NEAR
EXTERN ExitProcess@4:NEAR
EXTERN GetMessageA@16:NEAR
EXTERN GetModuleHandleA@4:NEAR
EXTERN LoadCursorA@8:NEAR
EXTERN LoadIconA@8:NEAR
EXTERN PostQuitMessage@4:NEAR
EXTERN RegisterClassA@4:NEAR
EXTERN ShowWindow@8:NEAR
EXTERN TranslateMessage@4:NEAR
EXTERN UpdateWindow@4:NEAR

includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib

MSGSTRUCT STRUC
MSHWND DD ? ;IDENTIFYER OF WINDOW
MSMESSAGE DD ? ; MESSAGE IDENTIFIER
MSWPARAM DD ? ; AUXILARY INFO ABOUT MESSAGE
MSLPARAM DD ? ; SAME AS ABOVE
MSTIME DD ? ; TIME OF SENDING MESSAGE
MSPT DD ? ; CURSOR POSITION AT TIME OF SENDING MSG
MSGSTRUCT ENDS

WNDCLASS STRUC
CLSSTYLE DD ? ;WINDOWS STYLE
CLWNDPROC DD ? ; POINTER TO THE WINDOW PROCEDURE
CLSCEXTRA DD ? ; INFO ON AUX BYTES FOR STRUC
CLWNDEXTRA DD ? ; SAME AS ABOVE
CLSHINSTANCE DD ? ; APP DESCRIPTOR
CLSHICON DD ? ; WINDOW ICON DESCRIP
CLSHCURSOR DD ? ; WINDOW CURSOR INFO
CLBKGROUND DD ? ; WINDOW BRUSH DESCIPTOR
CLMENUNAME DD ? ;MENU DEXRIPTOR
CLNAME DD ? ;SPECIFIES WINDOW CLASS NAME
WNDCLASS ENDS

_DATA SEGMENT
NEWHWND DD 0
MSG MSGSTRUCT <?>
WC WNDCLASS <?>
HINST DD 0
TITLENAME DB 'BLOODY HELL VER 1', 0
CLASSNAME DB 'CLASS32', 0
CAP DB 'Message', 0
MES1 DB 'You clicked left', 0
MES2 DB 'You clicked Exit' , 0
_DATA ENDS

_TEXT SEGMENT
START:
PUSH 0 ; GET APP DESCRIPTOR
CALL GetModuleHandleA@4
MOV [HINST], EAX
REG_CLASS:
;STYLE
MOV [WC.CLSSTYLE], style
MOV [WC.CLWNDPROC], OFFSET WNDPROC
MOV [WC.CLSCEXTRA], 0
MOV [WC.CLWNDEXTRA], 0
MOV EAX, [HINST]
MOV [WC.CLSHINSTANCE], EAX
;ICON
PUSH IDI_APPLICATION
PUSH 0
CALL LoadIconA@8
MOV [WC.CLSHICON], EAX
;CURSOR
PUSH IDC_CROSS
PUSH 0
CALL LoadCursorA@8
MOV [WC.CLSHCURSOR], EAX
;BACKGROUND WINDOW
MOV [WC.CLBKGROUND], 17 ; WINDOW COLOR
MOV DWORD PTR [WC.CLMENUNAME], 0
MOV DWORD PTR [WC.CLNAME], OFFSET CLASSNAME
PUSH OFFSET WC
CALL RegisterClassA@4
;create window of reggeed class
PUSH 0
PUSH [HINST]
PUSH 0
PUSH 0
PUSH 400 ; WINDOW HEIGHT
PUSH 400 ; WINDOW WIDTH
PUSH 100 ; Y CO ORD DINATES TOP LEFT
PUSH 100 ; X SAME AS ABOVE FOR X
PUSH WS_OVERLAPPEDWINDOW
PUSH OFFSET TITLENAME ; WINDOW NAME
PUSH OFFSET CLASSNAME ; CLASS NAME
PUSH 0
CALL CreateWindowExA@48
CMP EAX, 0
JZ _ERR
MOV [NEWHWND], EAX ; WINDOWS DESCRIPTOR
PUSH SW_SHOWNORMAL
PUSH [NEWHWND]
CALL ShowWindow@8 ; DISPLAY NEW WINDOW
PUSH [NEWHWND]
CALL UpdateWindow@4 ; redraw visible part of window

MSG_LOOP:
PUSH 0
PUSH 0
PUSH 0
PUSH OFFSET MSG
CALL GetMessageA@16
CMP EAX, 0
JE END_LOOP
PUSH OFFSET MSG
CALL TranslateMessage@4
PUSH OFFSET MSG
CALL DispatchMessageA@4
JMP MSG_LOOP
END_LOOP:
PUSH [MSG.MSWPARAM]
CALL ExitProcess@4
_ERR:
JMP END_LOOP

WNDPROC PROC
PUSH EBP
MOV EBP, ESP
PUSH EBX
PUSH ESI
PUSH EDI
CMP DWORD PTR [EBP+0CH], WM_DESTROY
JE WMDESTROY
CMP DWORD PTR [EBP+0CH], WM_CREATE
JE WMCREATE
CMP DWORD PTR [EBP+0CH], WM_LBUTTONDOWN
JE LBUTTON
CMP DWORD PTR [EBP+0CH], WM_RBUTTONDOWN
JE RBUTTON
JMP DEFWNDPROC

RBUTTON:
JMP WMDESTROY
LBUTTON:
;DISPLAY MESSAGE
PUSH 0
PUSH OFFSET CAP
PUSH OFFSET MES1
PUSH DWORD PTR [EBP+08H]
CALL MessageBoxA@16
MOV EAX, 0

JMP FINISH
WMCREATE:
MOV EAX, 0
JMP FINISH
DEFWNDPROC:
PUSH DWORD PTR [EBP+14H]
PUSH DWORD PTR [EBP+10H]
PUSH DWORD PTR [EBP+0CH]
PUSH DWORD PTR [EBP+08H]
CALL DefWindowProcA@16
JMP FINISH
WMDESTROY:
PUSH 0
PUSH OFFSET CAP
PUSH OFFSET MES2
PUSH DWORD PTR [EBP+08H]
CALL MessageBoxA@16
PUSH 0
CALL PostQuitMessage@4
MOV EAX, 0
FINISH:
POP EDI
POP ESI
POP EBX
POP EBP
RET 16
WNDPROC ENDP
_TEXT ENDS




END START

dedndave

it probably has something to do with how it is linked, rather than the code, itself
if you use the masm32\bin\build batch file, it uses these command-line parameters

\masm32\bin\Link /SUBSYSTEM:WINDOWS /OPT:NOREF %1.obj

if you are getting a console window, you may be linking with /SUBSYSTEM:CONSOLE

psunlock

Thank you very much exactly right ;)

jj2007

Quote from: dedndave on January 10, 2010, 04:08:58 PM
if you are getting a console window, you may be linking with /SUBSYSTEM:CONSOLE
Exactly, I just tested it. I was utterly surprised that this syntax (_DATA SEGMENT
instead of .data etc.) works. Is there a specific reason why you use a syntax that looks so much different from the one we use here?

brethren

jj, if i'm not mistaken that code looks like it comes from The Assembly Programming Master Book. The author is one of those purity of assembler guys, ie all arguments must be pushed on the stack, macros are evil etc etc. Thats why he doesn't use the more flexible simplified segment directives

Some advice to the OP. That code is more obtuse than it needs to be, you'd find it easier learning from the iczelion tutorials. They can be found here
http://win32assembly.online.fr/tutorials.html

Vortex

The author of the book probably does not know the exact meaning of macro assembler.

dedndave

QuoteI was utterly surprised that this syntax (_DATA SEGMENT instead of .data etc.) works

when you use ".data" for 16-bit, the assembler creates a segment named "_DATA" (it's an old C standard segment name)
the shortcut is to use @DATA, which actually gives you DGROUP
for 16-bit code, the segment names do not appear in the EXE unless a compiler puts them there
for a 32-bit PE file, the segment is named .data, or at least, that is how it appears in the header
i don't think the name matters much during execution
so long as the references agree

jj2007

Interesting. It looks definitely a bit Russian - as if it had been written for a punchcard reader that doesn't know lowercase - or was it the Open WatCom assembler, WASM??

psunlock,
Welcome to the forum, and congrats that you made this code work. The original assembles but hangs around in mem when launched. And as brethren rightly said, the Iczelion tutorials are a great start. So are the examples in \masm32\examples. Assembly coding for Windows has become a lot easier since these days, take advantage of these improvements.
Best,
jj

psunlock

gotta start somewhere ;)
so far out of all the tuts i could find this is the one i understood so far