Вы находитесь на странице: 1из 9

td_win32asm_110.

asm
;==============================================================================
;
Test Department's WINDOWS 32 BIT x86 ASSEMBLY EXAMPLE's
110
;==============================================================================
;==============================================================================
; ==> Part 110 : example of a bitmap animation ( splitted GIF ) using a timer
;-----------------------------------------------------------------------------; Someone asked me how to include an animated GIF in win32asm.
; Because I am to lazy to decode a GIF file here is another way, using bitmaps.
; Split your favorite GIF into single bitmaps, declare this bitmaps in the
; resource file and then write the code in the assembler file.
; First in WinMain we register and create our main window with a menubar.
; In the window procedure (WP1), responding to a WM_CREATE message, we create
; a button ( API=CreateWindowEx ) with the style BS_BITMAP OR BS_PUSHBUTTON.
; API=LoadBitmap loads the button bitmap from the resource file.
; API=SendMessage sends a BM_SETIMAGE message to the window procedure.
; API=SetTimer inits a windows timer.
; On event this timer sends a WM_Timer message to our window procedure, where
; we can change the bitmap deleting the old (!) and loading the new one.
; Clicking the button, a WM_COMMAND message is send to the window procedure !
; We react with a message box. Here you can also perform any other action ...
; "lpfnWndProc" is a pointer to the subroutine label "WindowProc" ( WP1 ) where
; all the action code for this main window resist.
; "lpfnWndProc" is part of WndClassEx structure used by API RegisterClassEx.
;==============================================================================
; Assembler directives
;-----------------------------------------------------------------------------.386
; specifies the processor our program want run on
.Model Flat ,StdCall
; always the same for Win95 (32 Bit)
option casemap:none
; case sensitive !!!
;==============================================================================
; Include all files where API functins resist you want use, set correct path
;-----------------------------------------------------------------------------include D:\Masm32\include\windows.inc
includelib kernel32.lib
includelib user32.lib
includelib gdi32.lib
;==============================================================================
; Declaration of used API functions,take a look into WIN32.HLP and *.inc files
;-----------------------------------------------------------------------------GetModuleHandleA
PROTO :DWORD
LoadIconA
PROTO :DWORD,:DWORD
LoadCursorA
PROTO :DWORD,:DWORD
RegisterClassExA
PROTO :DWORD
CreateWindowExA
PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,
:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
ShowWindow
PROTO :DWORD,:DWORD
UpdateWindow
PROTO :DWORD
GetMessageA
PROTO :DWORD,:DWORD,:DWORD,:DWORD
TranslateMessage
PROTO :DWORD
Page 1

DispatchMessageA
PostQuitMessage
DefWindowProcA
ExitProcess
MessageBoxA
DestroyWindow
SendMessageA
LoadBitmapA
DeleteObject
SetTimer
KillTimer

PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO

td_win32asm_110.asm
:DWORD
:DWORD
:DWORD,:DWORD,:DWORD,:DWORD
:DWORD
:DWORD,:DWORD,:DWORD,:DWORD
:DWORD
:DWORD,:DWORD,:DWORD,:DWORD
:DWORD,:DWORD
:DWORD
:DWORD,:DWORD,:DWORD,:DWORD
:DWORD,:DWORD

;==============================================================================
; .const = the constants area starts here,constants are defined and fixed
;-----------------------------------------------------------------------------.const
; - Parameter MAIN WINDOW CallBack Procedure ( API=RegisterClassExA ) WP1_CallBack
equ [ebp+4]
;return address
WP1_hWnd
equ [ebp+8]
;handle of window who receives message
WP1_uMsg
equ [ebp+12]
;the message number
WP1_wParam
equ [ebp+16]
;extra info about the message
WP1_lParam
equ [ebp+20]
;extra info about the message
;==============================================================================
; .Data = the data area starts here, datas are defined but not fixed
;-----------------------------------------------------------------------------.Data
IconName
db "TDIcon",0
;icon name in rc file
MenuName
db "TDMenu",0
;menu name in rc file
ClassName
db "TDWinClass",0
;name of windows class
WindowName
db "Test Department",0
;window name titel bar
Bu00_ClassName
db "BUTTON",0
;predefined ClassName !!!
Bu01_WindowName
db "BMP",0
;button text
MB1Titel
db "Realy Exit ?",0
;message box name
MB1Text
db "Your choice ...",0
;message box text
MB2Titel
db "Message Box",0
;message box name
MB2Text
db "PUSHBUTTON clicked",0
;mesg. box text
pushbutton0100_hWnd
dd 0h
;handle button
hObject
dd 0h
;handle of bitmap graphic
counter
dd 80h
;counter start/reset value
;==============================================================================
; .Data? = the data? area starts here, not defined and not fixed
;-----------------------------------------------------------------------------.data?
align 4
; - WndClassEx Structure ( API=RegisterClassExA ) cbSize
dd ?
;size in bytes of this structure
style
dd ?
;window style
lpfnWndProc
dd ?
;address of user proc function
cbclsExtra
dd ?
;extra bytes to allocate set to 0
cbWndExtra
dd ?
;extra bytes class directive, rc file
hInstance
dd ?
;program handle(API=GetModuleHandleA)
Page 2

hIcon
hcursor
hbrBackground
lpszMenuName
lpszClassName
hIconSm

dd
dd
dd
dd
dd
dd

?
?
?
?
?
?

td_win32asm_110.asm
;handle of icon (API=LoadIconA)
;handle of cursor (API=LoadCursor)
;background color, 0=transparent
;name of menu class in resource file
;name of windows this window class
;iconhandle 0=search in resource file

align 4
; - Msg Structure ( API=GetMessageA ) - member POINT = POINT structure
hWnd
dd ?
;handle of window who receives message
message
dd ?
;the message number
wParam
dd ?
;extra info about the message
lParam
dd ?
;extra info about the message
time
dd ?
;time the message was posted
xpt
dd ?
;cursor x-position, POINT struc
ypt
dd ?
;cursor x-position, POINT struc
;==============================================================================
; .CODE = our code area starts here
Main = label of our program code
;-----------------------------------------------------------------------------.Code
Main:
;==============================================================================
; Always get your program ID first (API=GetModuleHandleA)
;-----------------------------------------------------------------------------push
0h
;lpModuleHandle, 0=get program handle
call
GetModuleHandleA
;- API Function mov
hInstance,eax
;return value in eax=handle of program
;==============================================================================
; The API function "RegisterClassExA" registers a window class
; This API needs a "WNDCLASSEX" structure so we fill it with correct values
;-----------------------------------------------------------------------------mov
cbSize,30h
;size in bytes of WNDCLASSEX structure
mov
style,3h
;window style
mov
lpfnWndProc,OFFSET WP1
;address of user lpfnWndProc function
mov
cbclsExtra,0h
;extra bytes to allocate set to 0
mov
cbWndExtra,0h
;class directive in rc file
mov
hbrBackground,2h
;background,1=background(parameter+1)
mov
lpszMenuName,OFFSET MenuName;menu name in resource file,0=no menu
mov
lpszClassName,OFFSET ClassName;name of windows class
mov
hIconSm,0h
;iconhandle 0=search in rc file
;-----------------------------------------------------------------------------; API "LoadIconA" loads an icon defined in the resource file and store the
; handle in the "WNDCLASSEX" structure
;-----------------------------------------------------------------------------push
OFFSET IconName
;icon-string or icon resource id
push
hInstance
;our program handle
call
LoadIconA
;- API Function mov
hIcon,eax
;handle of newly loaded icon
;-----------------------------------------------------------------------------; API "LoadCursorA" loads a default system cursor, in this case we must set
Page 3

td_win32asm_110.asm
; hInstance to 0 and lpCursorName to a default system cursor value, here 32512
; Then we store the cursor handle in the "WNDCLASSEX" structure
;-----------------------------------------------------------------------------push
32512
;lpCursorName,default value in dezimal
push
0h
;hInstance, 0=default system cursor
call
LoadCursorA
;- API Function mov
hcursor,eax
;handle of the cursor
;-----------------------------------------------------------------------------; Now, after filled the "WNDCLASSEX" structure we call API "RegisterClassEx"
;-----------------------------------------------------------------------------push
OFFSET cbSize
;pointer to WNDCLASSEX structure
call
RegisterClassExA
;- API Function ;==============================================================================
; API "CreateWindowExA" creates an overlapped, pop-up, or child window with an
; extended style. The return value in EAX is the handle of the new window.
; This API sends a WM_CREATE message to the window procedure (WP1).
;-----------------------------------------------------------------------------push
0h
;lpParam, extra pointer data 0=no data
push
hInstance
;hInstance, handle of our program
push
0h
;hMenu, handle window menu 0=class menu
push
0h
;hWndParent, handle parent window 0=no
push
00000074h
;intnHeight, window height pixel
push
00000080h
;intnWidth, window width pixel
push
00000050h
;inty, vertical position window
push
00000150h
;intx, horizontal position window
push
04CA0000h
;dwStyle, look into WIN32.HLP
push
OFFSET WindowName
;lpWindowName, pointer to window name
push
OFFSET ClassName
;lpClassName, pointer to class name
push
0300h
;dwExStyle, extra window style 0=no
call
CreateWindowExA
;- API Function mov
hWnd,eax
;hwnd,return value=handle of window
;==============================================================================
; API "ShowWindow" function sets the specified window's show state.
;-----------------------------------------------------------------------------push
1h
;nCmdShow, show state 1=SW_SHOWNORMAL
push
hWnd
;hwnd, handle of window
call
ShowWindow
;- API Function ;==============================================================================
; API "UpdateWindow" updates the area of the specified window by sending a
; WM_PAINT message to the window if the window's update region is not empty.
;-----------------------------------------------------------------------------push
hWnd
;hwnd, handle of window
call
UpdateWindow
;- API Function ;==============================================================================
; API "GetMessageA" retrieves a message & places it in the specified structure.
;-----------------------------------------------------------------------------LoopGetMessage:
push
0h
;wMsgFilterMax, highest message value
push
0h
;wMsgFilterMin, lowest message value
Page 4

push
push
call
cmp
je

0h
OFFSET hWnd
GetMessageA
eax,0h
ExitPrg

td_win32asm_110.asm
;hWnd, handle of window who gets msg.
;lpMsg, pointer to MSG structure
;- API Function ;check if return value=0 (exit)
;if return value is 0 goto LABEL

;==============================================================================
; API "TranslateMessage" translates key code into ASCII character messages
;-----------------------------------------------------------------------------push
OFFSET hWnd
;lpMSG, pointer to msg structure
call
TranslateMessage
;- API Function - keyboard code
;==============================================================================
; API "DispatchMessageA" function dispatches a message to a window procedure.
;-----------------------------------------------------------------------------push
OFFSET hWnd
;lpMSG, pointer to msg structure
call
DispatchMessageA
;- API Function jmp
LoopGetMessage
;check for message again, goto LABEL
;==============================================================================
; Next we terminate our program (API=ExitProcess)
;-----------------------------------------------------------------------------ExitPrg:
push
hInstance
;push our programm handle to exit
call
ExitProcess
;- API Function ;##############################################################################
; The Window Procedure (API=RegisterClassExA) for this registered window.
;-----------------------------------------------------------------------------WP1:
push
ebp
;create stack frame
mov
ebp,esp
;
pushad
;push all register to the stack
mov
eax,WP1_uMsg
;move the message number to eax
;==============================================================================
; WM_CREATE (value=01h) message received ?
;-----------------------------------------------------------------------------WP1_uMsg_01h:
cmp
eax,1h
;check if WM_CREATE message recieved
jne
WP1_uMsg_02h
;if not goto label
;-----------------------------------------------------------------------------; In this case API "CreateWindowExA" creates a child window with a predefined
; window class name, here BUTTON !
; Parameter dwStyle defines the button: BS_PUSHBUTTON (0h) OR BS_BITMAP (80h)
; dwStyle defines also WS_VISIBLE ( 10000000h ) OR WS_CHILD ( 40000000h )
; Don't forget to take a look into WIN32.HLP ( CreateWindow ) and Windows.inc.
; In hMenu we PUSH the ID of the child window ( a value of your your choice )
; The return value in EAX is the handle of the new child window.
;-----------------------------------------------------------------------------push
0h
;lpParam, extra pointer data 0=no data
push
hInstance
;hInstance, handle of our program
push
0100h
;hMenu, the window ID
Page 5

