C++ With Assembly – MOV

C++ with Assembly is often times a complex subject with many C++ programmers. Assembly for many modern programs are not required as the preprocessor can provide just enough speed as programming in pure ASM can provide. It is better to learn how to write the best code for the preprocessor V.S. programming in ASM.  There are a few subjects that can utilize more speed in ASM then allowing the preprocessor convert the code to ASM but these will be covered in a later topic.

I recommend knowing basic C++, I am using Microsoft Visual Studio C++ 2010 Express for these tutorials.  Create a basic console application and include iostream I am going to be using the line using namespace std; in my examples to help keep the code clean for the beginners.

To inform the compiler what sections are using assembly code you can use either of the following:

__asm {

Code

}

Or  __asm code

If you are using a different compiler then Microsoft Visual Studio then there may be different methods depending on your compiler. Please research to your specific compiler to be certain.

Our first example is going to move the number 248 into a defined variable number1.

int Number1;

int Result;

__asm {

mov Number1, 248

mov EAX, Number1

Mov Result, EAX

}

The code is pointless as it does nothing that programming in pure C++ cannot do. Let’s examine the code.

We define two integers Number1 and Result and leave them blank.

__asm {

}

Then we create the __asm block by the following code.

Mov Number1, 248

This command we are moving number 248 into number1. If we was to do a cout << Number1; It would display the number 248.

MOV EAX, Number1

This command we are moving the number 248 into the EAX register(If you do not know what the EAX register is or simply a EAX register please refer to my register section or to Intel, IBM, or AMD developer documentations.)

Mov Result, EAX

This command moves what is in register EAX into the Result variable. Now we have put the number 248 into the result variable.

If we do a cout << Result; It should show 248.

There are execution commands such as Add and more that I will explain in other documents.

#include “stdafx.h”

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

//asm(“assembly code”);

int Number1;

int Result;

__asm {

mov Number1, 248

mov EAX, Number1

Mov Result, EAX

}

cout << “Number1 = ” << Number1 << endl;

return 0;

}

Recap

This document covered the basics of including basic assembly code in Microsoft Visual Studio c++. This example is rather useless as in C++ can be done in less typing but this code was just used as an example to get your feet wet with assembly.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: