Click here to Skip to main content
Click here to Skip to main content

Assembly Programming with Visual Studio 2010/2012

, 25 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
An example of how to program in assembly by using Visual Studio 2010 or 2012

Introduction 

This article provides a simple example on how to write a small program in x86 assembly language. The technologies used will be MASM (the Microsoft Assembler now distributed with Visual Studio) and Microsoft Visual Studio 2010 or Visual Studio 2012. Assembly programs offer the advantage of speed and full control over things that in other programming languages you cannot even touch. The example is a very simple example of basic text encryption and entails all basic assembly commands that one can use to start dealing with the magical realm of low level programming. And knowing how the computer responds at that level is crucial for someone who wants to be a programmer. Again, the main advice is one: EXPERIMENT! Try various commands you find in MASM manuals, change commands on your own, play with the memory and the registers.

Programming in Assembly

Programming in Assembly means telling the computer how to do things in a much more detailed manner than you would do with a High Level Programming Language (like C++).

Example Program

I have written a small introduction to assembly here . For the purpose of this small tutorial, I am using as an example a simple encoding/decoding program (HuoCodec) which I distribute in the Download Files section of this article (and which is explained in more details here) . This program accepts input from the keyboard (letters and numbers typed by the user) and "encodes" it by just adding a value to the ASCII code value of the typed letter. 

Assembly basics explained 

Assembly has everything to do with memory. Most of the time, you will have to move data from one place (register) of the memory to another place in the memory. This is conducted with the mov command. For example, the command...

mov     AscChar, al

...moves the contents of the AL memory register to the memory segment representing variable AscChar (this is the variable which holds the character entered by the user).

You can also conduct operations on memory, like for example, adding a value to an existing value stored in the memory. The command...

add	al, 2

...adds 2 to the contents of the AL memory register. This is the "key" with which we "encrypt" the text entered by the user. In this case, when the user enters 'a', the program will show 'c' in the screen (since it will have added 2 to the ASCII value of the 'a' the user entered).

The flow of an assembly program can be controlled by comparing two values with the cmp command...

cmp     al, 5

...and then reconvert the flow where we want depending on the result of the comparison. For example, when we want to jump to point 'endLoop' (which denotes the end of the program by putting a label with that name in the code) if AL is equal to 5, then we use the je (Jump if Equal) command:

je      endLoop

The respective part is commented out in the code distributed. You can experiment on your own and see what happens in case you put other similar commands in the code, like jz (Jump if Zero).

DECRYPTION EXPERIMENT: In case you want to decrypt a message encrypted with that program, just create a new program with -5 (instead of 5) added to the AL register!

 

MASM Commands

The Microsoft MASM Assembler has embedded some ready-made functions that can be used to perform specific tasks. For example, in order to get the character entered by the user in a command line window, we invoke the crt_getch function in our project:

call    crt__getch

Similarly, when we want to print out something on the screen, we use the StdOut function:

invoke  StdOut , offset AscChar

EXPERIMENT: Find out for yourselves what the Locate function does.

IMPORTANT NOTE: You have to include the related MASM libraries in your project in order to use the above mentioned functions. In this project, include the masm32rt.inc file in the \masm32\include\ folder as it is stated in the 'include' statement of the source code (it is distributed with MASM and you will also find it in the zip file from this site).

How to Use VS2010 to Write Assembly

Using Visual Studio to write an assembly program may be tricky. Specific steps are to be followed in order to be able to create your first MASM x86 assembly program with VS2010 / VS2012 (images from the configuration steps mentioned below are taken from here):

Expand the ‘Other Project Types‘ tree, Select ‘Visual Studio Solutions‘, and create a new ‘Blank Solution‘.

File | Add | New Project…

Expand the ‘Other Languages‘, ‘Visual C++‘, ‘General‘ section and create a new ‘Empty Project‘.

Now right click on the Project in the Solution Explorer and select ‘Build Customizations…‘.

Tick the ‘masm‘ box and say OK.

Add a new file to the project with the .asm extension by right clicking on the Project in the Solution Explorer and selecting ‘Add | New Item…‘ then ‘Text File‘. Enter a filename ending with .asm (e.g. test.asm). Press OK.

Now (and if you skipped the last steps, this won’t work) right click on the Project and select ‘Properties‘. You should see a dialog like this (Note the MASM item at the bottom of the tree). If you don’t, then something went wrong.

There are a few critical things to set up in the Linker options in order to get it to work:

Set the following property to Windows or Console as appropriate:

Configuration Properties > Linker > System> SubSystem

Set the entry point to the name of your main method (as per the END directive – see code):

Configuration Properties > Linker > Advanced > EntryPoint

All you have to do now is write some code and run it.

Visual Studio 2012 and MASM 

The things mentioned above also apply to Visual Studio 2012. You must also define Configuration Parameters for your project as shown in the picture below. 

I transfered the Huo_MASM_7 application from VS2010 to VS2012 and it worked perfectly with alsmost no issues. I say "almost" because the only thing I had to change was the SAFESEH compile parameter which had to change in NO for the program to sucessfully compile, as seen in the figure below.

Other than that, the program compiled and worked perfectly. 

x86-64 Bit Assembly Programming

The x86-64bit assembly language extends the 32 bit registers and also has some new ones. So it has, for example, rax and rcx which are the 64-bit versions of the eax, ecx 32-bit registers. It also defines 8 new registers (r8, r9, etc) and the 32-bit versions of these registers (r8d, r9d). You then "just" have to use these registers to play with 64 bit assembly programming. The important thing to know is that what you did with the stack (with push and pop commands) in previous 32-bit assembly programs, you have to do it only via registers in 64 bits. This could mean that porting a 32-bit program to 64 bits could be more complex than you thought it would be.

In more detail, the main changes regarding the registers in 64 bit x86 assembly are:

  • The EAX, EBX, ECX, EDX, ESI, EDI, EBP and ESP "general purpose" registers are all enlarged to 64-bits. The enlarged registers are accessed using RAX, RBX, RCX, RDX, RSI, RDI, RBP and RSP.
  • You can still access the low dword of these registers (i.e., the least significant 32 bits) by using the existing names EAX, EBX, ECX, EDX, ESI, EDI, EBP and ESP.
  • Eight (8) new registers are defined : r8, r9, r10, r11, r12, r13, r14, r15. The 32-versions of these registers are : r8d, r9d, r10d, .... The r8w, r9w, ... are the 16-bit variants (the low word) for these new registers and r8b, r9b, ... are the 8-bit variants (the low byte).

You have to have a tool to compile code for 64 bit. Possible tools to use include MASM 64-bit (search your PC for the ml64.exe file, which is usually packed with every Visual Studio) or GoAsm (see Internet references below).

MASM Resources in Internet

One can find more on assembly programming with MASM on the following sites:

32 bit Internet resources

  1. http://www.masm32.com/
  2. http://www.masm32.com/board/index.php
  3. http://www.infernodevelopment.com/introduction-masm32
  4. http://thestarman.pcministry.com/asm/win32/index.html
  5. http://www.piclist.com/techref/language/asm/win32asm.htm

64 bit Internet resources

  1. http://www.masm32.com/board/index.php?board=43.0
  2. http://www.godevtool.com/GoasmHelp/64bits.htm#easy [GoAsm]
  3. http://www.codegurus.be/codegurus/Programming/assembler&win64_en.htm
  4. http://www.masm32.com/board/index.php?topic=6557.0;prev_next=prev

History

  • Initial tutorial version posted: 2011-10-21
  • Article updated: 2011-10-26
  • Article updated: 2012-09-20 

License

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

Share

About the Author

