Wednesday, June 11, 2008

A Simple Hello World Program in MASM

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

No comments: