Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / ASM
Article

32-bit Assembler is Easy

Rate me:
Please Sign up or sign in to vote.
2.78/5 (21 votes)
30 Dec 2006CPOL3 min read 63.3K   1.1K   39   9
Maximum performance, reduced size applications using 32-bit assembly is easy

Sample Image - assembleriseasy.jpg

Introduction

Contrary to popular legend, if you can write a Windows application in a compiler based language ( C/C++, Visual Basic etc), you can also write it in Assembly, especially if you choose a Macro Assembler such as MASM.

Why develop in assembler?

1. When written correctly, assembly language produces high speed, reduced final size executables that are beyond the capacity of the best compilers.

2. Using an assembler, you can write dynamic link libraries with the above mentioned advantages, that can be used from any other language you use ( such as C/C++, VB etc )that can call a DLL.

3. Similarly, writing a static library, you are able to directly link them into your programs written in other languages since it will be in the object module format that is used by the Visual C++ compiler.

Bare Bone Executable

.386

Tells the assembler to use the 80386 instruction set. This is the safest bet unless you want to use instructions available only on later processors such as .486, .586 etc.

.MODEL FLAT, STDCALL

.MODEL specifies the memory model of the program. Only one model is applicable under Win32 no matter what type of application you develop (standard executable, dll, static library, console etc), the FLAT one. That is to say, we need not be concerned with memory model or segments like we used to do the old days of the 16-bit world. The memory is a  large continuous space of 4 GB and hence, you don't have to play with segment registers.

STDCALL informs the assembler how the parameters are passed to the functions. You don't need to bother about this in your first steps.

OPTION CASEMAP:NONE

Function names etc are case sensitive. There are more options but you do not need to bother for them now.

Include  ...

This directive tells the assembler to open the file the name of which follows and place its contents  there. In our case we used WINDOWS.INC, kernel32.inc, user32.inc since our program needs definitions of constants/structures and function prototypes that reside there.

IncludeLib ...

The easiest way to tell the assembler which import libraries your program uses. In our case, we need user32.lib and kernel32.lib.

.DATA

A section that contains initialized data of your program (variables). In our example, two (multi)BYTE variables are defined, namely MsgCaption and MsgBoxText. Something like string variables in the High level languages.

The .DATA? can be used for uninitialized data and the .CONST section contains declaration of constants (can never be modified in your program).

You don't have to use all three sections in your program. Declare only the section(s) you want to use.

.CODE

This is where your code resides

Start:

    your code here

End Start

where Start is any arbitrary label used to specify the extent of your code. Both labels must be identical.

The above could be considered as a skeleton project that is only missing the actual code. In our example we have two lines of code with Windows API function calls:

  Invoke MessageBox, NULL,Offset MsgBoxText, Offset MsgCaption, MB_OK
  Invoke ExitProcess,NULL

You can assemble our sample code with the MASM assembler using:

  ml  /c  /coff  /Cp assembleriseasy.asm

and link with:

  link /SUBSYSTEM:WINDOWS /RELEASE /VERSION:4.0 assembleriseasy.obj

Please note that instead of using command line, there are a lot of tools most (if not all) of which are free, to help you build your applications easily.

Conclusion

I know I haven't gone into any detail of any assembler instructions but I did this on purpose. I just wanted you to see the basic structure of an assembly listing file to verify that it is simple enough to start programming in Assembly.

Assembler Links

WinAsm Studio IDE, tutorials, source code, Assembler board: http://www.winasm.net

MASM32 is a working development for 32 bit Microsoft assembler (MASM): http://www.masm32.com/

Ewaynes's Win32 Assembly Homepage: http://asmedit.massmind.org

The site of flat assembler http://www.flatassembler.net/

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Cyprus Cyprus
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionEasy Assembler Pin
Ramon Sala14-Jun-12 7:49
Ramon Sala14-Jun-12 7:49 
QuestionNice stuff Pin
TheRaven18-Sep-11 13:17
TheRaven18-Sep-11 13:17 
GeneralMore Resources Pin
kbsoftware31-Dec-06 17:32
kbsoftware31-Dec-06 17:32 
GeneralRe: More Resources [modified] Pin
akyprian1-Jan-07 6:28
akyprian1-Jan-07 6:28 
GeneralRe: More Resources Pin
S Douglas1-Jan-07 23:24
professionalS Douglas1-Jan-07 23:24 
Clicketies Smile | :)

Resource 1
http://webster.cs.ucr.edu/AoA/index.html[^]
Resource 2
http://webster.cs.ucr.edu/AsmTools/index.html[^]
Resource 3
http://webster.cs.ucr.edu/AsmTools/HLA/index.html[^]



I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:


GeneralRe: More Resources Pin
akyprian2-Jan-07 0:50
akyprian2-Jan-07 0:50 
GeneralRe: More Resources Pin
S Douglas2-Jan-07 1:15
professionalS Douglas2-Jan-07 1:15 
GeneralRe: More Resources Pin
akyprian2-Jan-07 4:27
akyprian2-Jan-07 4:27 
GeneralRe: More Resources Pin
yeohhs14-Apr-09 19:48
yeohhs14-Apr-09 19:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.