td_win32asm_110.asm
push
WP1_hWnd
;hWndParent, handle parent window 0=no
push
00000020h
;intnHeight, window height pixel
push
00000025h
;intnWidth, window width pixel
push
00000010h
;inty, vertical position window
push
00000028h
;intx, horizontal position window
push
50000080h
;dwStyle, button style
push
OFFSET Bu01_WindowName
;lpWindowName, pointer to window name
push
OFFSET Bu00_ClassName
;lpClassName, pointer to class name
push
0h
;dwExStyle,
call
CreateWindowExA
;- API Function mov
pushbutton0100_hWnd,eax
;return value=handle of window
;-----------------------------------------------------------------------------; API "LoadBitmapA" loads a bitmap from the resource file
;-----------------------------------------------------------------------------push
80h
;lpBitmapName, bitmap resource ID
push
hInstance
;hInstance, handle of modul instance
call
LoadBitmapA
;- API Function mov
hObject,eax
;hObject, handle of graphic object
;-----------------------------------------------------------------------------; API "SendMessageA" sends a message to the Window Procedure lpfnWndProc
;-----------------------------------------------------------------------------push
eax
;lParam, handle of bitmap
push
0h
;wParam, first message ( IMAGE_BITMAP )
push
0F7h
;uMsg, message to send ( BM_SETIMAGE )
push
pushbutton0100_hWnd
;hWnd, handle of destination window
call
SendMessageA
;- API Function ;-----------------------------------------------------------------------------; API "SetTimer" creates a timer with the specified time-out value
;-----------------------------------------------------------------------------push
0h
;tmprc, address timer procedure,0=this
push
80h
;uTimeout, time-out value milliseconds
push
10h
;idTimer, timer identifier, nonzero
push
WP1_hWnd
;hwnd, handle of window for timer msg.
call
SetTimer
;- API Function jmp
WP1_return
;==============================================================================
; WM_DESTROY (value=02h) message received ?
;-----------------------------------------------------------------------------WP1_uMsg_02h:
cmp
eax,2h
;check if value=2h (WM_DESTROY)
jne
WP1_uMsg_111h
;if not 2h go to LABEL
call
My_CleanSystem
;- SubRoutine ;-----------------------------------------------------------------------------; API "PostQuitMessage" indicates to Windows a request to terminate
;-----------------------------------------------------------------------------push
0h
;nExitCode, exit code=wParam
call
PostQuitMessage
;- API Function popad
;pop all register back from stack
xor
eax,eax
;set eax to 0 to exit our program
mov
esp,ebp
;delete stack frame
pop
ebp
;
ret
10h
;return and clear stack
Page 6

td_win32asm_110.asm
;==============================================================================
; WM_COMMAND (value=111h) message recieved ?
;-----------------------------------------------------------------------------WP1_uMsg_111h:
cmp
eax,111h
;check if WM_COMMAND message recieved
jne
WP1_uMsg_112h
;if not goto label
;-----------------------------------------------------------------------------; Check extra message info menu control, "&Exit" item in menu bar,ID=1h clicked
;-----------------------------------------------------------------------------WP1_wParam_01h:
mov
eax,WP1_wParam
;extra info about the message in ax
cmp
ax,1h
;ID of "&Exit" item in rc file
jne
WP1_wParam_0100h
;if not 1h goto LABEL
;-----------------------------------------------------------------------------; API "MessageBoxA" creates a message box, we can choose if we want exit prg.
;-----------------------------------------------------------------------------push
4h
;uType, style, 4=MB_YESNO Button
push
OFFSET MB1Titel
;lpCaption,pointer to title text
push
OFFSET MB1Text
;lpText,pointer to text message box
push
WP1_hWnd
;handle of owner window 0=no owner
call
MessageBoxA
;- API Function cmp
eax,6h
;if return value=6h (IDYES) then exit
jne
WP1_return
;if return value=7h (IDNO) goto LABEL
;-----------------------------------------------------------------------------; API "DestroyWindow" function destroys the given window if we want exit prg.
;-----------------------------------------------------------------------------push
WP1_hWnd
;hwnd, handle of window to destroy
call
DestroyWindow
;- API Function jmp
WP1_return
;
;-----------------------------------------------------------------------------; Check extra message, button (PUSHBUTTON=0100h) clicked
;-----------------------------------------------------------------------------WP1_wParam_0100h:
cmp
ax,0100h
;ID of pushbutton window
jne
WP1_return
;if not 0100h goto LABEL
;-----------------------------------------------------------------------------; API "MessageBoxA" creates a message box, we can only click OK
;-----------------------------------------------------------------------------push
0h
;uType, style, 0=MB_OK Button
push
OFFSET MB2Titel
;lpCaption,pointer to title text
push
OFFSET MB2Text
;lpText,pointer to text message box
push
WP1_hWnd
;handle of owner window 0=no owner
call
MessageBoxA
;- API Function jmp
WP1_return
;
;==============================================================================
; WM_SYSCOMMAND (value=112h) message recieved ?
;-----------------------------------------------------------------------------WP1_uMsg_112h:
cmp
eax,112h
;check if WM_COMMAND message recieved
jne
WP1_uMsg_113h
;if not goto label
mov
eax,WP1_wParam
;extra info about the message
Page 7

cmp
jne
call
jmp

eax,0F060h
WP1_return
My_CleanSystem
WP1_return

td_win32asm_110.asm
;SC_CLOSE=0F060h received ?
;
;- SubRoutine -

;==============================================================================
; WM_TIMER (value=113h) message received ?
;-----------------------------------------------------------------------------WP1_uMsg_113h:
cmp
eax,113h
;check if WM_TIMER message recieved
jne
WP1_return
;if not goto label
;-----------------------------------------------------------------------------; API "DeleteObject" deletes a bitmap freeing all associated system resources
;-----------------------------------------------------------------------------push
hObject
;hObject, handle of graphic object
call
DeleteObject
;- API Function ;-----------------------------------------------------------------------------; API "LoadBitmapA" loads a bitmap from the resource file (depends on counter)
;-----------------------------------------------------------------------------cmp
counter,83h
;check counter, last bitmap
jb
Anim
;if counter below goto LABEL
mov
counter,80h
;reset counter to 80h, first bitmap
Anim:
push
counter
;lpBitmapName, bitmap resource ID
push
hInstance
;hInstance, handle of modul instance
call
LoadBitmapA
;- API Function mov
hObject,eax
;handle of bitmap graphic
inc
counter
;counter=counter+1
;-----------------------------------------------------------------------------; API "SendMessageA" sends a message to the Window Procedure lpfnWndProc
;-----------------------------------------------------------------------------push
eax
;lParam, handle of bitmap
push
0h
;wParam, first message ( IMAGE_BITMAP )
push
0F7h
;uMsg, message to send ( BM_SETIMAGE )
push
pushbutton0100_hWnd
;hwnd, handle of destination window
call
SendMessageA
;- API Function jmp
WP1_return
;
;==============================================================================
; API "DefWindowProcA" calls the window procedure to provide default processing
; for any window messages that an application does not process.
; This function ensures that every message is processed.
; It is called with the same parameters received by the window procedure.
;-----------------------------------------------------------------------------WP1_return:
popad
;pop all register from stack
push
WP1_lParam
;extra info about the message
push
WP1_wParam
;extra info about the message
push
WP1_uMsg
;the message number
push
WP1_hWnd
;handle of window who receives message
call
DefWindowProcA
;- API Function mov
esp,ebp
;delete stack frame
pop
ebp
;
Page 8

td_win32asm_110.asm
ret
10h
;return and clear stack
;##############################################################################
;******************************************************************************
; My own subroutine(s) for a compacter code resist here ...
;-----------------------------------------------------------------------------My_CleanSystem:
;-----------------------------------------------------------------------------; API "KillTimer" destroys the specified timer.
;-----------------------------------------------------------------------------push
10h
;uIDEvent, timer identifier
push
WP1_hWnd
;hWnd, handle window that installed timer
call
KillTimer
;- API Function ;-----------------------------------------------------------------------------; API "DeleteObject" deletes a bitmap freeing all associated system resources
;-----------------------------------------------------------------------------push
hObject
;hObject, handle of graphic object
call
DeleteObject
;- API Function ret
;******************************************************************************
;==============================================================================
; end Main = end of our program code
;-----------------------------------------------------------------------------end Main
;end of our program code, entry point
;==============================================================================
; To create the exe file use this commands with your Microsoft Assembler/Linker
;-----------------------------------------------------------------------------; ml.exe /c /coff td_win32asm_110.asm
;asm command
; rc.exe /v rsrc.rc
;rc command
; cvtres.exe /machine:ix86 rsrc.res
; link.exe /subsystem:windows td_win32asm_110.obj rsrc.obj ;link command
;==============================================================================

Page 9