Win32 Assembly – Part-3

w32

Note: Visit Win32 Assembly – Part 2 before proceeding.

Tools of trade:  Masm32 & WinAsm

Output: 

3

Template: ( from Part-2 )

.386
.model flat, stdcall
option casemap:none

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

DialogProc Proto :DWORD, :DWORD, :DWORD, :DWORD

.data?
hInstance HINSTANCE ?

.code
Start:

invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,1001,NULL,Addr DialogProc,NULL
invoke ExitProcess,0



DialogProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

.if uMsg == WM_COMMAND


.elseif uMsg == WM_CLOSE
 invoke EndDialog,hWnd,NULL

.else
 mov eax,FALSE
 ret

.endif
 mov eax, TRUE
 Ret
DialogProc endp

End Start

Code:

.386
.model flat, stdcall
option casemap:none

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

DialogProc Proto :DWORD, :DWORD, :DWORD, :DWORD

.data
WindowTitle db "Win32 ASM Part-3",0
MessageText1 db "What is FUN?",0
MessageText2 db "Learning Assembly is FUN",0

.data?
hInstance HINSTANCE ?

.code
Start:

invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,1001,NULL,Addr DialogProc,NULL
invoke ExitProcess,0



DialogProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

.if uMsg == WM_COMMAND
 
    mov eax, wParam
 
    .if eax == 1003
 
       invoke SetDlgItemText,hWnd,1002,Addr MessageText2
 
    .elseif eax == 1004
 
       invoke SendMessage,hWnd,WM_CLOSE,NULL,NULL
 
    .endif
 

.elseif uMsg == WM_INITDIALOG
 
    invoke SetWindowText,hWnd,Addr WindowTitle
    invoke SetDlgItemText,hWnd,1002,Addr MessageText1

.elseif uMsg == WM_CLOSE
    invoke EndDialog,hWnd,NULL

.else
    mov eax,FALSE
    ret

.endif
    mov eax, TRUE
    Ret
DialogProc endp

End Start

Description:

includelib gdi32.lib
The file gdi contains function for windows graphics device interface.
It is associated with drawing applications and font management.

elseif uMsg == WM_INITDIALOG
WM_INITDIALOG
This msg is sent to the dialog box before dialog box is displayed. 
Typically, Dialog Box use this msg to initialize controls and do 
tasks such as appearance,etc before the dialog box is displayed.

invoke SetWindowText,hWnd,Addr WindowTitle
SetWindowText
This changes the text of specified window title bar if it has any. 
It also changes the text of any control also.

hWnd
We have already discussed in previous tuts. 

Addr WindowTitle
This is the address of the WindowTitle variable which is defined in
data section.

Output till now:
1

invoke SetDlgItemText,hWnd,1002,Addr MessageText1
SetDlgItemText
Sets the text or title of any control in dialog box

1002
It is the ID of the edit/text box.

Addr Messagetext1
This is the address of the Messagetext1 variable which is defined in
data section.

Output till now:
2

invoke SetDlgItemText,hWnd,1002,Addr MessageText2
SetDlgItemText is invoked the change the text in editbox when click
button is pressed.

Final Output:
3


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

Advertisements

Win32 Assembly – Part 2

w32

Note: Visit Win32 Assembly – Part 1 before proceeding.

Tools of trade:  Masm32 & WinAsm

Output: 

12

Code:

.386
.model flat, stdcall
option casemap:none

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

DialogProc Proto :DWORD, :DWORD, :DWORD, :DWORD

.data
MsgText db "This is hello world using dialog box",0

.data?
hInstance HINSTANCE ?

.code
Start:

invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,1001,NULL,Addr DialogProc,NULL
invoke ExitProcess,0



DialogProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

.if uMsg == WM_COMMAND
     mov eax, wParam
 
    .if eax == 1003
        invoke SetDlgItemText,hWnd,1002,Addr MsgText
 
    .elseif eax == 1004
        invoke SendMessage,hWnd,WM_CLOSE,NULL,NULL
 
    .endif

.elseif uMsg == WM_CLOSE
     invoke EndDialog,hWnd,NULL

.else
     mov eax,FALSE
     ret

.endif
     mov eax, TRUE
     Ret
DialogProc endp

End Start

Description:

Create a window as shown in below images:

23
45678

Use Template
Use the template given in Win32 Assembly - Basics. It will look 
something like below:
1

DialogProc Proto :DWORD, :DWORD, :DWORD, :DWORD
DialogProc
It is just a pointer to the dialog box. It is also called 
CALL Back Function (CBF). CBF is a function which is called when
an event takes place.

Proto 
Tells the assembler how many parameters are needed for the 
function.

MsgText db "This is hello world using dialog box",0
Define a string of bytes. Refer, Win32 Assembly - part 1 if not
clear.

hInstance HINSTANCE ?
hInstance is the variable name of type HINSTANCE which is left
uninitialized. Instance handle is returned by Operating System using API GetModuleHandle. Instance handle is a dword value that gives the 
actual start address (imagebase) in memory of the EXE or DLL.

