Win32 Assembly – Part 1

w32

Note: Visit Win32 Assembly – Basics before proceeding.

Tools of trade:  Masm32 & WinAsm

Output: 

Hello World

Code:

.386
.model flat, stdcall
option casemap:none

include windows.inc
include kernel32.inc
include user32.inc
includelib kernel32.lib
includelib user32.lib

.data
MessageText db "Hello World",0
CaptionText db "Welcome to ASM",0

.code
Start:

invoke MessageBox,NULL,Addr MessageText,Addr CaptionText,MB_OK
invoke ExitProcess,0

End Start

Description:

include windows.inc

This is included so that we can used predefined constants and 
structures. It is a master include file that defines all the 
Windows data types, function calls, data structures, and constant 
identifiers so you can refer to them by name in your code. Of 
note here is that windows.inc defines NULL and MB_OK so that 
these can be used by name to make the code more readable.

include kernel32.inc

This contains the prototypes for functions in kernel32.dll

include user32.inc

This contains the prototypes for functions in user32.dll

includelib kernel32.lib

It is required to invoke ExitProcess api.

Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658%28v=vs.85%29.aspx

includelib user32.lib

It is required to invoke MessageBox api.

Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx

.data
MessageText db "Hello World",0

Defining a array of bytes containing string "Hello World". This is 
the message we want to display.

CaptionText db "Welcome to ASM",0

Defining a array of bytes containing string "Welcome to ASM". This 
is the title of the window.

Note: I have null terminated both the strings. 
Reason?? Left for the home work and try yourself.

.code
Start:

invoke MessageBox,NULL,Addr MessageText,Addr CaptionText,MB_OK

Invoke is similar to call function except one difference. Invoke 
does type checking of parameters whereas call does not. So, to 
invoke to work, we have included *.inc file for the respective 
.dll as *.inc file contain the prototypes for functions in .dll

MessageBox api:

This api is defined in user32.dll. It has 4 Parameters:

hWnd [in, optional]

    Type: HWND

    A handle to the owner window of the message box to be created. If
    this parameter is NULL, the message box has no owner window.

lpText [in, optional]

    Type: LPCTSTR

    The message to be displayed. If the string consists of more than
    one line, you can separate the lines using a carriage return 
    and/or linefeed character between each line.

lpCaption [in, optional]

    Type: LPCTSTR

    The dialog box title. If this parameter is NULL, the default 
    title is Error.

uType [in]

    Type: UINT

We have passed the following 4 arguments:

NULL - there is no parent window.
addr MessageText - address of our text string.
addr CaptionText - address of our caption.
MB_OK - one of a set of pre-defined styles.

Addr

This give the address of the string. For above, address of 
MessageText and CaptionText.

invoke ExitProcess,0

This calls the ExitProcess with argument 0 i.e the return value. 
This api is invoked to exit the program gracefully.

End Start

Note: If you have any doubt, ask in the comment section. It will help others also.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s