Anda di halaman 1dari 5

td_win32asm_000.

asm
;==============================================================================
;
Test Department's WINDOWS 32 BIT x86 ASSEMBLY TUTORIAL'S
000
;==============================================================================
;==============================================================================
; ==> Part 000 : the basic stuff about windows 32 bit x86 assembly
;
NOTE : PROGRAM BELOW ONLY STARTS AND EXITS IMMEDIATELY !!!
;-----------------------------------------------------------------------------; I don't like proc and invoke statements, everything here is pure assembler !
; Muchas gracias and my best wishes flys to: Iczelion, _HaK_, _masta_
; I learn most of this stuff from your tut's ...
; This tutorials are written for MASM32 package and will work well with it.
; You can get MASM32 via ==> http://www.movsd.com
;==============================================================================
; ==> What you need:
;-----------------------------------------------------------------------------; 1. Basic knowledge of assembly language and operating systems.
;
C64, Amiga, Atari ST, Apple, MSDos 6 or Atari XL (like me) is OK.
; 2. An Intel 386, better 486, or greater computer with Windows 9x.
; 3. MASM32 package, then you have all you need and more to start !
; 4. The API reference WIN32.HLP and the resource file reference RC.HLP.
; 5. The Assembler Book, Addison-Wesley, Trutz Eyke Podschun - buy it.
; 6. A debugger like W32DASM or SOFTICE will be a usefull help.
;==============================================================================
; ==> Helpfull notes about assembly language and Windows 9x:
;-----------------------------------------------------------------------------; 1. The main note is the use of predefined API functions (OS functions).
;
In WIN32.HLP you find a list of API functions and parameters names.
;
API names are case sensitive !
; 2. You must set the correct path to the include files in your ASM file.
;
API functions resist in the *.lib files you include.
; 3. You must declare all API functions you want use in the ASM file.
;
Look into *.inc file,not *.lib file where the function resist,for help.
;
Example: MessageBoxA resist in USER32.LIB, declare is in USER32.INC !
; 4. API parameter must PUSHed to the stack before the function is called.
;
Important: PUSH the last parameter first, then the second-last ...
;
You can find default PARAMETER NAMES in the file WIN32.HLP.
;
Look for default PARAMETER VALUES into WINDOWS.INC (it's a text file).
; 5. API functions give you one or more return value mostly in EAX !
;
In WIN32.HLP you find more information about the return values.
; 6. Some API functions needs a DATA STRUCTURE defined in the .DATA section.
;
This structure is a parameter block used by the specified API function.
;
Structures could be nested !
;
It is always a good idea to align this structures to a doubleword border !
; 7. A basic mask for our assembler programs is included in this tutorial 000.
;
Take a look BELOW and learn how it works to increase your knowledge.
; 8. If you want add a menu bar, icon, sound, bitmap graphic etc. to your
;
program you must create a resource file and link it with the asm file.
;
A basic rc.file named RSRC.RC is included in this tutorial 000.
;==============================================================================
; ==> The API function, an example:
;-----------------------------------------------------------------------------; 1. For example we create a Message Box with a YES and NO button.
;
The API function call is "MessageBoxA"
; 2. Switch to our Api reference(WIN32.HLP),search for "MessageBox",and find:
;
;
int MessageBox
;
(
;
HWND hWnd,
// handle of owner window
;
LPCTSTR lpText
// address of text in message box
;
LPCTSTR lpCaption,
// address of title of message box
;
UINT uType
// style of message box
;
);
;
;
"hWnd" if no window calls our MessageBox we set it to 0=no window
;
"lpText" is a pointer to our textstring shown in the Message Box
;
"lpCaption" is a pointer to our textstring shown in the titel bar
;
"uType" is the type of the button in the Message Box
;
;
So we look for possible parameter names in WIN32.HLP and choose
;
MB_YESNO button.
;
We can use this parameter name or, i prefer, the parameter value.
;
For checking the parameter value of the parameter name MB_YESNO
;
we search in WINDOWS.INC for the text MB_YESNO and find:
;
MB_YESNO
equ 4h
Page 1

td_win32asm_000.asm
; 3. API functions gives you a return value (WIN32.HLP) mostly in EAX.
;
It is up to you to react to this values, for example you can store it
;
for future use.
; 4. Possible return values for our Message Box with uType MB_YESNO (4h)
;
are stored in EAX, IDYES (6h) or IDNO (7h).
;
Search in WINDOWS.INC for the text IDYES and for IDNO and find:
;
IDYES
equ 6
;
IDNO
equ 7
; 5. So our program looks like this:
;
;
--- Declaration of the API function --;
MessageBoxA
PROTO :DWORD, :DWORD, :DWORD, :DWORD
;
;
--- Define the Text Strings for the Message Box --;
.DATA
;
MB1_Name
db "Test Department",0
;message box name
;
MB1_Text
db "Realy exit ?",0
;message box text
;
;
--- This is the program code inside the CODE section --;
.CODE
;
push 4h
;uType, 4h=MB_YESNO Button
;
push OFFSET MB1_Name ;lpCaption, pointer to message box name
;
push OFFSET MB1_Text ;lpText, pointer to message box text
;
push 0h
;hwnd, handle of owner window, 0=no owner
;
call MessageBoxA
;- API Function ;
cmp
eax,7h
;check if return value in EAX=7h (IDNO)
;==============================================================================
; ==> The API function with a data structure, an example:
;-----------------------------------------------------------------------------; 1. In this example we use the API function "GetMessageA".
;
This function retrieves a message and places it in the specified structure
;
For other API functions YOU (!) must fill the structure with the correct
;
values before you call the API function.
;
Do it very carefully; if one parameter is wrong the whole program crash.
;
Please imagine that a structure can include another structure !
;
For example : member Point of the Msg structure is also a structure !
; 2. Switch to our Api reference (WIN32.HLP), search for "GetMessageA" :
;
;
BOOL GetMessage(
;
LPMSG lpMsg,
// address of structure with message
;
HWND
hWnd,
// handle of window
;
UINT
wMsgFilterMin, // first message
;
UINT
wMsgFilterMax, // last message
;
);
;
; 3. You see this API functions needs a pointer to a structure where extra
;
information were stored.
; 4. If an API function needs a data block structure you must create it in
;
the DATA section.
; 5. So switch to the API reference (WIN32.HLP), search for MSG and POINT :
;
;
typedef struct tagMSG { // msg
;
HWND
hwnd
;handle of the window whose window procedure receives
;
;the message
;
UINT
message
;Specifies the message number
;
WPARAM wParam
;Specifies additional information about the message
;
LPARAM lParam
;Specifies additional information about the message
;
DWORD time
;Specifies the time at which the message was posted
;
POINT pt
;Specifies the cursor position, in screen coordinates
;
;when the message was posted ( POINT structure )
;
} MSG
;
;
; 6. Well, our program looks like this:
;
;
--- Define the structure MSG --;
.DATA
;
align 4
;align structure to a doubleword border
;
hwnd
dd 0h
;handle of window who receives message
;
message
dd 0h
;the message number
;
wParam
dd 0h
;extra info about the message
;
lParam
dd 0h
;extra info about the message
;
time
dd 0h
;time the message was posted
;
xpt
dd 0h
;cursor x-position message posted, POINT struc
;
ypt
dd 0h
;cursor y-position message posted, POINT struc
;
;
--- This is the program code inside the CODE section --;
.CODE
Page 2

;
;
;
;
;
;

push
push
push
push
call
cmp

0h
0h
0h
OFFSET hwnd
GetMessageA
eax,0FFFFFFFFh

td_win32asm_000.asm
;wMsgFilterMax, highest msg. value
;wMsgFilterMin, lowest msg. value
;hWnd, 0=get any message
;lpMsg, push address of msg structure
;- API Function ;return value or error code (0FFFFFFFF) in EAX

;==============================================================================
; ==> The resource file:
;-----------------------------------------------------------------------------; A resource is a binary data that a resource compiler or developer adds to an
; application's executable file.
; A resource can be either standard or defined.
; The data in a standard resource describes an icon, cursor, menu, dialog box,
; bitmap, enhanced metafile, font, accelerator table, message-table entry,
; string-table entry, or version.
; An application-defined resource, also called a custom resource, contains any
; data required by a specific application.
; A basic rc.file named RSRC.RC is included in this tutorial 000.
; Look in the resource help file (RC.HLP) for extra information !
;
; Now look into the file rsrc.rc and let's go step by step:
;
; 1. Define an icon, bitmap and sound you want use in the ASM file:
;
; TDIcon
ICON
td_jolly.ico
;"TDIcon"=icon name
;
;"ICON"=menu keyword
;
;"td_jolly.ico"=filename of the icon
; TDBitmap BITMAP
picture.bmp
;"TDBitmap"=bitmap name
;
;"BITMAP"=menu keyword
;
;"picture.bmp"=filename of bitmap
; TDWave
WAVE
sound.wav
;"TDWave"=wave name
;
;"WAVE"=menu keyword
;
;"sound.wav"=filename of the wave
; 2. Here the menu description starts:
;
; TDMenu
MENU
;"TDMenu"=menu name,"MENU"=menu keyw.
; {
;"{"=start of menu, same as "BEGIN"
; MENUITEM "&Exit", 1
;"MENUITEM"=menu keyword
;
;"&Exit"=MENUITEM Textstring
;
;"&"=the next letter is underlined
;
;"1"=the MENUITEM ID
; MENUITEM "&Copy", 2
;"MENUITEM"=menu keyword
;
;"&Copy"=MENUITEM Textstring
;
;"&"=the next letter is the hot key
;
;"2"=the MENUITEM ID
; }
;"}"=end of MENU, the same as "END"
;==============================================================================
; ==> Additional information about the resource file:
;-----------------------------------------------------------------------------; 1. MENUITEM statement doesn't invoke a popup menu when selected:
;
;
MENUITEM "&Exit", ID [,options]
;
; 2. The ID is a number that identify the menu item in the message send to
;
the window procedure (lpfnWndProc) when the menu item is selected.
;
You can use one option or combine them, for example GRAYED,MENUBREAK.
;
INACTIVE and GRAYED cannot be combined together.
;
Available options are:
;
;
GRAYED
- The menu item is inactive, and it does not generate a
;
WM_COMMAND message. The text is grayed.
;
INACTIVE - The menu item is inactive, and it does not generate a
;
WM_COMMAND message. The text is displayed normally.
;
MENUBREAK - This item and the following items appear on a new line of
;
the menu.
;
HELP
- This item and the following items are right-justified.
;
; 3. POPUP statement invokes a popup window when selected:
;
;
POPUP "&Exit" [,options]
;
{
;
[menu list]
;
}
;
; 4. SEPARATOR statement will draw a horizontal line in the popup window:
;
Page 3

td_win32asm_000.asm
;
POPUP "&Exit" [,options]
;
{
;
[menu item1]
;
MENUITEM SEPARATOR
;
[menu item2]
;
}
;
; 5. The next step after you are finished with the menu resource script is
;
to reference it in your program.
;
If you declare your menu in the WNDCLASSEX Structure, API-function
;
is RegisterClassEx, this menu becomes your default menu for every
;
window you create from this class (0=no default menu):
;
;
.DATA
;
MenuName db "TDMenu",0
;"TDMenu" your menu name
;
;
.CODE
;
mov
lpszMenuName,OFFSET MenuName
;pointer to menu name,0=no
;
; 6. If you load your menu and PUSH the handle into hMenu, before you call
;
the API function CreateWindowClassEx, you can get individual menus
;
for every window.
;
;
.DATA
;
MenuName db "TDMenu",0
;"TDMenu" your menu name
;
hMenu
dd 0h
;handle of your menu
;
;
.CODE
;
push
OFFSET MenuName
;pointer to the menu name
;
push
hInstance
;handle of your program
;
call
LoadMenuA
;API function, loads menu
;
mov
hMenu,eax
;get the menu ID
;
; 7. When the user selects a menu item, the window procedure, defined in the
;
API function RegisterClassEx, lpfnWndProc will receive a WM_COMMAND
;
message.
;
The low word of wParam contains the menu ID of the selected menu item.
;
It's up to you to react to this parameter, in ax, in your lpfnWndProc
;
routine.
;##############################################################################
;******************************************************************************
; BELOW IS THE BASIC MASK OR OUR ASSEMBLER PRORAMS.
; PROGRAM ONLY STARTS AND EXITS IMMEDIATELY !
; It is always the same procedure if we create a program, we must INCLUDE *inc
; and *lib files, declare API FUNCTIONS, define CONSTANTS, define DATA and at
; last write the new CODE into the code area.
; Feel free to assemble and link this program but do not be afraid if nothing
; happens, it is only a basic mask with no other functions than start & exit.
;******************************************************************************
;##############################################################################
;==============================================================================
; 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
; You must set the correct path to the include and library files
;-----------------------------------------------------------------------------include \masm32\include\windows.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
;==============================================================================
; Declaration of used API functions,take a look into WIN32.HLP and *.inc files
; GetModulHandle= example of an API function
; PROTO
= one or more parameter must pushed to the stack before call
; :DWORD
= the parameter, in this case doubleword (32 Bit)
;-----------------------------------------------------------------------------GetModuleHandleA
PROTO :DWORD
ExitProcess
PROTO :DWORD
;==============================================================================
; .const = the constants area starts here,constants are defined & fixed
Page 4

td_win32asm_000.asm
; example_const = the constant name
; equ
= the value for this constant name follows
; 0Ah
= the value in hexadezimal
;-----------------------------------------------------------------------------.const
example_const
equ 0Ah
;not used, example only
;==============================================================================
; .Data = the data area starts here, datas are defined but not fixed
; db
= databyte
1 Byte (8 Bit)
; dw
= dataword
2 Byte (16 Bit)
; dd
= doubleword
4 Byte (32 Bit)
; Textstrings are databytes and must be terminated by ,0
; ,13,10 means control and line feed
; db 41h dup (0)= reserves 41 hexadezimal databytes initialized to zero
;-----------------------------------------------------------------------------.Data
example_text
db "First row",13,10
;not used,example only
db "Second row",0
;
hInstance
dd 0h
;our program handle
;==============================================================================
; .Data? = the data? area starts here, not defined and not fixed
;-----------------------------------------------------------------------------.data?
example_data?
dd ?
;not used,example only
;==============================================================================
; .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
;value in eax=handle of our program
;==============================================================================
; Try to insert your code here, for example a simple message box.
;------------------------------------------------------------------------------

;==============================================================================
; Next we terminate our program (API=ExitProcess)
;-----------------------------------------------------------------------------ExitPrg:
;label for future use
push
hInstance
;push our programm handle to exit
call
ExitProcess
;- API Function ;==============================================================================
; 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_000.asm
;asm command
; rc.exe /v rsrc.rc
;rc command
; cvtres.exe /machine:ix86 rsrc.res
; link.exe /subsystem:windows td_win32asm_000.obj rsrc.obj
;link command
;==============================================================================

Page 5

Anda mungkin juga menyukai