invoke GetModuleHandle,NULL
This api will return the instance handle of the exe or dll.
The argument to this api is long pointer to module name 
(lpModuleName). It is given NULL in our case, as if arg is NULL then
GetModuleHandle returns a handle to the file used to create the
calling process
Ref: GetModuleHandle

mov hInstance,eax
moving the instance handle in hInstance variable so that we can 
use it later.

invoke DialogBoxParam,hInstance,1001,NULL,Addr DialogProc,NULL
DialogBoxParam creates a dialog box from dialog box resource with
defined properties.

Ref: DialogBoxParam

hInstance 
It is the instance handle which we already got it.

1001 
It is the id of the dialog box.

10

NULL 
because this parameter is for handle to parent window. Since, 
there is no parent windows. Therefore, it's NULL.

Addr DialogProc
Address of the Call Back Function(CBF). In this case, it is
DialogProc, we defined earlier.

NULL 
because this parameter is for the lparam parameter of dialog
box. Leave, it for now, you will understand it later.

invoke ExitProcess,0
This is use to exit the process gracefully.

DialogProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
Defining the procedure for the DialogProc method. 

Proc
With proc directive, you can specify registers to be saved, define
parameters to the procedure and assign symbol names to the parameters

HWND,UNIT are of DWORD type.

hWnd
It is the parameter name. It will be used to store the window handle.
In simple terms, consider it as "Who sends the message?"

uMsg
It is the parameter name. It will be used to store the msg that is
send to the OS. Consider, it as "What is the message?"

wParam
It is used when additional parameter is to be passed.

lParam
It is used when additional parameter to be passed is a pointer.

.if uMsg == WM_COMMAND
IF clause as used in different languages. So, what this line conveys,
WM_COMMAND msg is send whenever an event occurs like mouse click,
button click, etc.

    mov eax, wParam
    moving the content of wParam in eax.
    wParam stores the ID of the control who sent the message.
 
    .if eax == 1003
     ID 1003 is the for "Click Me" button.
     9    
    invoke SetDlgItemText,hWnd,1002,Addr MsgText
    if "Click Me" button is clicked, then this method is invoked.
        
    SetDlgItemText
    This is used to set the title or text of a control in Dialog box
    Ref: SetDlgItemText

    hWnd
    a handle to the dialog box that contains the control.

    1002
    It is the ID of the control where text/title is to be written.
    Here, in this case, it is the ID of the textbox.
11

    Addr MsgText
    Pointer to the message text to be written in textbox

    .elseif eax == 1004
    1004 is the ID for exit button

    invoke SendMessage,hWnd,WM_CLOSE,NULL,NULL
    SendMessage
    Sends the specified message to the window.

    Ref: SendMessage

    WM_CLOSE
    This is the message to close or terminate the window

    Next two parameters are set NULL as no additional parameters to
    be passed in wParam and lParam. Therefore, they are set NULL.

    .endif

.elseif uMsg == WM_CLOSE
if the user close the window using "X" button.
 invoke EndDialog,hWnd,NULL
 EndDialog
 Destroys the modal dialog box

 hWnd
 We already discussed

 NULL
 It is the return value. 

.else
 mov eax,FALSE
 Dialog box procedure always return TRUE or FALSE. When returning
 TRUE ( i.e any other value than 0 ) then no further message
 processing will be performed and a cycle is complete.
 When returning FALSE, it will mean that default window procedure 
 will be called.

.endif
 mov eax, TRUE
 Ret
DialogProc endp
DialogProc ends here.

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

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.

Win32 Assembly – Basics

w32
Template:

.386
.model flat, stdcall
option casemap:none

.data

.data?

.Const

.code
Start:
End Start

Description:

.386

It is the assembler directive, which tells to use the 80386 
instruction set. There are many predecesor of this like 80486,
80586,etc

.model flat, stdcall

It tells which memory model to use. Under win32, there is only one 
memory model i.e flat. Under DOS, memory is divided in 64k segments
but under win32, momory is devided in 4GB seg. Therefore, you need
not to take of segment registers under win32.

stdcall

This is used to tell, which convention is to be used, means in a 
function, parameters are passed left to right in stack or right 
to left and who will adjust the stack once function is finished 
it's procedure. Generally, parameters are passed right to left in 
stack and it is the duty to callee to adjust the stack.MODEL is an 
assembler directive that specifies memory model of your program. 
Under Win32, there's only on model, FLAT model.MODEL is an assembler
directive that specifies memory model of your program. Under Win32,
there's only one model, FLAT model.

option casemap:none

This tell to consider words differently except few identifiers.

Example:  name and Name are different

There are mainly two sections viz Data and Code section.

Data seciton is divided into 3 sections viz Data, .Data? and Const

.data

This section is used to declare the variables and initializing them 
here.

Example:  MessageTxt db "Hello World"

This seciton constitutes the size of executable.

.data?

This section is used for declaring uninitialized variables like 
buffer.

This section doesn't constitute the size of executable. Variables 
are initialized at runtime

.Const

This section is similar to .data section but with one condition i.e 
it is read only. So, once you declare here, then it can't be changed.

.Code 

As you would have already guessed what comes in this section. This 
section includes the whole logic of your program. 

Start:

End Start

Code Section begins with a label. It isn't need to be Start means 
you can name it anything but it should end with "End" keyword.

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