Masm32
Sitting at a coffee shop this afternoon I had the privilege to eaves drop on a couple of guys discussing how Assembly Language is dead as far as writing desktop applications is concerned. Hmm Assembler was the golden word. The word that got me glued in on their conversation.

If that was not enough they were up about how it is only used in device drivers, cracking software, building parts of Operating Systems and Embedded Systems. So I decided to prove them wrong by doing something else other than what they mentioned and a Windows App was the most appropriate candidate.

So its not a good habit to listen in but I thought it makes a great intro to this article. This is not meant to be a tutorial on X86 Assembly but just to show how easy it is to make the basic “Hello World” program in Windows. In the days to come I will probably have to compile a tutorial on X86 Assembly because I will use some terms here which will not be understood and the tutorial will explain this better.

For now let us focus on getting our program to run. I will assume in this article that you have some experience in programming. I am not talking about Server-side scripting. I am talking about programming in a language that you would need to go through the program-compile/build/assemble cycles because we are going to do exactly that and I will not be explaining what building, assembing or compiling is.

The first thing to do when wanting to write any computer program is having the right tools. In this case we will use Masm32 which can be downloaded here absolutely free. The good news is that you wont need anything else; well other than a PC or laptop with Windows 2000 or higher. I am sure you already have that since you are already reading this post.

Once the Masm32 download is complete, you will need to install it. It is quite simple. Extract the zip file, choose the drive you’d like to install Masm32 to and start the process and follow through to the end reading the warnings as you go on. In some cases you will get false alerts from Anti Virus programs so you might wanna probably disable your Anti Virus Software before installing Masm32.

Now that Masm32 is installed, the next thing you would do now is open a new document (File -> New). Then type the following:

.386
.model flat, stdcall
option casemap:none

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

.data
msg db "Hello World!", 0
cpt db "My 1st Ms-ASM Program"

.code
begin:
invoke MessageBox, NULL, addr msg, addr cpt, MB_OK
invoke ExitProcess, NULL
end begin

Basically this is the entire code for our little program. To explain each of these lines, here goes:

.386

This tells the Assembler what Processor architecture we are targeting. In this case we are the targeting the 80386 family of processors. These Processors have 32 bit registers. Current processors from Intel and AMD fall under this category.

.model flat, stdcall

In this line the “model flat” tells the Assembler that we are not using the segmented memory model which if my memory serves me right had a memory limit of something like 1MB (1024 Bytes). Anyway that’s a story for another day. Now the “stdcall” tells us how we are going to pass parameters to functions which would be either from right to left or the usual way. In this case it is the usual way. Other calling conventions in addition to stdcall are c, syscall, basic, fortran and pascal.

option casemap:none

This tells us not to worry about being case sensitive. That sounds so Microsoft. Okay, moving on…

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

These lines include the necessary definitions and declarations that are crucial to the platform we are developing on so far as Operating Systems are concerned.

“Windows.inc” contains Windows Specific declarations and definitions.
“Kernel32.inc” and “kernel32.lib” contains kernel32.dll specific headers and library code
“user32.inc” and “user32.lib” contains user32.dll specific headers and library code respectively. This DLL is crucial when working with controls such as buttons, dialog, statics, textedits etc.

.data

This is the section that shows where our data will be declared. Our data can be thought in terms of variables, identifiers  eg strings, integers etc.

msg db "Hello World!", 0
cpt db "My 1st Ms-ASM Program"

In these lines we the syntax is as, the first item is the label or identifier, the “db” here stands for the data size specifiers in this case  DWORD PTR (32bit). Others are WORD PTR (16bit) and BYTE PTR (8bit). The section in quotation marks is the actual value. In this case a string of characters. Finally the “0″ at the end represents a “NULL” terminating character, basically a “NULL” terminated string. Those familiar with C/C++ will understand this.

.code

This marks the beginning of the code segment.

Begin:

Is a label that marks a block of execution it must be there in every “.code” segment.

invoke MessageBox, NULL, addr msg, addr cpt, MB_OK
invoke ExitProcess, NULL

This two lines start with the command invoke which executes a function so to speak. In this case “MessageBox” and “ExitProcess” these details are defined in user32.inc and windows.inc. The Function is what is invoked and what follows after are parameters. The “addr” means the address of the data which in this case is “msg” and “cpt”. The MB_OK is the “Ok” button we are so familiar with in Windows programs. The “NULL” here is what the functions return. Similar to saying “void” in C/C++

end begin

This marks the end of the execution block or rather label.

Once this is done its time to save the file as “yourProgram.asm”. Please remember to save it in a sub-folder within the Masm32 folder otherwise it will not compile. I don’t understand why but that’s just the way it is.

Asm Save As

Once saved then you can Assemble and link the program by going to the “Project->Assemble and Link” Menu Item.

Assemble & Link

ASM Console

If everything goes well as seen in the console window above then you can go to the sub-folder in which you saved your ASM file where you should see three files. An ASM file, an OBJ file and the EXE executable. Simply Double-Click the executable you created and you should see something like this:

Hello World Program

Hello World Program

Well that’s as simple as it gets. Assembly Language is not for the faint of heart even though this one seems like a walk in the park. In the coming days I will do a more comprehensive tutorial on X86 Assembly and explain the nuts and bolts concerning it.

So this just goes to show it doesn’t need to take you ages to do develop software in  x86 Assembly. Once one   fully understands x86 Assembly, the possibilities are endless. I will be writing about X86 Assembly more in the next couple of days or so.

Read: A Brief Understanding of x86 Assembly Language

Related posts:

  1. A Brief Understanding of x86 Assembly Language
  2. How To Use A Windows Mobile PDA as a Modem
  3. Avoid problems when using .NET or other Technologies.
  4. Microsoft Security Essentials – Its About Time

13 Responses to “How To Program Windows 32 Using The Microsoft Assembler”

  1. Shamwow says:

    Hey Hunnie, great post! This is exactly what I was searching for

  2. Personally, I learned that migrating from Python version 2 to 3 was primarily a subject of relearning a few things: It for certain wasn’t as drastic a transfer as moving from Python to say the Java or Perl languages. Many of the changes have been long awaited, such as true division and changes to dict. Executing a print() is a whole bunch easier than System.out.println() in Java, so the learning curve is comparatively smaller and there are advantages to be realized.

  3. Thanks for great post! Will be bookmarking this article for future reference!

  4. Is definitely a really great article, thank you very much. I have saved this unique website to my own favorites.

  5. watch says:

    I am new to blogging and getting information out there. Your post here is watch very informative and gives me more insight as to creating an watch impact when commenting.

  6. sobbayi says:

    Thanks Ciera, if you note anything that can make the article more factual or errors in it you can mail me and I’ll make corrections. Thanks

  7. Not sure I agree with everything written, but some good points made.

  8. UGG Boots says:

    This article was very useful for a paper I am writing for my thesis.

    Thanks

    Bernice Franklin
    UGG Purses
    UGG Bags
    Classic Tall Chestnut

  9. asko says:

    Hi HI First time skipped here on your site, founde on Yahoo. Thank you very much for the reassurance. I completely agree with you. I will discuss your answer with my partner. Thanks again.

  10. Great read, well-written. The problem I think is that when visitors hover their mouse into your name and see that url directing to a blogger profile in their status bar, its more likely that they won

  11. Fast Shop says:

    Welcome First time jumped here on your site, founde on Google.

  12. Issac Maez says:

    Hey, thanks for this one. I read it like five times and I laughed each time. Hah-hah!

  13. [...] This is not meant to be an in-depth tutorial on x86 Assembly but it is simply a follow up of the article on programming Windows 32 using MASM32. The mentioned article can be found here. [...]

Leave a Reply