You are on page 1of 10

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

Not quite what you are looking for? You may want to try: Mixing .NET and Assembly Language in a standalone 64-bit exe Using the Free Visual C++ 2005 Express Edition to Develop Win32 Applications
9,048,017 members and growing! Email Password Sign in Join Lost password?

highlights off

Home

Articles

Quick Answers Help!

Discussions

Zones

Features

Community

visual studio + masm

General Programming Programming Tips General

Assembly Programming with Visual Studio 2010


By Palavos | 27 Oct 2011 | Article
Windows Intermediate VS2010 masm

Licence First Posted Views Downloads Bookmarked

CPOL 21 Oct 2011 15,908 1,205 53 times

See Also
More like this More by this author

An example of how to program in assembly by using Visual Studio 2010


Article Browse Code Stats Revisions (4) Alternatives
4.41 (14 votes) 8

Download required MASM library - 1.28 KB Download source and executable - 4.84 KB

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. 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

1 de 10

06/07/2012 3:15

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

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++). I have written a small introduction to assembly here. 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...
Collapse | Copy Code

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...
Collapse | Copy Code

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...
Collapse | Copy Code

Related Articles
Iterating through menustrip items Mocking the generic repository LINQ on the command-line Using DataPlotter implementing three kinds of Scan-converting Line Segments algorithm Dynamically Pointing to Shared Data Sources on SQL Reporting Services using a Report Definition Customization Extension (RDCE) Design the WCF service without implementing interface Merge DataGrid Header Wcf.js: Call WCF web services from Node.js NodeJS REST server trials to validate effective scripting How To Lock Device Screen For Windows Mobile Creating animations with Dundas Chart for ASP.NET Smarter Data Labels with Dundas Chart SmartLabels Understanding Chart Areas with Dundas Chart for .NET Using screensavers inside the Windows Media Player Making Sense of Geographic Data with Dundas Map and AJAX Handling connection notification

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:
Collapse | Copy Code

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:

2 de 10

06/07/2012 3:15

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

Collapse | Copy Code

call

crt__getch

between a desktop machine and Windows CE based devices SmartLink Create data-driven applications with the Hera Application Framework Towards the self-documenting database: extended properties Accessibility audit vs. accessibility testing Digital Signatures and PDF Documents

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

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 (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.

3 de 10

06/07/2012 3:15

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

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 wont 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 dont, then something went wrong.

4 de 10

06/07/2012 3:15

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

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

5 de 10

06/07/2012 3:15

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

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

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

6 de 10

06/07/2012 3:15

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

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

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

About the Author

Palavos

Software Developer Kakos Bros Solutions Greece Member

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.

Article Top

7 de 10

06/07/2012 3:15

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

Sign Up to vote

Poor

Excellent

Vot e

Comments and Discussions


You must Sign In to use this message board. (secure sign-in) Search this forum Profile popups Noise
Medium

Go
Expand Posts & Replies

Layout

Per page

10

Updat e Refresh input output elwolv First Prev Next 6:48 25 Oct '11

this is a hardware/software question? does the current MASM and labtop architecture allows any form of inp out form in Assembly commands, since we do not have a parallel port pins available? all hardware contact through USP? can we access USP pins through assembly directly by passing the OS driver mechanism? i.e. any of the ports in the laptop allows inp out stream latched or non-latched? elwolv

Sign In View Thread Permalink Re: input output Palavos 6:46 26 Oct '11

Assembly is a low level language so it can handle such things as data passing through an interface like USB or paraller ports. Unfortunately I do not have any relevant experience. Did you search the links I provide in the article? The MASM32 Forum could have something related to drivers of devices. Per ardua ad astra, per ignem vincimus! http://www.kakos.com.gr[^]

Sign In View Thread Permalink Only 32 bit examples. Any 64 bit examples (64 bit registers, etc) ? Integration with CUDA? 8:14 24 Oct '11 BradTheCoder

64 bit assembly is becoming important to integrate with CUDA. (NVIDIAs parallel computing architecture.) It would nice to see some 64 bit assembly intstruction sets in a tutorial.
Sign In View Thread Permalink Re: Only 32 bit examples. Any 64 bit examples (64 bit registers, etc) ? Integration with CUDA? [modified] 6:28 26 Oct '11 Palavos

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.

8 de 10

06/07/2012 3:15

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

As soon as I have time I will post a new tutorial with reference to these new registers. However I understand that the main set of commands used in 64-bit remains the same with the one used with MASM32. Per ardua ad astra, per ignem vincimus! http://www.kakos.com.gr[^]

-- modified 26 Oct '11.

Sign In View Thread Permalink Message Automatically Removed 3:43 21 Oct '11

goldcoastpainter

Message Automatically Removed


Re: Assembly Programming with Visual Studio 2010 Palavos 4:00 21 Oct '11

Thanks! Feel free to vote it if you like it! Will make more tutorials in the near future. Per ardua ad astra, per ignem vincimus! http://www.kakos.com.gr[^]

Sign In View Thread Permalink Re: Assembly Programming with Visual Studio 2010 12:10 21 Oct '11 AspDotNetDev

FYI, I'm pretty sure Gold Coast Painter is a spammer who just posted that very generic message to post that URL. Nice idea for an article, by the way. I have bookmarked it for future reading.
Somebody in an online forum wrote:

INTJs never really joke. They make a point. The joke is just a gift wrapper.

Sign In View Thread Permalink Re: Assembly Programming with Visual Studio 2010 12:25 21 Oct '11 Palavos

We live in a computer world. And what a "computer world" would that be without spam, bugs and stupid people interested in programming when you can do everything with "drag'n'drop" ? Thanks for the bookmark! Will try to make ti worth in the near future (as much as situation here in Greece allows me to do so)... Per ardua ad astra, per ignem vincimus! http://www.kakos.com.gr[^]

Sign In View Thread Permalink Last Visit: 18:00 31 Dec '99 General Joke Rant Last Update: 22:39 5 Jul '12 Question Bug Answer 1

News Suggestion Admin

Permalink | Advertise | Privacy | Mobile Layout: Web04 | 2.5.120703.1 | Last Updated 27 Oct 2011fixed | fluid Article Copyright 2011 by Palavos Everything else Copyright CodeProject, 1999-2012 Terms of Use

9 de 10

06/07/2012 3:15

Assembly Programming with Visual Studio 2010 - CodeProject

http://www.codeproject.com/Articles/271627/Assembly-Programming-...

10 de 10

06/07/2012 3:15

You might also like