Palavos
Software Developer Kakos Bros Solutions
Greece Greece
Spiros [Spyridon or Spyros are also used] Kakos (huo) lives in Athens, Greece. He is currently working as an IT consultant in a large firm. Begun programming during the Commodore era in MS Basic and is still trying to learn (mostly in C++ and C#)...
He likes chess and has recently bought a new (old) modem for one of his Commodores 128 (yes, he has two of them!) to set up a server based on 8-bit technology. He thinks that when the World Wide Web crashes completely by an alien cyber attack, he will be the only one capable of surfing with his Commodore computer and will eventually save the day...
He likes reading and writting philosophy and is a fond admirer of Aristotle and Alfred Russel Wallace. His main heritage is Harmonia Philosophica.
At his free time he is researching the application of polypyrrole (PPy) in the PCB manufacturing process (through-hole plating) at the National Technical University of Athens - Advanced Materials section.
Follow on   Twitter

Comments and Discussions

 
Suggestionx86 assembly online video course PinmemberMember 111387798-Oct-14 8:30 
QuestionAwesome PinmemberKyle Seidlitz9-Mar-14 20:46 
QuestionEntry point? Pinmembersoulfiremage2-Feb-14 1:07 
GeneralMy vote of 5 PinmemberM Rayhan18-Jan-14 21:28 
GeneralMy vote of 3 PinmemberRussriguez3-Jan-14 11:18 
QuestionMissing libraries? PinmemberMember 1035577423-Oct-13 15:23 
AnswerRe: Missing libraries? Pinmemberpwasser4-Mar-14 13:05 
QuestionDebug and Intellisense PinmemberMrDooDoo19-Sep-13 2:16 
AnswerRe: Debug and Intellisense PinmemberPalavos22-Sep-13 22:47 
Questionin which directory should I put MASM library? PinmemberMember 100285465-May-13 4:39 
AnswerRe: in which directory should I put MASM library? PinmemberPalavos15-May-13 3:04 
QuestionThanks the blog PinmemberSunday Medagbase3-Apr-13 15:09 
AnswerRe: Thanks the blog PinmemberPalavos15-May-13 3:03 
QuestionHow to load custom icons PinmemberBenceGado10-Feb-13 8:02 
AnswerRe: How to load custom icons Pinmemberruiwng21-Feb-13 21:22 
GeneralMy vote of 5 PinmemberPeter Hawke31-Jan-13 18:35 
GeneralMy vote of 5 PinmemberSM_Azarani17-Dec-12 19:47 
GeneralMy vote of 5 Pinmembergndnet24-Nov-12 3:05 
GeneralRe: My vote of 5 PinmemberPalavos26-Nov-12 12:38 
QuestionNaked! :) Pinmembernacnuduk19759-Oct-12 5:39 
GeneralMy vote of 5 PinmemberIvo Ivanov25-Sep-12 21:18 
GeneralRe: My vote of 5 PinmemberPalavos25-Sep-12 22:20 
GeneralMy vote of 5 PinmemberDr Bob25-Sep-12 10:18 
GeneralRe: My vote of 5 PinmemberPalavos25-Sep-12 11:35 
GeneralMy vote of 5 PinmemberAditya_Pandey21-Sep-12 3:18 
GeneralRe: My vote of 5 PinmemberPalavos25-Sep-12 11:29 
Questioninput output Pinmemberelwolv25-Oct-11 7:48 
AnswerRe: input output PinmemberPalavos26-Oct-11 7:46 
QuestionOnly 32 bit examples. Any 64 bit examples (64 bit registers, etc) ? Integration with CUDA? PinmemberBradTheCoder24-Oct-11 9:14 
AnswerRe: Only 32 bit examples. Any 64 bit examples (64 bit registers, etc) ? Integration with CUDA? [modified] PinmemberPalavos26-Oct-11 7:28 
GeneralAssembly Programming with Visual Studio 2010 Pinmembergoldcoastpainter21-Oct-11 4:43 
GeneralRe: Assembly Programming with Visual Studio 2010 PinmemberPalavos21-Oct-11 5:00 
GeneralRe: Assembly Programming with Visual Studio 2010 PinmvpAspDotNetDev21-Oct-11 13:10 
GeneralRe: Assembly Programming with Visual Studio 2010 PinmemberPalavos21-Oct-11 13:25 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 25 Sep 2012
Article Copyright 2011 by Palavos
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid