10
× highlights off 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 Sign in Sign in Sign in Join Join Join Join Lost password? Home Articles Quick Answers Discussions Zones Features Community Help! visual studio + masm Article Browse Code Stats Revisions (4) Alternatives 8 » General Programming » Programming Tips » General Licence CPOL First Posted 21 Oct 2011 Views 15,908 Downloads 1,205 Bookmarked 53 times Assembly Programming with Visual Studio 2010 By Palavos | 27 Oct 2011 | Article Windows Intermediate VS2010 masm An example of how to program in assembly by using Visual Studio 2010 See Also More like this More by this author 4.41 (14 votes) 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 Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-... 1 de 10 06/07/2012 3:15

Assembly Programming With Visual Studio 2010 - CodeProject

Embed Size (px)

Citation preview

Page 1: Assembly Programming With Visual Studio 2010 - CodeProject

×

highlights off

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 inSign inSign inSign in JoinJoinJoinJoin Lost password?

Home Articles Quick Answers Discussions Zones Features

Community Help!visual studio + masm

Article Browse Code Stats Revisions (4) Alternatives

8

» General Programming » Programming Tips » General

Licence CPOLFirst Posted 21 Oct 2011

Views 15,908Downloads 1,205Bookmarked 53 times

Assembly

Programming with

Visual Studio 2010By Palavos | 27 Oct 2011 | Article

Windows Intermediate VS2010 masm

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

See Also

More like this

More by this author

4.41 (14 votes)

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

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

1 de 10 06/07/2012 3:15

Page 2: Assembly Programming With Visual Studio 2010 - CodeProject

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

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:

Related Articles

Iterating through menustrip items

Mocking the generic repository

LINQ on the command-line

Using DataPlotter implementingthree kinds of Scan-converting LineSegments algorithm

Dynamically Pointing to Shared DataSources on SQL Reporting Servicesusing a Report DefinitionCustomization Extension (RDCE)

Design the WCF service withoutimplementing interface

Merge DataGrid Header

Wcf.js: Call WCF web services fromNode.js

NodeJS REST server trials to validateeffective scripting

How To Lock Device Screen ForWindows Mobile

Creating animations with DundasChart for ASP.NET

Smarter Data Labels with DundasChart SmartLabels

Understanding Chart Areas withDundas Chart for .NET

Using screensavers inside theWindows Media Player

Making Sense of Geographic Datawith Dundas Map and AJAX

Handling connection notification

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

2 de 10 06/07/2012 3:15

Page 3: Assembly Programming With Visual Studio 2010 - CodeProject

Collapse | Copy Code

call crt__getch

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

between a desktop machine andWindows CE based devices

SmartLink

Create data-driven applications withthe Hera Application Framework

Towards the self-documentingdatabase: extended properties

Accessibility audit vs. accessibilitytesting

Digital Signatures and PDFDocuments

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

3 de 10 06/07/2012 3:15

Page 4: Assembly Programming With Visual Studio 2010 - CodeProject

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.

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

4 de 10 06/07/2012 3:15

Page 5: Assembly Programming With Visual Studio 2010 - CodeProject

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

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

5 de 10 06/07/2012 3:15

Page 6: Assembly Programming With Visual Studio 2010 - CodeProject

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

http://www.masm32.com/1.

http://www.masm32.com/board/index.php2.

http://www.infernodevelopment.com/introduction-masm323.

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

6 de 10 06/07/2012 3:15

Page 7: Assembly Programming With Visual Studio 2010 - CodeProject

Article Top

http://thestarman.pcministry.com/asm/win32/index.html4.

http://www.piclist.com/techref/language/asm/win32asm.htm5.

64 bit Internet resources

http://www.masm32.com/board/index.php?board=43.01.

http://www.godevtool.com/GoasmHelp/64bits.htm#easy [GoAsm]2.

http://www.codegurus.be/codegurus/Programming

/assembler&win64_en.htm

3.

http://www.masm32.com/board/index.php?topic=6557.0;

prev_next=prev

4.

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

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

7 de 10 06/07/2012 3:15

Page 8: Assembly Programming With Visual Studio 2010 - CodeProject

Sign Up to vote Poor Excellent VoteVoteVoteVote

Search this forum GoGoGoGo

Comments and Discussions

You must Sign In to use this message board. (secure sign-in)

Profile popups Noise Medium Layout Expand Posts & Replies Per page 10

UpdateUpdateUpdateUpdate

Refresh First Prev Next

elwolv 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

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

BradTheCoder8:14 24 Oct '11

64 bit assembly is becoming important to integrate with CUDA. (NVIDIA’s

parallel computing architecture.) It would nice to see some 64 bit assembly

intstruction sets in a tutorial.

Sign In· View Thread ·Permalink

Palavos

6:28 26 Oct '11

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.

input output

Re: input output

Only 32 bit examples. Any 64bit examples (64 bit registers,etc) ? Integration with CUDA?

Re: Only 32 bit examples.Any 64 bit examples (64 bitregisters, etc) ? Integrationwith CUDA? [modified]

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

8 de 10 06/07/2012 3:15

Page 9: Assembly Programming With Visual Studio 2010 - CodeProject

Permalink | Advertise | Privacy | MobileWeb04 | 2.5.120703.1 | Last Updated 27 Oct 2011

Article Copyright 2011 by PalavosEverything else Copyright © CodeProject, 1999-2012

Terms of Use

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

goldcoastpainter3:43 21 Oct '11

Message Automatically Removed

Palavos4: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

AspDotNetDev12:10 21 Oct '11

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

Palavos12:25 21 Oct '11

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 Last Update: 22:39 5 Jul '12 1

General News Suggestion Question Bug Answer

Joke Rant Admin

Layout:fixed | fluid

Message AutomaticallyRemoved

Re: Assembly Programmingwith Visual Studio 2010

Re: AssemblyProgramming with VisualStudio 2010

Re: AssemblyProgramming withVisual Studio 2010

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

9 de 10 06/07/2012 3:15

Page 10: Assembly Programming With Visual Studio 2010 - CodeProject

Assembly Programming with Visual Studio 2010 - CodeProject http://www.codeproject.com/Articles/271627/Assembly-Programming-...

10 de 10 06/07/2012 3:15