C++ Assembly – ADD

This section of the assembly tutorial builds on the MOV tutorial and adds one execution command to the code. This is the ADD execution command. This is still a fairly pointless piece of code that can be executed just as well in pure C++ as in assembly.

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.  This tutorial also assumes that you have covered the previous tutorial of the MOV command.

In this example we are going to add the numbers 248 and 100 together and display them.

int Number1, Number2, Result;

__asm {

mov Number1, 248

mov Number2, 100

mov EAX, Number1

Add EAX, Number2

Mov Result, EAX

}

Let’s examine the code.

We define the integers Number1, Number2 and Result.

Mov Number1, 248

Mov Number2, 100

We initiate the two variables Number1 and Number2.

MOV EAX, Number1

This command we are moving the number 248 into the EAX register.

ADD EAX, Number2

This ASM line adds what is in EAX with Number2 and stores it in the EAX register.

Mov Result, EAX

We are moving the EAX register into the Result Variable.

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

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[])

{

int Number1, Number2, Result;

__asm {

mov Number1, 248

mov Number2, 100

mov EAX, Number1

Add EAX, Number2

Mov Result, EAX

}

cout << “Number1 = ” << Result << 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: