1. Code File (.ASM):
.386
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
include user32.inc
includelib kernel32.lib
includelib user32.lib
DlgProc proto :DWORD,:DWORD,:DWORD,:DWORD
.data
appname db " My First Applicaton",0 ; you can use db for BYTE also
msgerror db "Error !",0
msgnotext db " No Text Entered",0
strformat db "%s %s",0
hello db "Hello",0
.data?
yourname db 30 dup(?) ; buffer to store name equal to C equivalent name[30];
hinstance dd ? ;a DWORD varibale to store the instance of Application
greetingmessage db 100 dup(?)
.const
IDD_DLGMAIN equ 1001
IDC_NAME equ 1003
IDC_LBLNAME equ 1002
IDC_CMDOK equ 1004
.code
start:
invoke GetModuleHandle,0; invoke is used to call a fucntion
mov hinstance,eax ;the finction returns value in Accumulater Register which is EAX in x86 machines...ofcourse its Asemlbly you need to know ;)
invoke DialogBoxParam,hinstance,IDD_DLGMAIN,0,addr DlgProc,0 ; addr DlgProc is C equivalent to &DlgProc
invoke ExitProcess,eax
DlgProc proc hwnd:DWORD,umsg:DWORD,wparam:DWORD,lparam:DWORD
.if umsg==WM_COMMAND
.if wparam==IDC_CMDOK
invoke GetDlgItemText,hwnd,IDC_NAME,addr yourname,sizeof yourname ;Fetch up the text from Textbox IDC_NAME and store it in the bufer
.if eax>0 ;If the textbox contains data because the function will return the lenght of string it fetched
invoke wsprintf,addr greetingmessage,addr strformat,addr yourname,addr hello
invoke MessageBox,hwnd,addr greetingmessage,addr yourname,MB_ICONINFORMATION
.else
invoke MessageBox,hwnd,addr msgnotext,addr msgerror,MB_ICONASTERISK
.endif
.endif
.elseif umsg==WM_CLOSE
invoke EndDialog,hwnd,0
invoke PostQuitMessage,0
.endif
xor eax,eax
Ret
DlgProc EndP
end start
2. Resource File (.RC) :
;This Resource Script was generated by WinAsm Studio.
#define IDD_DLGMAIN 1001
#define IDC_NAME 1003
#define IDC_LBLNAME 1002
#define IDC_CMDOK 1004
IDD_DLGMAIN DIALOGEX 0,0,250,94
CAPTION "A Hello World Application"
FONT 8,"MS Sans Serif"
STYLE 0x10cc0000
EXSTYLE 0x00000000
BEGIN
CONTROL "Enter your name :",IDC_LBLNAME,"Static",0x50000000,11,24,55,13,0x00000000
CONTROL "",IDC_NAME,"Edit",0x50010080,75,24,171,12,0x00000200
CONTROL "&OK",IDC_CMDOK,"Button",0x50010000,71,55,99,20,0x00000000
END
Wednesday, June 11, 2008
Tuesday, June 10, 2008
Microsoft Macro Assembler Tutorial (MASM)
Microsoft Macro Assembler Tutorial (MASM)
Perquisites:
• Basic Programming knowledge of Any Language
• Must know what a compiler, interpreter, assembler and linker are.
Introduction:
MASM is Assembler developed by Microsoft for assembly language programmers to be able to create Windows Applications using WINDOWS API’s.
It Contains set of include files and libraries which contains declarations of various windows API’s. In this tutorial I’ll be telling you basic features of MASM and how to code a simple Hello Application using MASM. For writing MASM apps I will use a free IDE (Integrated Development Environment) WinASM. WinASM is free and have good features like intellisence which is need of programmers in this fast coding world.
Basic Building Blocks:
A programming language consists of Data types, Expressions or Statements, Conditionals and Looping. Masm comprises of “Basically” 3 types of Data. I insist on the Word Basically because the Windows Data types declared in the header files like ‘HWND’, ‘LPCTR’ are none other then Aliases used for WORD and DWORD data type in MASM.
So there are basically three Data types in MASM:
1. BYTE: This is the smallest data type and uses 8 bit for its storage. its alias is 'db'.
2. WORD: This data type uses 16 bits and is generally referred to int/integer data type in other languages. its alias is 'dw'.
3. DWORD: This is called ‘Double Word’ and takes 32 bits for its storage and is referred to as long/unsigned long data type in other Languages. its alias is 'dw'.
So you write a program using these three basic data types.
Here I would like to give a general reference to you guys how and when to use these data types.
For storing an integer number you should use WORD data type,
For storing a Long Integer number you should use the DWORD data type and for storing a single character use BYTE Data type.
Strings are handled in different ways,(They are rather Array of bytes).
Now comes the general instructions used in MASM. You all are thinking that Assembly Programming must be tough but guys it isn’t masm contains macros which enables you to use English like statements such as if…else statement, while…end while statement. Now importantly comments are used by ‘;’ sign. All the coders must know how comments are crucial in Big Software Development
e.g. if you want to compare two variables , if they are equal or not, an assembly instruction should be as
CMP EAX, BYTE PTR [szName]
But in MASM you can use Conditions Macro .IF as
.IF EAX==BYTE PTR [szName]
.END IF
General Structure of a MASM Program:
Here I will write a general structure of a MASM program and I will explain it. I will be writing a simple program that will show a message box on the screen
.386
.model stdcall,flat
Include windows.inc
Include kernel32.inc
Includelib kernel32.lib
Includelib user32.lib
.data
szMessage db “Hello !”,0
szTitle db “First MASM Program”,0
.code
Start:
Invoke MessageBoxA, 0, addr szMessage, addr szTitle
Invoke ExitProcess, 0
End Start
Now the first line is ‘.386’ this tell the Assembler that the code will be compatible on Intel 80386 Processor and above. However processor below it will not be able to execute the program 100% accurate and may even crash.
The second line ‘.model stdcall,flat’ tells the Assembler that the code model to be used is for Protected mode and the parameters to the functions will be passed from right to left .
Now in the third line we are using include statement which is similar to C ‘s #include statement, that is this is including a header file ‘Windows.inc’ containing prototypes of WINDOWS API’s and Constants. Note: Header Files in ASM end with .inc extension. Then you will notice another preprocessor ‘Inclidelib’ this is a flexible preprocessor which tells which libraries to be linked while generating the EXE.
Now most importantly All the variables in MASM are declared in ‘.data’ section and all code is written in ‘.code’ section so I have declared 2 string variables in data section which are holding strings to be used in messagebox API.
Then I am writing the code in ‘.code’ section. The code section starts with an Entry point Label which you can name anything you wish. I named it Start. Note: The label should end with semi colon. All the code is written with in this label blocks. The label is then at the ended with the ‘end’ statement as end label.
Now I have Called the MessageBox function using Invoke macro. And this is the general basic tutorial of MASM. I hope I have given a brief intro of MASM to the existing programmers.
Regards,
Rohit
Perquisites:
• Basic Programming knowledge of Any Language
• Must know what a compiler, interpreter, assembler and linker are.
Introduction:
MASM is Assembler developed by Microsoft for assembly language programmers to be able to create Windows Applications using WINDOWS API’s.
It Contains set of include files and libraries which contains declarations of various windows API’s. In this tutorial I’ll be telling you basic features of MASM and how to code a simple Hello Application using MASM. For writing MASM apps I will use a free IDE (Integrated Development Environment) WinASM. WinASM is free and have good features like intellisence which is need of programmers in this fast coding world.
Basic Building Blocks:
A programming language consists of Data types, Expressions or Statements, Conditionals and Looping. Masm comprises of “Basically” 3 types of Data. I insist on the Word Basically because the Windows Data types declared in the header files like ‘HWND’, ‘LPCTR’ are none other then Aliases used for WORD and DWORD data type in MASM.
So there are basically three Data types in MASM:
1. BYTE: This is the smallest data type and uses 8 bit for its storage. its alias is 'db'.
2. WORD: This data type uses 16 bits and is generally referred to int/integer data type in other languages. its alias is 'dw'.
3. DWORD: This is called ‘Double Word’ and takes 32 bits for its storage and is referred to as long/unsigned long data type in other Languages. its alias is 'dw'.
So you write a program using these three basic data types.
Here I would like to give a general reference to you guys how and when to use these data types.
For storing an integer number you should use WORD data type,
For storing a Long Integer number you should use the DWORD data type and for storing a single character use BYTE Data type.
Strings are handled in different ways,(They are rather Array of bytes).
Now comes the general instructions used in MASM. You all are thinking that Assembly Programming must be tough but guys it isn’t masm contains macros which enables you to use English like statements such as if…else statement, while…end while statement. Now importantly comments are used by ‘;’ sign. All the coders must know how comments are crucial in Big Software Development
e.g. if you want to compare two variables , if they are equal or not, an assembly instruction should be as
CMP EAX, BYTE PTR [szName]
But in MASM you can use Conditions Macro .IF as
.IF EAX==BYTE PTR [szName]
.END IF
General Structure of a MASM Program:
Here I will write a general structure of a MASM program and I will explain it. I will be writing a simple program that will show a message box on the screen
.386
.model stdcall,flat
Include windows.inc
Include kernel32.inc
Includelib kernel32.lib
Includelib user32.lib
.data
szMessage db “Hello !”,0
szTitle db “First MASM Program”,0
.code
Start:
Invoke MessageBoxA, 0, addr szMessage, addr szTitle
Invoke ExitProcess, 0
End Start
Now the first line is ‘.386’ this tell the Assembler that the code will be compatible on Intel 80386 Processor and above. However processor below it will not be able to execute the program 100% accurate and may even crash.
The second line ‘.model stdcall,flat’ tells the Assembler that the code model to be used is for Protected mode and the parameters to the functions will be passed from right to left .
Now in the third line we are using include statement which is similar to C ‘s #include statement, that is this is including a header file ‘Windows.inc’ containing prototypes of WINDOWS API’s and Constants. Note: Header Files in ASM end with .inc extension. Then you will notice another preprocessor ‘Inclidelib’ this is a flexible preprocessor which tells which libraries to be linked while generating the EXE.
Now most importantly All the variables in MASM are declared in ‘.data’ section and all code is written in ‘.code’ section so I have declared 2 string variables in data section which are holding strings to be used in messagebox API.
Then I am writing the code in ‘.code’ section. The code section starts with an Entry point Label which you can name anything you wish. I named it Start. Note: The label should end with semi colon. All the code is written with in this label blocks. The label is then at the ended with the ‘end’ statement as end label.
Now I have Called the MessageBox function using Invoke macro. And this is the general basic tutorial of MASM. I hope I have given a brief intro of MASM to the existing programmers.
Regards,
Rohit
Subscribe to:
Posts (Atom)
