Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Simple Windows Forms Application in Assembly Language

align assembly

  • Please log in to reply
5 replies to this topic

#1 gokuajmes

gokuajmes

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 483 posts

Posted 31 March 2010 - 07:36 AM

You never knew it was soo hard to make a Windows forms work ! for those who don't like MS products please have a look at this tut and just reflect back on How much of a work Microsoft has done for YOU users , Hats off to those guys ^^ [ you rock ]

New Concepts:


Creating a dialog box based app
Using Child Window Controls - buttons
Creating a resource script
Function prototypes
Using windows messages



New API functions:



GetModuleHandle
DialogBoxParam
SetDlgItemText
SendMessage
EndDialog



In this section we will create a program with a simple dialog box with a button that displays a message and an exit button. Start WinAsm and open a new exe project. First we will create the dialog box template using WinAsm's Visual Resource Editor. This displays the controls on a grid inside your dialog box as it will appear in your program. The Toolbox and Dialog toolbars (from the View menu) allow you to add more controls and arrange them visually. Rather than typing out a resource script manually, the entire dialog box can be designed in this way.
Either select Project>Add New Rc, or rightclick in the project explorer bar and select Add New Rc. Visual mode is on by default so click on "Add New Dialog" (the first button in the Toolbox bar) and you should see this:


pic1..png


Now we will add a caption, 2 buttons and an editbox. Click the "Resources" tab at the bottom right of the screen, doubleclick the Caption box and enter "Simple Dialog Box Program". Now click on the "Edit" button (4th button on Toolbox) and draw on a box something like this:


pic2..png


Now in the explorer bar, click on IDC_EDIT1002 and below clear its Text field. Finally click on "Button" in the Toolbx bar and add 2 buttons, changing their Caption fields in the explorer bar to "Say Hello" and "Exit" like this:


pic3..png


The Dialog toolbar buttons allow you to align and size the controls to make them more pleasing to the eye. To see the resource script WinAsm has created for us, turn off visual mode by pressing F12, deselecting "Visual Mode" on the Resources menu or pressing the toolbar button:


pic3..png


Turning off visual mode will also allow you to type a resource script manually or use the one I have included in the sourcecode section by pasting in the script from dialogbox1.rc:


pic4..png


In order to begin writing the code around our dialog box template we need to know the names and control IDs of the dialog box, the editbox and the buttons as shown in the first 4 lines of the resource script above.
Select Untitled1.asm in the project explorer bar and paste in dialogbox1.asm from the sourcecode section:



pic5..png


The small "+" sign at the bottom left shows the start and end of the dialog box procedure. Clicking on it expands the whole procedure as shown below:


pic6..png


Finally save your project, resource script and asm files and press the Go All button. You should see this when you click on the "Say Hello" button.


pic7..png


Clicking "Go All" will compile the resource script as well as assembling and linking. If you alter the layout of the dialog box or its controls you can recompile the resource script without re-assembling and linking by selecting Make>Compile RC. Now we will analyse the code. The header and library files should be self-explanatory by now.

Prototypes
DlgProc proto : DWORD,: DWORD,: DWORD,: DWORD - First We declare the function prototype for DlgProc so we can refer to it with the addr operator when we invoke DialogBoxParam later on.


Variables
Message - our null-terminated "Hello World!" string.
hInstance - (defined in windows.inc as a DWORD) uninitialised but will hold the module handle of our dialog box for the call to DialogBoxParam.

Program Code
invoke GetModuleHandle - retrieves the module handle or instance handle of our program. This is the ID of your program and is used as a parameter to several API functions our program must call, so it's customary to retrieve it at the beginning of our program.

mov hInstance,eax - moves the result of the GetModuleHandle API from eax into the variable we declared for it above (upon returning from a Win32 function, the function's return value, if any, is placed in eax).

invoke DialogBoxParam - creates a modal dialog box and returns when the dialog box is destroyed. Takes 5 parameters: the instance handle, the dialog box template, the parent window handle, the address of the dialog box procedure, and dialog-specific data.
invoke ExitProcess - the final instruction as outlined in our skeleton chapter.


Dialog Procedure
The dialog procedure is a callback function that processes messages sent to the dialog box. It has four parameters and returns either true or false according to whether or not it processes the message. In this case the child window controls are sending messages to the dialog procedure by calling it and passing the message in the parameters.


The first line consists of the function declaration of DlgProc. Note the parameter:type pairs that follow PROC directive. The parameters consist of the window handle, a message identifier (uMsg), and two message parameters (wParam & lParam) which specify additional message information.
When child window controls send a message, the uMsg parameter contains WM_COMMAND and wParam contains the control ID. Message processing is accomplished by using an IF-ELSEIF-ENDIF series which first checks to see if uMsg contains a WM_COMMAND message from one of the controls or a WM_CLOSE message.

If the message is WM_CLOSE then EndDialog is called to destroy the dialog box. EndDialog doesn't do this immediately, it only sets a flag for the internal dialog box manager and continues to execute the next instructions.
If the message is WM_COMMAND then a nested IF-ELSEIF-ENDIF series checks the value in wParam to see how to respond - in this case whether the "Say Hello" button (ID 1003) or the "Exit" button (ID 1004) has been pressed.
SetDlgItemText - sets the text of a control (the editbox ID 1002).
SendMessage - sends a WM_CLOSE message for the dialog procedure.
The next line XOR eax,eax just zeroes eax before RET returns execution to DialogBoxParam and subsequantly ExitProcess is called.


Analysis in the Debugger
As usual we should open our app in Olly to see how the code has been assembled:

pic8..png

Since we are coding in asm, we get a nice clean dissassembly in Olly. The first square bracket on the left encloses the first 4 lines of our code and the second bracket encloses the dialog procedure which is less easy to understand at first glance. MASM has converted the high-level IF-ELSEIF-ENDIF constructs into compare (CMP) and conditional jump instructions. As before, the final block of instructions highlighted in yellow are jump thunks pointing to the Import Address Table.

Notes
The Name field of the dialog box and its controls can be changed to something more meaningful by doubleclicking in the visual editor. The script will be changed accordingly, eg:

pic9..png

These names can then be declared as constants in the asm file and used in the code instead of the numeric control IDs in order to improve readability:

pic10..png

There are some peculiarities regarding the dialog box template parameter passed to DialogBoxParam . If the dialog box name and control ID are defined in the resource script (as they are in line 1 of the example WinAsm has made for us) then the control ID alone can be passed to DialogBoxParam. If the name and control ID are also declared as a constant in the asm file then the name can be passed to DialogBoxParam. If the name and control ID are not defined in the resource script then the name may be declared as a string variable in the .data section of the asm file and passed to DialogBoxParam with the addr operator.
Including the line include resource.h at the top of the resource script allows the use of the descriptive names rather than the hex numbers of Styles which define the appearance of the dialog and its controls.

Now that the tutorial is over do you feel , microsoft does not produce quality products.[Open source I agree, you got no money so use it,But open source rocks more If you make something useful to give back for the community ] [ We learn technology ,we drink technology and smell technology But why don't we Teach what we learn

So I Eagerly Urge all the members here try to make applications that others may use [ I will be the first to appreciate and + rep you when u do . Even a simple application that writes a text file, A simple directory Size reader , etc ]
I am making a small Photo editor app for the Forum , with simple GDi + 3D text and rotations

  • 0

#2 Deadlock

Deadlock

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 17 April 2010 - 06:54 AM

Great Effort. Thanks for the nice tutorial.
  • 0

#3 gokuajmes

gokuajmes

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 483 posts

Posted 24 April 2010 - 07:18 PM

thank you , it was my pleasure. Do read all of my other tutorials:w00t:
  • 0

#4 try

try

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 10 July 2011 - 11:36 AM

@gokuajmes
Firstly sorry for digging this old thread.
But can i know what compiler @ ide are you using ?
I din't know there is existence of gui ide for ASM.
  • 0

#5 try

try

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 10 July 2011 - 09:36 PM

hi gokuajmes,
what IDE are you using in this picture ?
Posted Image
  • 0

#6 gokuajmes

gokuajmes

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 483 posts

Posted 11 July 2011 - 07:24 AM

It is WinAsm more info here
http://www.winasm.net/

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download